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)>2009-12-05 21:52:23 (GMT)
committer Walter Bender <walter@walter-laptop.(none)>2009-12-05 21:52:23 (GMT)
commitc2daa3ea24f91bbf3daa5711d0b5971a587eb610 (patch)
tree26a010a4ed194d14e80916d24e465c70aa9f785b /grid.py
parent7cd15de24ee4f27a392129dbddfaf7d1c27cf156 (diff)
automated dealing of extra cards
Diffstat (limited to 'grid.py')
-rw-r--r--grid.py97
1 files changed, 33 insertions, 64 deletions
diff --git a/grid.py b/grid.py
index 02c1bd1..c211da9 100644
--- a/grid.py
+++ b/grid.py
@@ -26,39 +26,27 @@ import random
from sprites import *
from card import *
+from deck import *
from constants import *
#
-# class for defining 4x3 matrix of cards
+# class for managing 4x3 matrix of cards
#
class Grid:
def __init__(self, tw):
# the playing surface is a 3x4 grid
self.grid = []
- # create the deck of cards
- self.deck = {}
- # remember the position in the deck
- self.index = 0
- # how many cards are in the deck?
- self.count = 0
# how many cards are on the playing field
self.cards = 0
# card spacing
self.left = int((tw.width-(tw.card_w*5.5*tw.scale))/2)
self.xinc = int(tw.card_w*1.5*tw.scale)
- self.top = int((tw.height-(tw.card_h*3.5*tw.scale))/2)
+ # self.top = int((tw.height-(tw.card_h*3.5*tw.scale))/2)
+ self.top = 10
self.yinc = int(tw.card_h*1.25*tw.scale)
- # Initialize the deck of cards
- # some loop through all the patterns
- for shape in range(0,SHAPES):
- for color in range(0,COLORS):
- for num in range(0,NUMBER):
- for fill in range(0,FILLS):
- self.deck[self.count] = Card(tw,shape,color,num,fill)
- self.count += 1
-
+ # deal the initial 12 cards
def deal(self, tw):
# layout the initial 12 cards from the deck
# find upper left corner of grid
@@ -68,67 +56,43 @@ class Grid:
y = self.top
for r in range(0,3):
for c in range(0,4):
- self.grid.append(self.deck[self.index])
- self.draw_a_card(x,y)
+ a = tw.deck.deal_next_card()
+ self.grid.append(a)
+ self.place_a_card(a,x,y)
x += self.xinc
self.cards += 1
- self.grid.append(None) # leave a space for the extra cards
+ # leave a space for the extra cards
+ self.grid.append(None)
x = self.left
y += self.yinc
- def deal_3_extra_cards(self, tw):
+ # add cards when there is no match
+ def deal_extra_cards(self, tw):
# if there are still cards in the deck and only 12 cards in the grid
- if self.index < self.count and self.cards == DEAL:
+ if tw.deck.empty() is False and self.cards == DEAL:
# add 3 extra cards to the playing field
for r in range(0,3):
i = self.grid.index(None)
- self.grid[i] = self.deck[self.index]
+ self.grid[i] = tw.deck.deal_next_card()
x = self.left+self.xinc*(i%5)
y = self.top+self.yinc*int(i/5)
- self.draw_a_card(x,y)
+ self.place_a_card(self.grid[i],x,y)
self.cards += 1
- # shuffle the deck
- def shuffle(self):
- # hide all the cards
- for c in self.deck:
- self.deck[c].hide_card()
- # randomize the deck
- for n in range(0,DECKSIZE*4):
- i = random.randrange(DECKSIZE)
- j = random.randrange(DECKSIZE)
- self.swap_cards(i,j)
- # reset the index to the beginning of the deck after a shuffle
- self.index = 0
- return
-
- def swap_cards(self,i,j):
- tmp = self.deck[j]
- self.deck[j] = self.deck[i]
- self.deck[i] = tmp
- return
-
- # given a spr, find the corresponding card in the deck
- def spr_to_card(self, spr):
- for c in self.deck:
- if self.deck[c].spr == spr:
- return self.deck[c]
- return None
-
# remove a set from grid
# and deal new cards from the deck
def remove_and_replace(self, clicked_set, tw):
for a in clicked_set:
+ # find the position in the grid of the clicked card
+ i = self.xy_to_grid(a.x,a.y)
# only add new cards if we are down to 12 cards
- i = int(5*(a.y-self.top)/self.yinc) + \
- int((a.x-self.left)/self.xinc)
if self.cards == DEAL:
- if self.index < self.count:
- # save card in grid position of card we are replacing
- self.grid[i] = self.deck[self.index]
- self.draw_a_card(a.x,a.y)
- else:
+ if tw.deck.empty():
self.grid[i] = None
+ else:
+ # save card in grid position of card we are replacing
+ self.grid[i] = tw.deck.deal_next_card()
+ self.place_a_card(self.grid[i],a.x,a.y)
else:
self.cards -= 1
# mark grid positions of cards we are not replacing
@@ -136,10 +100,15 @@ class Grid:
# move clicked card to the set area
a.x = 10
a.y = self.top + clicked_set.index(a)*self.yinc
- self.spr_to_card(a).show_card()
+ tw.deck.spr_to_card(a).show_card()
+
+ # place a card at position x,y and display it
+ def place_a_card(self,c,x,y):
+ if c is not None:
+ c.spr.x = x
+ c.spr.y = y
+ c.show_card()
- def draw_a_card(self,x,y):
- self.deck[self.index].spr.x = x
- self.deck[self.index].spr.y = y
- self.deck[self.index].show_card()
- self.index += 1
+ # convert from sprite x,y to grid index
+ def xy_to_grid(self,x,y):
+ return int(5*(y-self.top)/self.yinc) + int((x-self.left)/self.xinc)