-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048_tamirat_final.py
331 lines (284 loc) · 11.4 KB
/
2048_tamirat_final.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# sup man
import random
# the module to give random number
import numpy as np
# the package to import the numpy and in order to transpose the matrix and other staff
import copy
# the module to copy the matrix different element of the list
import os
# is the module to clear the matrix after one command
import sys
# is the module to exit whenever and wherever needed
# This is the program to add random numbers in the first matrix
new_grid = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
# what jdsg
i = random.randint(0, 3)
j = random.randint(0, 3)
for i in range(2):
while (new_grid[i][j] != 0):
i = random.randint(0, 3)
j = random.randint(0, 3)
new_grid[i][j] = 2
new_grid = np.array(new_grid)
new_grid = copy.deepcopy(new_grid)
for i in range(15):
print('~', end=" ")
print()
for i in new_grid:
for j in i:
print("|", j, " ", "|", end="")
print("")
for i in range(15):
print('~', end=" ")
print()
# this is the function to tell weather you win or not. by checking weather consequative element are
# not the same both in row and column, there is no zero in the matrix
def winner_gameOver(new_grid):
gameOver = False
for i in range(4):
for j in range(4):
if new_grid[i][j] >= 2048:
print("congratulation you won !!")
sys.exit() #TO terminate the game after the game over
for i in range(4):
for j in range(3):
if new_grid[i][j] == new_grid[i][j + 1] and 0 and np.array(new_grid).transpose()[i][j] == np.array(new_grid).transpose()[i][j + 1] not in new_grid not in new_grid and not gameOver:
print("game over")
gameOver = True
sys.exit() #TO terminate the game after the game over
# this is the function to add random numbers after each update
def rand_setter(new_grid):
i = random.randint(0, 3)
j = random.randint(0, 3)
while (new_grid[i][j] != 0):
i = random.randint(0, 3)
j = random.randint(0, 3)
new_grid[i][j] = 2
return new_grid
# this the function to intiate the game in the first step and display the
# options to play the game like 'w' for up, 'a' for left,'s' for down and 'd' for right
def caller(new_grid):
command = {'a': 'left',
'd': 'right',
'w': 'up',
's': 'down'}
print(command)
player = input("enter your choice:")
choice = command.get(player, 'try again')
# these functions like move_left, move_right, move_up and move_down
# are for the game to move the numbers in different directions and the all return the updated matrix
if choice == 'left':
new_grid = move_left(new_grid)
return new_grid
elif choice == 'right':
new_grid = move_right(new_grid)
return new_grid
elif choice == 'up':
new_grid = move_up(new_grid)
return new_grid
elif choice == 'down':
new_grid = move_down(new_grid)
return new_grid
# this is the function to compress the matrix to right and merge it as
# there order.
def move_right(new_grid):
global j, i
# the matrix to compress the main matrix using other matrix
new_num = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(4):
rank = 0
for j in range(4):
if new_grid[i][3-j] != 0:
new_num[i][3-rank] = new_grid[i][3-j]
rank += 1
# this the program to add the compressed matrix
for i in range(4):
if new_num[i][3] == new_num[i][2] and new_num[i][1] == new_num[i][0] and new_num[i][3] != 0 and new_num[i][1] != 0:
new_num[i][3] = new_num[i][3] + new_num[i][2]
new_num[i][2] = new_num[i][1] + new_num[i][0]
new_num[i][1] = 0
new_num[i][0] = 0
if new_num[i][3] == new_num[i][2] and new_num[i][3] != 0:
new_num[i][3] = new_num[i][3] + new_num[i][2]
new_num[i][2] = new_num[i][1]
new_num[i][1] = new_num[i][0]
new_num[i][0] = 0
if new_num[i][2] == new_num[i][1] and new_num[i][2] != 0:
new_num[i][2] = new_num[i][2] + new_num[i][1]
new_num[i][1] = new_num[i][0]
new_num[i][0] = 0
if new_num[i][1] == new_num[i][0] and new_num[i][1] != 0:
new_num[i][1] = new_num[i][1] + new_num[i][0]
new_num[i][0] = 0
if new_num[i][3] != new_num[i][2] and new_num[i][2] != new_num[i][1] and new_num[i][1] != new_num[i][0]:
new_num[i][3] = new_num[i][3]
new_num[i][2] = new_num[i][2]
new_num[i][1] = new_num[i][1]
new_num[i][0] = new_num[i][0]
new_num = np.array(new_num)
new_grid = copy.deepcopy(new_num)
# call the function to add random number to the updated one
rand_setter(new_grid)
# it return the updated one to the caller function
return new_grid
# move to left
# this is the function to compress the matrix to left and merge it as
# there order.
def move_left(new_grid):
# the matrix to compress the main matrix using other matrix
new_num = [[0 , 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
# this the program to add the compressed matrix
for i in range(4):
rank = 0
for j in range(4):
if new_grid[i][j] != 0:
new_num[i][rank] = new_grid[i][j]
rank += 1
for i in range(4):
if new_num[i][0] == new_num[i][1] and new_num[i][2] == new_num[i][3] and new_num[i][0] != 0 and new_num[i][3] != 0:
new_num[i][0] = new_num[i][0] + new_num[i][1]
new_num[i][1] = new_num[i][2] + new_num[i][3]
new_num[i][2] = 0
new_num[i][3] = 0
if new_num[i][0] == new_num[i][1] and new_num[i][0] != 0:
new_num[i][0] = new_num[i][1] + new_num[i][0]
new_num[i][1] = new_num[i][2]
new_num[i][2] = new_num[i][3]
new_num[i][3] = 0
if new_num[i][1] == new_num[i][2] and new_num[i][1] != 0:
new_num[i][1] = new_num[i][1] + new_num[i][2]
new_num[i][2] = new_num[i][3]
new_num[i][3] = 0
if new_num[i][2] == new_num[i][3] and new_num[i][2] != 0:
new_num[i][2] = new_num[i][2] + new_num[i][3]
new_num[i][3] = 0
if new_num[i][0] != new_num[i][1] and new_num[i][1] != new_num[i][2] and new_num[i][2] != new_num[i][3]:
new_num[i][0] = new_num[i][0]
new_num[i][1] = new_num[i][1]
new_num[i][2] = new_num[i][2]
new_num[i][3] = new_num[i][3]
new_grid = np.array(new_num)
new_grid = copy.deepcopy(new_grid)
# call the function to add random number to the updated one
rand_setter(new_grid)
# it return the updated one to the caller function
return new_grid
# this is the function called move_up to compress the matrix to and merge it as
# there order.
def move_up(new_grid):
# I used numpy package to transpose it easily and compress it to left and
# merge the consquative member if the are equal and transpose it to the orginal matrix
new_grid = np.array(new_grid).transpose()
# the matrix to compress the main matrix using other matrix
new_num = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
# to compress the non zero numbers in the matrix
for i in range(4):
rank = 0
for j in range(4):
if new_grid[i][j] != 0:
new_num[i][rank] = new_grid[i][j]
rank += 1
new_num = np.array(new_num)
# merge the similar numbers
for i in range(4):
if new_num[i][0] == new_num[i][1] and new_num[i][2] == new_num[i][3] and new_num[i][0] != 0 and new_num[i][3] != 0:
new_num[i][0] = new_num[i][0] + new_num[i][1]
new_num[i][1] = new_num[i][2] + new_num[i][3]
new_num[i][2] = 0
new_num[i][3] = 0
if new_num[i][0] == new_num[i][1] and new_num[i][0] != 0:
new_num[i][0] = new_num[i][1] + new_num[i][0]
new_num[i][1] = new_num[i][2]
new_num[i][2] = new_num[i][3]
new_num[i][3] = 0
if new_num[i][1] == new_num[i][2] and new_num[i][1] != 0:
new_num[i][1] = new_num[i][1] + new_num[i][2]
new_num[i][2] = new_num[i][3]
new_num[i][3] = 0
if new_num[i][2] == new_num[i][3] and new_num[i][2] != 0:
new_num[i][2] = new_num[i][2] + new_num[i][3]
new_num[i][3] = 0
if new_num[i][0] != new_num[i][1] and new_num[i][1] != new_num[i][2] and new_num[i][2] != new_num[i][3]:
new_num[i][0] = new_num[i][0]
new_num[i][1] = new_num[i][1]
new_num[i][2] = new_num[i][2]
new_num[i][3] = new_num[i][3]
# to transpose the matrix to the original one
new_grid = np.array(new_num).transpose()
new_grid = copy.deepcopy(new_grid)
rand_setter(new_grid)
return new_grid
# this is the function called move_down to compress the matrix to and merge it as
# there order.
def move_down(new_grid):
# I used numpy package to transpose it easily and compress it to right and
# merge the consequative member if the are equal and transpose it to the orginal matrix
new_grid = np.array(new_grid).transpose()
# the matrix to compress the main matrix using other matrix
new_num = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
# to compress the non zero numbers in the matrix
for i in range(4):
rank = 0
for j in range(4):
if new_grid[i][3-j] != 0:
new_num[i][3-rank] = new_grid[i][3-j]
rank += 1
new_num = np.array(new_num)
# merge the similar numbers
for i in range(4):
if new_num[i][3] == new_num[i][2] and new_num[i][1] == new_num[i][0] and new_num[i][3] != 0 and new_num[i][1] != 0:
new_num[i][3] = new_num[i][3] + new_num[i][2]
new_num[i][2] = new_num[i][1] + new_num[i][0]
new_num[i][1] = 0
new_num[i][0] = 0
if new_num[i][3] == new_num[i][2] and new_num[i][3] != 0:
new_num[i][3] = new_num[i][3] + new_num[i][2]
new_num[i][2] = new_num[i][1]
new_num[i][1] = new_num[i][0]
new_num[i][0] = 0
if new_num[i][2] == new_num[i][1] and new_num[i][2] != 0:
new_num[i][2] = new_num[i][2] + new_num[i][1]
new_num[i][1] = new_num[i][0]
new_num[i][0] = 0
if new_num[i][1] == new_num[i][0] and new_num[i][1] != 0:
new_num[i][1] = new_num[i][1] + new_num[i][0]
new_num[i][0] = 0
if new_num[i][3] != new_num[i][2] and new_num[i][2] != new_num[i][1] and new_num[i][1] != new_num[i][0]:
new_num[i][3] = new_num[i][3]
new_num[i][2] = new_num[i][2]
new_num[i][1] = new_num[i][1]
new_num[i][0] = new_num[i][0]
# to transpose the matrix to the original one
new_grid = np.array(new_num).transpose()
new_grid = copy.deepcopy(new_grid)
rand_setter(new_grid)
return new_grid
while True:
new_grid = (caller(new_grid))
_ = os.system('cls')
for i in range(15):
print('_', end=" ")
print()
for i in new_grid:
for j in i:
print("|", j, " ", "|", end=" ")
print("")
for i in range(15):
print('~', end=" ")
print()
winner_gameOver(new_grid)