-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.py
176 lines (137 loc) · 4.83 KB
/
logic.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
# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# %%
# libraries and modules required
import random
# %%
# function for : when game is started
def startGame():
matrix= [[0 for i in range(4)] for i in range(4)]
return matrix
# %%
# function for : adding new "2" in the matrix at empty position
def addNew2(matrix):
row,colunm=random.randint(0,3),random.randint(0,3)
while matrix[row][colunm] != 0:
row,colunm=random.randint(0,3),random.randint(0,3)
matrix[row][colunm]=2
# %%
# function for : current status of matrix
def currStatus(matrix):
# if any cell is having "2048",that mean game is over and "player won"
for i in range(4):
for j in range(4):
if matrix[i][j]==2048:
return "Player won"
# ---------------------------------------------------------------------------------------------------------------------------------------------
# if any cell in the matrix is empty(ie, 0) , that mean "Game is not over yet"
for i in range(4):
for j in range(4):
if matrix[i][j]==0:
return "Game is not over"
# ---------------------------------------------------------------------------------------------------------------------------------------------
# if all cells in matrix is filled
for i in range(3):
for j in range(3):
if (matrix[i][j]==matrix[i][j+1]) or (matrix[i][j]==matrix[i+1][j]):
return "Game is not over"
for i in range(3):
if matrix[3][i]==matrix[3][i+1]:
return "Game is not over"
for i in range(3):
if matrix[i][3]==matrix[i+1][3]:
return "Game is not over"
# ---------------------------------------------------------------------------------------------------------------------------------------------
# If none of above case occure
return "Player lost"
# %%
# function for : compression of matrix
def compress(matrix):
changed=False
newCompressedMatrix= [[0 for i in range(4)] for j in range(4)]
for i in range(4):
curr=0
for j in range(4):
if matrix[i][j]!=0:
newCompressedMatrix[i][curr]=matrix[i][j]
if curr!=j:
changed=True
curr+=1
return newCompressedMatrix,changed
# %%
# function for : merging cells which are equal and adjecent (and are not equal to "0")
def merge(matrix):
changed=False
for i in range(4):
for j in range(3):
if matrix[i][j]!=0 and (matrix[i][j]==matrix[i][j+1]):
matrix[i][j]=(2*matrix[i][j])
changed=True
matrix[i][j+1]=0
return matrix,changed
# %%
# function for : reversing rows of matrix
def reverse(matrix):
newReversedMatrix=[]
for i in range(4):
newReversedMatrix.append(matrix[i][::-1])
return newReversedMatrix
# %%
# function for : transposing matrix
def transpose(matrix):
newTransposedMatrix=[[0 for i in range(4)] for j in range(4)]
for i in range(4):
for j in range(4):
newTransposedMatrix[j][i]=matrix[i][j]
return newTransposedMatrix
# %% [markdown]
# ALL POSSIBLE STEPS AND THEIR STEPS TO PERFORM
#
# # LEFT
# ### compress -> merge -> compress -> addNew2
# # RIGHT
# ### reverse -> compress -> merge -> compress -> reverse -> addNew2
# # UP
# ### transpose -> compress -> merge -> compress -> transpose -> addNew2
# # DOWN
# ### transpose -> reverse -> compress -> merge -> compress -> reverse -> transpose -> addNew2
# %%
# code for LEFT move
def leftMove(matrix):
newMatrix,change1= compress(matrix)
newMatrix2,change2=merge(newMatrix)
newMatrix3,change3= compress(newMatrix2)
# addNew2(newMatrix2)
finalChange= (change1 or change2 or change3)
return newMatrix3,finalChange
# %%
# code for RIGHT move
def rightMove(matrix):
newMatrix= reverse(matrix)
newMatrix2,change1= compress(newMatrix)
newMatrix3,change2=merge(newMatrix2)
newMatrix4,change3= compress(newMatrix3)
newMatrix5= reverse(newMatrix4)
finalChange= (change1 or change2 or change3)
# addNew2(newMatrix4)
return newMatrix5,finalChange
# %%
def upMove(matrix):
newMatrix= transpose(matrix)
newMatrix2,change1= compress(newMatrix)
newMatrix3,change2=merge(newMatrix2)
newMatrix4,change3= compress(newMatrix3)
newMatrix5= transpose(newMatrix4)
finalChange= (change1 or change2 or change3)
return newMatrix5,finalChange
# %%
def downMove(matrix):
newMatrix= transpose(matrix)
newMatrix2= reverse(newMatrix)
newMatrix3,change1= compress(newMatrix2)
newMatrix4,change2= merge(newMatrix3)
newMatrix5,change3= compress(newMatrix4)
newMatrix6= reverse(newMatrix5)
newMatrix7= transpose(newMatrix6)
finalChange= (change1 or change2 or change3)
return newMatrix7,finalChange