diff options
author | Walter Bender <walter@walter-laptop.(none)> | 2009-11-29 21:43:54 (GMT) |
---|---|---|
committer | Walter Bender <walter@walter-laptop.(none)> | 2009-11-29 21:43:54 (GMT) |
commit | 36787ae146159ac9573e9d81dcde0a5ef0f3b50f (patch) | |
tree | d3c1023c6a337dec23eacf493cabd1757880543a | |
parent | ae13312f3034e66d3023e8ebaf8e3916a2a84262 (diff) |
added a match search button
-rw-r--r-- | VisualMatchActivity.py | 35 | ||||
-rw-r--r-- | card.py | 2 | ||||
-rw-r--r-- | grid.py | 20 | ||||
-rw-r--r-- | window.py | 42 |
4 files changed, 81 insertions, 18 deletions
diff --git a/VisualMatchActivity.py b/VisualMatchActivity.py index afba107..f58363e 100644 --- a/VisualMatchActivity.py +++ b/VisualMatchActivity.py @@ -67,7 +67,7 @@ class VisualMatchActivity(activity.Activity): toolbar_box.toolbar.insert(activity_button, 0) activity_button.show() - # New game button + # New-game Button self.button1 = ToolButton( "new-game" ) self.button1.set_tooltip(_('New game')) self.button1.props.sensitive = True @@ -75,7 +75,7 @@ class VisualMatchActivity(activity.Activity): toolbar_box.toolbar.insert(self.button1, -1) self.button1.show() - # Add three extra cards button + # Add-three-extra-cards Button self.button2 = ToolButton( "plus-3" ) self.button2.set_tooltip(_('Add three extra cards')) self.button2.props.sensitive = True @@ -83,6 +83,14 @@ class VisualMatchActivity(activity.Activity): toolbar_box.toolbar.insert(self.button2, -1) self.button2.show() + # Help Button + self.button3 = ToolButton( "search" ) + self.button3.set_tooltip(_('Is there a match?')) + self.button3.props.sensitive = True + self.button3.connect('clicked', self._button3_cb, self) + toolbar_box.toolbar.insert(self.button3, -1) + self.button3.show() + separator = gtk.SeparatorToolItem() separator.show() toolbar_box.toolbar.insert(separator, -1) @@ -188,6 +196,16 @@ class VisualMatchActivity(activity.Activity): (tw.deck.count-tw.deck.index)) self.button2.set_icon("plus-3") + def _button3_cb(self, button, activity): + self.show_button3(activity.tw) + return True + + def show_button3(self, tw): + if window.find_a_match(tw) is True: + tw.activity.status_label.set_text(_("Keep looking.")) + else: + tw.activity.status_label.set_text(_("No matches found.")) + def _journal_cb(self, button, path): title_alert = NamingAlert(self, path) title_alert.set_transient_for(self.get_toplevel()) @@ -204,7 +222,7 @@ class ProjectToolbar(gtk.Toolbar): gtk.Toolbar.__init__(self) self.activity = pc - # New game button + # New-game Button self.activity.button1 = ToolButton( "new-game" ) self.activity.button1.set_tooltip(_('New game')) self.activity.button1.props.sensitive = True @@ -213,7 +231,7 @@ class ProjectToolbar(gtk.Toolbar): self.insert(self.activity.button1, -1) self.activity.button1.show() - # Add three extra cards button + # Add-three-extra-cards Button self.activity.button2 = ToolButton( "plus-3" ) self.activity.button2.set_tooltip(_('Add three extra cards')) self.activity.button2.props.sensitive = True @@ -222,6 +240,15 @@ class ProjectToolbar(gtk.Toolbar): self.insert(self.activity.button2, -1) self.activity.button2.show() + # Help Button + self.activity.button3 = ToolButton( "search" ) + self.activity.button3.set_tooltip(_('Is there a match?')) + self.activity.button3.props.sensitive = True + self.activity.button3.connect('clicked', self.activity._button3_cb, + self.activity) + self.insert(self.activity.button3, -1) + self.activity.button3.show() + separator = gtk.SeparatorToolItem() separator.set_draw(True) self.insert(separator, -1) @@ -61,7 +61,7 @@ class Card: tw.card_h*tw.scale)) self.spr.label = "" - def draw_card(self): + def show_card(self): setlayer(self.spr,2000) draw(self.spr) @@ -66,10 +66,10 @@ 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) - self.grid.append(True) x += self.xinc - self.grid.append(False) # leave a space for the extra cards + self.grid.append(None) # leave a space for the extra cards x = self.left y += self.yinc @@ -79,8 +79,8 @@ class Grid: # add 3 extra cards to the playing field self.cards = 15 for r in range(0,3): - i = self.grid.index(False) - self.grid[i] = True + i = self.grid.index(None) + self.grid[i] = self.deck[self.index] x = self.left+self.xinc*(i%5) y = self.top+self.yinc*int(i/5) self.draw_a_card(x,y) @@ -117,19 +117,21 @@ class Grid: def remove_and_replace(self, clicked_set, tw): for a in clicked_set: # 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 == 12: 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: self.cards -= 1 # mark grid positions of cards we are not replacing - i = int(5*(a.y-self.top)/self.yinc) + \ - int((a.x-self.left)/self.xinc) - self.grid[i] = False + self.grid[i] = None # 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).draw_card() + self.spr_to_card(a).show_card() # Any more cards in the deck? if self.index < self.count: return True @@ -139,5 +141,5 @@ class Grid: 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].draw_card() + self.deck[self.index].show_card() self.index += 1 @@ -137,13 +137,14 @@ def _button_release_cb(win, event, tw): return True # add the selected card to the list + # and show the selection mask for a in tw.clicked: if a is None: i = tw.clicked.index(a) tw.clicked[i] = spr tw.selected[i].spr.x = spr.x tw.selected[i].spr.y = spr.y - tw.selected[i].draw_card() + tw.selected[i].show_card() break # we only want to add the card to the list once # if we have three cards selected, test for a set @@ -151,7 +152,7 @@ def _button_release_cb(win, event, tw): try: tw.clicked.index(None) except ValueError: - if set_check([tw.deck.spr_to_card(tw.clicked[0]), + if match_check([tw.deck.spr_to_card(tw.clicked[0]), tw.deck.spr_to_card(tw.clicked[1]), tw.deck.spr_to_card(tw.clicked[2])]): if tw.deck.remove_and_replace(tw.clicked, tw) is None: @@ -196,12 +197,24 @@ def _expose_cb(win, event, tw): def _destroy_cb(win, event, tw): gtk.main_quit() + +# +# Check to see whether there are any matches on the board +# +def find_a_match(tw): + a = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] + for i in Permutation(a): + cardarray = [tw.deck.grid[i[0]],tw.deck.grid[i[1]],tw.deck.grid[i[2]]] + if match_check(cardarray) is True: + return True + return False + # -# Check whether three cards are a set based on the criteria that +# Check whether three cards are a match based on the criteria that # in all characteristics: # either all cards are the same of all cards are different # -def set_check(cardarray): +def match_check(cardarray): for a in cardarray: if a is None: return False @@ -228,3 +241,24 @@ def set_check(cardarray): cardarray[0].color != cardarray[2].color: return False return True + +# +# Permutaion class for checking for all possible matches on the grid +# +class Permutation: + def __init__(self, justalist): + self._data = justalist[:] + self._sofar = [] + def __iter__(self): + return self.next() + def next(self): + for elem in self._data: + if elem not in self._sofar: + self._sofar.append(elem) + if len(self._sofar) == 3: + yield self._sofar[:] + else: + for v in self.next(): + yield v + self._sofar.pop() + |