Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/grid.py
blob: db3ccb2ba7abdd6e91dd18938a823bbd9309c350 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#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))


#
# class for defining 3x3 matrix of cards
#
class Grid:
    # 012
    # 345
    # 678
    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

    # reset everything to initial layout
    def reset3x3(self, tw):
        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()

    # TWO_BY_TWO = ((7,5,0,3),(7,4,5,2),(1,3,5,8),(4,5,6,1))
    # reset everything to initial layout
    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
    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))
    # 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()

    # swap card a and card b
    # swap their entries in the grid and the position of their sprites
    def swap(self, 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 test3x3(self):
        for i in (0,1,3,4,6,7):
            if 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 + \
               self.card_table[self.grid[i+3]].north != 0:
                return False
        return True

    # test all relevant borders, ignoring borders on the blank card
    def test2x3(self):
        for i in (0,3,6):
            if self.card_table[self.grid[i]].east + \
               self.card_table[self.grid[i+1]].west != 0:
                return False
        for i in (0,1,3,4):
            if self.card_table[self.grid[i]].south + \
               self.card_table[self.grid[i+3]].north != 0:
                return False
        return True

    # test all relevant borders, ignoring borders on the blank card
    def test3x2(self):
        for i in (0,1,3,4):
            if self.card_table[self.grid[i]].east + \
               self.card_table[self.grid[i+1]].west != 0:
                return False
        for i in (0,1,2):
            if self.card_table[self.grid[i]].south + \
               self.card_table[self.grid[i+3]].north != 0:
                return False
        return True

    # test all relevant borders, ignoring borders on the blank card
    def test2x2(self):
        for i in (0,3):
            if self.card_table[self.grid[i]].east + \
               self.card_table[self.grid[i+1]].west != 0:
                return False
        for i in (0,1):
            if self.card_table[self.grid[i]].south + \
               self.card_table[self.grid[i+3]].north != 0:
                return False
        return True