forked from mohi7solanki/2048-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
105 lines (87 loc) · 2.54 KB
/
Copy pathgame.py
File metadata and controls
105 lines (87 loc) · 2.54 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
import random
dir_code = {
'UP': (1,0),
'DOWN': (-1,0),
'LEFT': (0,1),
'RIGHT': (0,-1)
}
SCORE = 0
def merge(array, unused=None):
global SCORE
output = [i for i in array if i != 0]
for i in range(len(output)-1):
pair = output[i:i+2]
if pair[0] == pair[1]:
sum_of_pair = pair[0] + pair[1]
output[i] = sum_of_pair
SCORE += sum_of_pair
del output[i+1]
output.append(0)
diff = len(array) - len(output)
output = output + [0] * diff
return output
def iterate(start_point, direction, steps):
output = []
for step in range(steps):
row = start_point[0] + direction[0] * step
col = start_point[1] + direction[1] * step
output.append((row,col))
return output
class TwentyFortyEight:
def __init__(self, row, column):
self.row = row
self.column = column
self.reset()
self.first_dir = {}
self.first_dir['UP'] = [(0,i) for i in range(self.column)]
self.first_dir['DOWN'] = [(self.row-1,i) for i in range(self.column)]
self.first_dir['LEFT'] = [(i,0) for i in range(self.row)]
self.first_dir['RIGHT'] = [(i,self.column-1) for i in range(self.row)]
def __str__(self):
board = ''
for row in self.GRID:
board += str(row) + '\n'
return board
def reset(self):
self.GRID = [[0 for i in range(self.column)] for j in range(self.row)]
self.make_new_cell(initial=True)
def make_new_cell(self, initial=False):
tiles = [2] * 9 + [4]
if initial:
rows = random.sample(range(self.row), 2)
columns = random.sample(range(self.column), 2)
x1, x2 = rows
y1, y2 = columns
self.GRID[x1][y1] = random.choice(tiles)
self.GRID[x2][y2] = random.choice(tiles)
else:
while True:
row = random.randint(0,self.row-1)
column = random.randint(0,self.column-1)
if self.GRID[row][column] == 0:
self.GRID[row][column] = random.choice(tiles)
break
else:
continue
def move(self,direction):
initial_GRID = [row[:] for row in self.GRID]
indices = self.first_dir[direction]
for index in indices:
array_indices = iterate(index, dir_code[direction], self.row)
array_to_be_merged = []
for row, col in array_indices:
array_to_be_merged.append(self.GRID[row][col])
merged_array = merge(array_to_be_merged)
for index, i in zip(array_indices,range(len(merged_array))):
row, col = index
self.GRID[row][col] = merged_array[i]
if self.GRID != initial_GRID:
self.make_new_cell()
if __name__ == '__main__':
game = TwentyFortyEight(4,4)
while True:
print(f'SCORE: {SCORE}')
print(game)
move = input('Enter the move: ')
game.move(move)
print('')