-
Notifications
You must be signed in to change notification settings - Fork 0
/
logics.py
164 lines (135 loc) · 4.46 KB
/
logics.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
from random import randint
from constants import ROW_COUNT, COL_COUNT, CONST_VALUE_2, CONST_VALUE_2048
from constants import GAME_STATE_WON, GAME_STATE_LOST, GAME_STATE_NOTOVER
def start_game():
"""
Initialize the game with 4x4 matrix. Initially all values will be 0.
"""
matrix = [[0 for c in range(COL_COUNT)] for r in range(ROW_COUNT)]
return matrix
def add_new_2(mat):
"""
Find empty position in matrix and add value '2' at that position.
"""
row = randint(0, ROW_COUNT-1)
col = randint(0, COL_COUNT-1)
while (mat[row][col] != 0):
row = randint(0, ROW_COUNT-1)
col = randint(0, COL_COUNT-1)
mat[row][col] = CONST_VALUE_2
def compress(mat):
"""
Push all non zero values to left.
"""
grid_changed = False # Flag to track if grid is changed
new_mat = [[0 for c in range(COL_COUNT)] for r in range(ROW_COUNT)]
for r in range(ROW_COUNT):
pos = 0
for c in range(COL_COUNT):
if (mat[r][c] != 0):
new_mat[r][pos] = mat[r][c]
if (pos != c):
grid_changed = True
pos += 1
return new_mat, grid_changed
def merge(mat):
"""
Merge the adjacent cell's if they have same value
"""
grid_changed = False # Flag to track if grid is changed
for r in range(ROW_COUNT):
for c in range(COL_COUNT-1):
if (mat[r][c] == mat[r][c+1] and mat[r][c] != 0):
mat[r][c] *= 2
mat[r][c+1] = 0
grid_changed = True
return mat, grid_changed
def reverse(mat):
"""
Reverse a matrix
"""
for r in range(ROW_COUNT):
mat[r].reverse()
return mat
def transpose(mat):
"""
Transpose a matrix
"""
new_mat = [[0 for c in range(COL_COUNT)] for r in range(ROW_COUNT)]
for r in range(ROW_COUNT):
for c in range(COL_COUNT):
new_mat[r][c] = mat[c][r]
return new_mat
def move_left(grid):
"""
Perform left move operation on grid
"""
grid, grid_changed_compress = compress(grid)
grid, grid_changed_merge = merge(grid)
is_grid_changed = grid_changed_compress or grid_changed_merge
grid, temp = compress(grid)
return grid, is_grid_changed
def move_right(grid):
"""
Perform right move operation on grid
"""
grid = reverse(grid)
grid, grid_changed_compress = compress(grid)
grid, grid_changed_merge = merge(grid)
is_grid_changed = grid_changed_compress or grid_changed_merge
grid, temp = compress(grid)
grid = reverse(grid)
return grid, is_grid_changed
def move_up(grid):
"""
Perform up move operation on grid
"""
grid = transpose(grid)
grid, grid_changed_compress = compress(grid)
grid, grid_changed_merge = merge(grid)
is_grid_changed = grid_changed_compress or grid_changed_merge
grid, temp = compress(grid)
grid = transpose(grid)
return grid, is_grid_changed
def move_down(grid):
"""
Perform down move operation on grid
"""
grid = transpose(grid)
grid = reverse(grid)
grid, grid_changed_compress = compress(grid)
grid, grid_changed_merge = merge(grid)
is_grid_changed = grid_changed_compress or grid_changed_merge
grid, temp = compress(grid)
grid = reverse(grid)
grid = transpose(grid)
return grid, is_grid_changed
def get_current_state(mat):
"""
Returns the current state of game. Either WON or NOT_OVER or LOST
"""
# Check if at any position there is value 2048
for r in range(ROW_COUNT):
for c in range(COL_COUNT):
if (mat[r][c] == CONST_VALUE_2048):
return GAME_STATE_WON
# Check if there are any empty positions
for r in range(ROW_COUNT):
for c in range(COL_COUNT):
if (mat[r][c] == 0):
return GAME_STATE_NOTOVER
# Empty positions are not there, check if there is any movement possible.
# Excluding last row and col as it can lead to out of bound.
for r in range(ROW_COUNT-1):
for c in range(COL_COUNT-1):
if (mat[r][c] == mat[r+1][c] or mat[r][c] == mat[r][c+1]):
return GAME_STATE_NOTOVER
# Check for last row
for c in range(COL_COUNT-1):
if (mat[ROW_COUNT-1][c] == mat[ROW_COUNT-1][c+1]):
return GAME_STATE_NOTOVER
# Check for last col
for r in range(ROW_COUNT-1):
if (mat[r][COL_COUNT-1] == mat[r+1][COL_COUNT-1]):
return GAME_STATE_NOTOVER
return GAME_STATE_LOST