-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogics.py
More file actions
193 lines (164 loc) · 4.25 KB
/
logics.py
File metadata and controls
193 lines (164 loc) · 4.25 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""
Filename: operation_logic.py
Author: zhuangr
date: 05/04/2017
"""
"""
This file is about the logic of operations in the game of 2048.
It is based on some concepts of matrix in the operations. The
operations will be interpret in the method of a class
"""
import random
import interfaces
def matrix(n):
"""create an n*n matrix"""
class Oper:
"""A class that initial the matrix and interpret the operations
of the game of 2048"""
def __init__(self,rows = 4):
"""Constructor: initialize the matrix"""
self.mat = []
for i in range(rows):
row = []
for i in range(rows):
row.append(0)
self.mat.append(row)
def matrix(self, n):
"""create an n*n zero matrix"""
matrix = []
for i in range(n):
row=[]
for i in range(n):
row.append(0)
matrix.append(row)
return matrix
def random_update(self):
"""randomly assigned one empty cell with number 2 or 4"""
a = random.randrange(0, len(self.mat))
b = random.randrange(0, len(self.mat))
while self.mat[a][b] != 0:
a = random.randrange(0, len(self.mat))
b = random.randrange(0, len(self.mat))
c = random.random()
if c < 0.90:
self.mat[a][b] = 2
else:
self.mat[a][b] = 4
def add(self):
"""add the two cells horizontally toward the left side if the
two cells are identical"""
for r in range(len(self.mat)):
for c in range(len(self.mat)-1):
if self.mat[r][c] != 0 and self.mat[r][c] == self.mat[r][c+1]:
self.mat[r][c] = 2 * self.mat[r][c]
self.mat[r][c+1] = 0
def compress(self):
"eliminate zeros between cells which have values"
l = len(self.mat)
new_mat = self.matrix(l)
for r in range(l):
count = 0
for c in range(l):
if self.mat[r][c] != 0:
new_mat[r][count] = self.mat[r][c]
count += 1
return new_mat
def identify_offsets(self,row,column):
"""identify if there are identical cell in the surrounding of a given
cell"""
l = len(self.mat)
mat = self.mat[row][column]
offsets = [ (-1, 0),
(0,-1), (0, 1),
(1, 0) ]
for offset in offsets:
r = row + offset[0]
c = column + offset[1]
if (r >= 0 and r < l) and (c >= 0 and c < l):
if self.mat[r][c] == mat:
return True
return False
def game_state(self):
"""check all the cells and return 1 as win, -1 as lose, 0 as keep
going"""
l = len(self.mat)
for r in range(l):
for c in range(l):
if self.mat[r][c] == 2048:
return 1
count = 0
for r in range(l):
for c in range(l):
if self.identify_offsets(r, c) == True:
count += 1
if self.mat[r][c] == 0:
count += 1
if count == 0:
return -1
else:
return 0
def transpose(self):
"""transpose the matrix"""
l = len(self.mat)
newlist = []
for r in range(l):
alist = []
for c in range(l):
alist.append(self.mat[c][r])
newlist.append(alist)
return newlist
def reverse(self):
"""reverse the matrix which means flip the entries around"""
l = len(self.mat)
newlist = []
for r in range(l):
alist = []
for c in range(l):
alist.append(self.mat[r][-c - 1])
newlist.append(alist)
return newlist
def leftward(self):
"""update the matrix if the user's command is toward left"""
self.mat = self.compress()
self.add()
self.mat = self.compress()
def rightward(self):
"""update the matrix if the user's command is toward right"""
self.mat = self.reverse()
self.mat = self.compress()
self.add()
self.mat = self.compress()
self.mat = self.reverse()
def upward(self):
"""update the matrix if the user wants to move upward"""
self.mat = self.transpose()
self.mat = self.compress()
self.add()
self.mat = self.compress()
self.mat = self.transpose()
def downward(self):
"""update the matrix if the user wants to move downward"""
self.mat = self.transpose()
self.mat = self.reverse()
self.mat = self.compress()
self.add()
self.mat = self.compress()
self.mat = self.reverse()
self.mat = self.transpose()
def checker(self):
"Check if there are entries in all the cells"
l = len(self.mat)
count = 0
for r in range(l):
for c in range(l):
if self.mat[r][c] != 0:
count += 1
return count
def main():
m = Oper()
m.random_update()
k = interfaces.Graphics()
k.update_cells(m.mat)
k.startmainloop(m)
if __name__ == '__main__':
main()