1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#Copyright (c) 2009, Walter Bender
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
import pygtk
pygtk.require('2.0')
import gtk
import gobject
from sprites import *
from card import *
CARD_DEFS = ((1,3,-2,-3),(2,3,-3,-2),(2,3,-4,-4),
(2,1,-1,-4),(3,4,-4,-3),(4,2,-1,-2),
(1,1,-2,-4),(4,2,-3,-4),(1,3,-1,-2),
(0,0,0,0))
#
# class for defining 3x3 matrix of cards
#
class Grid:
# 012
# 345
# 678
# 9 is an extra (blank) card that matches everything
def __init__(self, tw):
self.grid = [0,1,2,3,4,5,6,7,8,9]
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
# 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:
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
if i == 9: # put the extra (blank) card off the screen
y = self.h
# reset everything to initial layout
def reset(self, tw):
self.set_grid([0,1,2,3,4,5,6,7,8,9])
self.set_orientation([0,0,0,0,0,0,0,0,0,0])
for i in range(9):
self.card_table[i].reload_image(tw, i)
# 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)
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)
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()
# swap in/out the blank card
def toggle_blank(self):
self.swap(5,9)
# swap card a and card b
# swap their entries in the grid and the position of their sprites
def swap(self, a, b):
self.print_grid()
print a, b
# swap grid elements and x,y positions of sprites
# print "swapping cards " + str(a) + " and " + str(b)
ai = self.grid.index(a)
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
# print the grid
def print_grid(self):
print self.grid[0:3]
print self.grid[3:6]
print self.grid[6:9]
return
# print the grid orientations
def print_orientations(self):
print self.card_table[self.grid[0]].orientation,\
self.card_table[self.grid[1]].orientation,\
self.card_table[self.grid[2]].orientation
print self.card_table[self.grid[3]].orientation,\
self.card_table[self.grid[4]].orientation,\
self.card_table[self.grid[5]].orientation
print self.card_table[self.grid[6]].orientation,\
self.card_table[self.grid[7]].orientation,\
self.card_table[self.grid[8]].orientation
return
# test all relevant borders, ignoring borders on the blank card
def test(self):
for i in (0,1,3,4,6,7):
if self.card_table[self.grid[i]].east != 0 and \
self.card_table[self.grid[i+1]].west != 0 and \
self.card_table[self.grid[i]].east + \
self.card_table[self.grid[i+1]].west != 0:
return False
for i in (0,1,2,3,4,5):
if self.card_table[self.grid[i]].south != 0 and \
self.card_table[self.grid[i+3]].north != 0 and \
self.card_table[self.grid[i]].south + \
self.card_table[self.grid[i+3]].north != 0:
return False
return True
|