-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridModel.py
More file actions
179 lines (166 loc) · 6.65 KB
/
GridModel.py
File metadata and controls
179 lines (166 loc) · 6.65 KB
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
from random import choice, randint
class GridModel():
def __init__(self):
self.grid = [ [0]*4 for i in range(4)]
self.score = 0
def reset(self) -> None:
self.score = 0
for row in range(4):
for col in range(4):
self.grid[row][col] = 0
def addNewValue(self) -> bool:
available = []
for row in range(4):
for col in range(4):
if self.grid[row][col] == 0:
available.append([row, col])
if len(available) == 0:
return False
else:
row, col = choice(available)
valueThreshold = randint(0, 7)
cellVal = 4 if valueThreshold < 2 else 2
self.grid[row][col] = cellVal
return True
def moveUp(self) -> bool:
valid = False
for col in range(4):
for row in range(3):
# Merge any duplicate cells
cellValue = self.grid[row][col]
if cellValue != 0:
for i in range(row + 1, 4):
targetCellValue = self.grid[i][col]
if targetCellValue == 0:
continue
if targetCellValue == cellValue:
self.doubleCell(row, col)
self.clearCell(i, col)
valid = True
break
else:
break
for row in range(3):
# Remove any gaps by shifting digits up.
cellValue = self.grid[row][col]
if cellValue == 0:
for i in range(row + 1, 4):
targetCellValue = self.grid[i][col]
if targetCellValue != 0:
self.grid[row][col] = targetCellValue
self.clearCell(i, col)
valid = True
break
return valid
def moveLeft(self) -> bool:
valid = False
for row in range(4):
for col in range(3):
# Merge any duplicate cells
cellValue = self.grid[row][col]
if cellValue != 0:
for i in range(col + 1, 4):
targetCellValue = self.grid[row][i]
if targetCellValue == 0:
continue
if targetCellValue == cellValue:
self.doubleCell(row, col)
self.clearCell(row, i)
valid = True
break
else:
break
for col in range(3):
# Remove any gaps by shifting digits up.
cellValue = self.grid[row][col]
if cellValue == 0:
for i in range(col + 1, 4):
targetCellValue = self.grid[row][i]
if targetCellValue != 0:
self.grid[row][col] = targetCellValue
self.clearCell(row, i)
valid = True
break
return valid
def moveDown(self) -> bool:
valid = False
for col in range(4):
for row in range(3, 0, -1):
# Merge any duplicate cells
cellValue = self.grid[row][col]
if cellValue != 0:
for i in range(row - 1, -1, -1):
targetCellValue = self.grid[i][col]
if targetCellValue == 0:
continue
if targetCellValue == cellValue:
self.doubleCell(row, col)
self.clearCell(i, col)
valid = True
break
else:
break
for row in range(3, 0, -1):
# Remove any gaps by shifting digits up.
cellValue = self.grid[row][col]
if cellValue == 0:
for i in range(row -1, -1, -1):
targetCellValue = self.grid[i][col]
if targetCellValue != 0:
self.grid[row][col] = targetCellValue
self.clearCell(i, col)
valid = True
break
return valid
def moveRight(self) -> bool:
valid = False
for row in range(4):
for col in range(3, 0, -1):
# Merge any duplicate cells
cellValue = self.grid[row][col]
if cellValue != 0:
for i in range(col - 1, -1, -1):
targetCellValue = self.grid[row][i]
if targetCellValue == 0:
continue
if targetCellValue == cellValue:
self.doubleCell(row, col)
self.clearCell(row, i)
valid = True
break
else:
break
for col in range(3, 0, -1):
# Remove any gaps by shifting digits up.
cellValue = self.grid[row][col]
if cellValue == 0:
for i in range(col - 1, -1, -1):
targetCellValue = self.grid[row][i]
if targetCellValue != 0:
self.grid[row][col] = targetCellValue
self.clearCell(row, i)
valid = True
break
return valid
def doubleCell(self, row, col) -> None:
value = self.grid[row][col] * 2
self.score = self.score + value
self.grid[row][col] = value
def clearCell(self, row, col) -> None:
self.grid[row][col] = 0
def checkLegalMove(self) -> bool:
for row in range(4):
for col in range(4):
if self.grid[row][col] == 0:
return True
if col < 3:
if self.grid[row][col] == self.grid[row][col + 1]:
return True
if row < 3:
if self.grid[row][col] == self.grid[row+1][col]:
return True
return False
def getCellValue(self, row, col) -> int:
return self.grid[row][col]
def getScore(self) -> int:
return self.score