Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/grid.py
diff options
context:
space:
mode:
authorWalter Bender <walter@walter-laptop.(none)>2010-02-24 14:41:48 (GMT)
committer Walter Bender <walter@walter-laptop.(none)>2010-02-24 14:41:48 (GMT)
commitd0f2e2eb2995c01789f8604f80b7e4d202388192 (patch)
treebebc2551c8b6df9f7af1c3e29c165e5961be2fc1 /grid.py
parentbba203b7b8333dca6e27ff7f365f2eaa620efd2f (diff)
majore refactoring
Diffstat (limited to 'grid.py')
-rw-r--r--grid.py172
1 files changed, 92 insertions, 80 deletions
diff --git a/grid.py b/grid.py
index db3ccb2..67646a5 100644
--- a/grid.py
+++ b/grid.py
@@ -23,7 +23,7 @@ pygtk.require('2.0')
import gtk
import gobject
-from sprites import *
+from random import uniform
from card import *
CARD_DEFS = ((1,3,-2,-3),(2,3,-3,-2),(2,3,-4,-4),
@@ -32,96 +32,110 @@ CARD_DEFS = ((1,3,-2,-3),(2,3,-3,-2),(2,3,-4,-4),
#
-# class for defining 3x3 matrix of cards
+# Class for defining 3x3 matrix of cards
#
class Grid:
- # 012
- # 345
- # 678
+ """
+ Grid positions correspond to one of:
+ 012 01x 012 01x
+ 345 34x 345 34x
+ 678 xxx xxx 67x
+ """
def __init__(self, tw):
- self.grid = [0,1,2,3,4,5,6,7,8,9]
+ self.grid = [0,1,2,3,4,5,6,7,8]
self.card_table = {}
- # stuff to keep around for the graphics
- self.w = tw.width
- self.h = tw.height
- self.d = tw.card_dim
- self.s = tw.scale
+ # Stuff to keep around for the graphics
+ self.w = int(tw.width)
+ self.h = int(tw.height)
+ self.d = int(tw.card_dim*tw.scale)
# Initialize the cards
i = 0 # i is used as a label on the sprite
- x = int((self.w-(self.d*3*self.s))/2)
- y = int((self.h-(self.d*3*self.s))/2)
for c in CARD_DEFS:
+ x, y = self.i_to_xy(i)
self.card_table[i] = Card(tw,c,i,x,y)
- self.card_table[i].draw_card()
- x += int(self.d*self.s)
- if x > (self.w+(self.d*2*self.s))/2:
- x = int((self.w-(self.d*3*self.s))/2)
- y += int(self.d*self.s)
i += 1
- # reset everything to initial layout
+ # Utility functions
+ def i_to_xy(self, i):
+ return int((self.w-(self.d*3))/2) + (i%3)*self.d,\
+ int((self.h-(self.d*3))/2) + int(i/3)*self.d
+
+ def xy_to_i(self, x, y):
+ return (x-int((self.w-(self.d*3))/2))/self.d +\
+ ((y-int((self.h-(self.d*3))/2))/self.d)*3
+
+ def set_orientation(self, neworientation, draw_card=True):
+ for c in range(9):
+ self.card_table[c].set_orientation(neworientation[c], draw_card)
+
+ def randomize_orientation(self, draw_card=True):
+ olist = [0,90,180,270]
+ for c in range(9):
+ o = int(uniform(0,4))
+ self.card_table[c].set_orientation(olist[o], draw_card)
+
+ def set_grid(self, newgrid):
+ for i, c in enumerate(newgrid):
+ x, y = self.i_to_xy(i)
+ self.card_table[c].spr.move((x,y))
+ self.grid[i] = c
+
+ def show_all(self):
+ for i in range(9):
+ self.card_table[i].spr.set_layer(100)
+
+ def hide_list(self, list):
+ for i in list:
+ self.card_table[i].spr.hide()
+
+ # Reset everything to initial layout
def reset3x3(self, tw):
+ self.show_all()
self.set_grid([0,1,2,3,4,5,6,7,8])
- self.set_orientation([0,0,0,0,0,0,0,0,0])
- for i in range(9):
- self.card_table[i].reload_image(tw, i)
- self.print_grid()
+ self.randomize_orientation()
- # TWO_BY_TWO = ((7,5,0,3),(7,4,5,2),(1,3,5,8),(4,5,6,1))
- # reset everything to initial layout
+ # Two by two = ((7,5,0,3),(7,4,5,2),(1,3,5,8),(4,5,6,1))
def reset2x2(self, tw):
- self.set_grid([4,5,0,6,1,2,3,7,8])
- self.set_orientation([0,0,0,0,0,0,0,0,0])
- for i in range(9):
- self.card_table[i].reload_image(tw, i)
- for i in (0,2,3,7,8):
- hide(self.card_table[i].spr)
- self.print_grid()
-
- # THREE_BY_TWO = ((7,5,0,2,4,3),(5,6,1,4,3,8))
- # reset everything to initial layout
+ self.show_all()
+ self.randomize_orientation()
+ r = int(uniform(0,4))
+ if r == 0:
+ self.set_grid([7,5,1,0,3,2,4,6,8])
+ self.hide_list([1,2,4,6,8])
+ elif r == 1:
+ self.set_grid([7,4,1,5,2,3,0,6,8])
+ self.hide_list([0,1,3,6,8])
+ elif r == 2:
+ self.set_grid([1,3,2,5,8,4,6,7,0])
+ self.hide_list([2,4,6,7,0])
+ else:
+ self.set_grid([4,5,0,6,1,2,3,7,8])
+ self.hide_list([0,2,3,7,8])
+
+ # Three by two = ((7,5,0,2,4,3),(5,6,1,4,3,8))
def reset3x2(self, tw):
- self.set_grid([7,5,0,2,4,3,1,6,8])
- self.set_orientation([0,0,0,0,0,0,0,0,0])
- for i in range(9):
- self.card_table[i].reload_image(tw, i)
- for i in (1,6,8):
- hide(self.card_table[i].spr)
- self.print_grid()
-
- # TWO_BY_THREE = ((5,2,4,6,1,7),(7,1,2,5,8,0))
+ self.show_all()
+ self.randomize_orientation()
+ r = int(uniform(0,2))
+ if r == 0:
+ self.set_grid([7,5,0,2,4,3,1,6,8])
+ self.hide_list([1,6,8])
+ else:
+ self.set_grid([5,6,1,4,3,8,0,2,7])
+ self.hide_list([0,2,7])
+
+ # Two by three = ((5,2,4,6,1,7),(7,1,2,5,8,0))
# reset everything to initial layout
def reset2x3(self, tw):
- self.set_grid([5,2,0,4,6,3,1,7,8])
- self.set_orientation([0,0,0,0,0,0,0,0,0])
- for i in range(9):
- self.card_table[i].reload_image(tw, i)
- for i in (0,3,8):
- hide(self.card_table[i].spr)
- self.print_grid()
-
- # force a specific layout
- def set_grid(self, newgrid):
- x = int((self.w-(self.d*3*self.s))/2)
- y = int((self.h-(self.d*3*self.s))/2)
- j = 0
- for c in newgrid:
- for i in range(9):
- if self.card_table[i].spr.label == c:
- self.card_table[i].spr.x = x
- self.card_table[i].spr.y = y
- self.card_table[i].draw_card()
- x += int(self.d*self.s)
- if x > (self.w+(self.d*2*self.s))/2:
- x = int((self.w-(self.d*3*self.s))/2)
- y += int(self.d*self.s)
- self.grid[j] = c
- j+=1
-
- def set_orientation(self, neworientation):
- for c in range(9):
- self.card_table[c].set_orientation(neworientation[c],True)
- self.card_table[c].draw_card()
+ self.show_all()
+ self.randomize_orientation()
+ r = int(uniform(0,2))
+ if r == 0:
+ self.set_grid([5,2,0,4,6,3,1,7,8])
+ self.hide_list([0,3,8])
+ else:
+ self.set_grid([7,1,3,2,5,4,8,0,6])
+ self.hide_list([3,4,6])
# swap card a and card b
# swap their entries in the grid and the position of their sprites
@@ -132,12 +146,10 @@ class Grid:
bi = self.grid.index(b)
self.grid[bi] = a
self.grid[ai] = b
- x = self.card_table[a].spr.x
- y = self.card_table[a].spr.y
- self.card_table[a].spr.x = self.card_table[b].spr.x
- self.card_table[a].spr.y = self.card_table[b].spr.y
- self.card_table[b].spr.x = x
- self.card_table[b].spr.y = y
+ ax,ay = self.card_table[a].spr.get_xy()
+ bx,by = self.card_table[b].spr.get_xy()
+ self.card_table[a].spr.move((bx,by))
+ self.card_table[b].spr.move((ax,ay))
# print the grid
def print_grid(self):