-
Notifications
You must be signed in to change notification settings - Fork 5
/
game_board.py
136 lines (108 loc) · 3.54 KB
/
game_board.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
import numpy as np
from numba import jit
dirs = [UP, DOWN, LEFT, RIGHT] = range(4)
@jit
def merge(a):
for i in [0,1,2,3]:
for j in [0,1,2]:
if a[i][j] == a[i][j + 1] and a[i][j] != 0:
a[i][j] *= 2
a[i][j + 1] = 0
return a
@jit
def justify_left(a, out):
for i in [0,1,2,3]:
c = 0
for j in [0,1,2,3]:
if a[i][j] != 0:
out[i][c] = a[i][j]
c += 1
return out
@jit
def get_available_from_zeros(a):
uc, dc, lc, rc = False, False, False, False
v_saw_0 = [False, False, False, False]
v_saw_1 = [False, False, False, False]
for i in [0,1,2,3]:
saw_0 = False
saw_1 = False
for j in [0,1,2,3]:
if a[i][j] == 0:
saw_0 = True
v_saw_0[j] = True
if saw_1:
rc = True
if v_saw_1[j]:
dc = True
if a[i][j] > 0:
saw_1 = True
v_saw_1[j] = True
if saw_0:
lc = True
if v_saw_0[j]:
uc = True
return [uc, dc, lc, rc]
class GameBoard:
def __init__(self):
self.grid = np.zeros((4, 4))#, dtype=np.int_)
def clone(self):
grid_copy = GameBoard()
grid_copy.grid = np.copy(self.grid)
return grid_copy
def insert_tile(self, pos, value):
self.grid[pos[0]][pos[1]] = value
def get_available_cells(self):
cells = []
for x in range(4):
for y in range(4):
if self.grid[x][y] == 0:
cells.append((x,y))
return cells
def get_max_tile(self):
return np.amax(self.grid)
def move(self, dir, get_avail_call = False):
if get_avail_call:
clone = self.clone()
z1 = np.zeros((4, 4))#, dtype=np.int_)
z2 = np.zeros((4, 4))#, dtype=np.int_)
if dir == UP:
self.grid = self.grid[:,::-1].T
self.grid = justify_left(self.grid, z1)
self.grid = merge(self.grid)
self.grid = justify_left(self.grid, z2)
self.grid = self.grid.T[:,::-1]
if dir == DOWN:
self.grid = self.grid.T[:,::-1]
self.grid = justify_left(self.grid, z1)
self.grid = merge(self.grid)
self.grid = justify_left(self.grid, z2)
self.grid = self.grid[:,::-1].T
if dir == LEFT:
self.grid = justify_left(self.grid, z1)
self.grid = merge(self.grid)
self.grid = justify_left(self.grid, z2)
if dir == RIGHT:
self.grid = self.grid[:,::-1]
self.grid = self.grid[::-1,:]
self.grid = justify_left(self.grid, z1)
self.grid = merge(self.grid)
self.grid = justify_left(self.grid, z2)
self.grid = self.grid[:,::-1]
self.grid = self.grid[::-1,:]
if get_avail_call:
return not (clone.grid == self.grid).all()
else:
return None
def get_available_moves(self, dirs = dirs):
available_moves = []
a1 = get_available_from_zeros(self.grid)
for x in dirs:
if not a1[x]:
board_clone = self.clone()
if board_clone.move(x, True):
available_moves.append(x)
else:
available_moves.append(x)
return available_moves
def get_cell_value(self, pos):
return self.grid[pos[0]][pos[1]]