-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid_3.py
executable file
·195 lines (135 loc) · 4.59 KB
/
Grid_3.py
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
from copy import deepcopy
directionVectors = (UP_VEC, DOWN_VEC, LEFT_VEC, RIGHT_VEC) = ((-1, 0), (1, 0), (0, -1), (0, 1))
vecIndex = [UP, DOWN, LEFT, RIGHT] = range(4)
class Grid:
def __init__(self, size = 4):
self.size = size
self.map = [[0] * self.size for i in range(self.size)]
# Make a Deep Copy of This Object
def clone(self):
gridCopy = Grid()
gridCopy.map = deepcopy(self.map)
gridCopy.size = self.size
return gridCopy
# Insert a Tile in an Empty Cell
def insertTile(self, pos, value):
self.setCellValue(pos, value)
def setCellValue(self, pos, value):
self.map[pos[0]][pos[1]] = value
# Return All the Empty c\Cells
def getAvailableCells(self):
cells = []
for x in range(self.size):
for y in range(self.size):
if self.map[x][y] == 0:
cells.append((x,y))
return cells
# Return the Tile with Maximum Value
def getMaxTile(self):
maxTile = 0
for x in range(self.size):
for y in range(self.size):
maxTile = max(maxTile, self.map[x][y])
return maxTile
# Check If Able to Insert a Tile in Position
def canInsert(self, pos):
return self.getCellValue(pos) == 0
# Move the Grid
def move(self, dir):
dir = int(dir)
if dir == UP:
return self.moveUD(False)
if dir == DOWN:
return self.moveUD(True)
if dir == LEFT:
return self.moveLR(False)
if dir == RIGHT:
return self.moveLR(True)
# Move Up or Down
def moveUD(self, down):
r = range(self.size -1, -1, -1) if down else range(self.size)
moved = False
for j in range(self.size):
cells = []
for i in r:
cell = self.map[i][j]
if cell != 0:
cells.append(cell)
self.merge(cells)
for i in r:
value = cells.pop(0) if cells else 0
if self.map[i][j] != value:
moved = True
self.map[i][j] = value
return moved
# move left or right
def moveLR(self, right):
r = range(self.size - 1, -1, -1) if right else range(self.size)
moved = False
for i in range(self.size):
cells = []
for j in r:
cell = self.map[i][j]
if cell != 0:
cells.append(cell)
self.merge(cells)
for j in r:
value = cells.pop(0) if cells else 0
if self.map[i][j] != value:
moved = True
self.map[i][j] = value
return moved
# Merge Tiles
def merge(self, cells):
if len(cells) <= 1:
return cells
i = 0
while i < len(cells) - 1:
if cells[i] == cells[i+1]:
cells[i] *= 2
del cells[i+1]
i += 1
def canMove(self, dirs = vecIndex):
# Init Moves to be Checked
checkingMoves = set(dirs)
for x in range(self.size):
for y in range(self.size):
# If Current Cell is Filled
if self.map[x][y]:
# Look Ajacent Cell Value
for i in checkingMoves:
move = directionVectors[i]
adjCellValue = self.getCellValue((x + move[0], y + move[1]))
# If Value is the Same or Adjacent Cell is Empty
if adjCellValue == self.map[x][y] or adjCellValue == 0:
return True
# Else if Current Cell is Empty
elif self.map[x][y] == 0:
return True
return False
# Return All Available Moves
def getAvailableMoves(self, dirs = vecIndex):
availableMoves = []
for x in dirs:
gridCopy = self.clone()
if gridCopy.move(x):
availableMoves.append(x)
return availableMoves
def crossBound(self, pos):
return pos[0] < 0 or pos[0] >= self.size or pos[1] < 0 or pos[1] >= self.size
def getCellValue(self, pos):
if not self.crossBound(pos):
return self.map[pos[0]][pos[1]]
else:
return None
if __name__ == '__main__':
g = Grid()
g.map[0][0] = 2
g.map[1][0] = 2
g.map[3][0] = 4
while True:
for i in g.map:
print(i)
print(g.getAvailableMoves())
v = input()
g.move(v)