-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
183 lines (177 loc) · 6.28 KB
/
engine.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
DOWN = 'DOWN'
UP = "UP"
LEFT = 'LEFT'
RIGHT = 'RIGHT'
STAY = 'stay'
class Board:
def __init__(self,size,cubes = 3, initial_board = None, debug = False, history = None):
if initial_board is None:
self.board = [[0]*x for x in [size]*size]
self.direction = {'LEFT': (0,-1),'RIGHT': (0,1), 'UP': (-1,0), 'DOWN': (1,0)}
self.size = size
self.lost = False
self.moves = 0
self.debug = debug
self.empty = [(x,y) for x in range(size) for y in range(size)]
for i in range (cubes):
self.insert_new()
elif isinstance(initial_board,list):
self.board = initial_board
self.direction = {'LEFT': (0, -1), 'RIGHT': (0, 1), 'UP': (-1, 0), 'DOWN': (1, 0)}
self.size = size
self.lost = False
self.moves = 0
self.debug = debug
self.empty = [(x,y) for x in range(size) for y in range(size) if initial_board[x][y] == 0]
else:
self.board = initial_board.board
self.direction = initial_board.direction
self.size = initial_board.size
self.lost = initial_board.lost
self.moves = initial_board.moves
self.debug = initial_board.debug
self.empty = initial_board.empty
if history != None:
self.insert_new = insert_new_by_history
self.history = history
else:
self.history = None
def insert_new_by_history(self,x,y,tile):
self.change_empty('remove',(x,y))
self.board[x][y] = tile
return (x,y,tile)
def get_score(self):
try:
return sum(sum(x) for x in self.board)
except:
print (self.board)
print (type(self.board))
raise GET_SCORE
def move_tile(self,tile,direction):
x,y = self.change_direction((tile[0],tile[1]))
orig_x,orig_y = (x,y)
if (x,y) in self.empty:
return
value = self.board[tile[0]][tile[1]]
a,b = self.direction[direction]
while ((x+a >= 0) and (y+b >= 0 ) and (x+a < self.size) and (y+b < self.size) and (self.board[x+a][y+b] == 0)):
x = x + a
y = y + b
self.board[orig_x][orig_y] = 0
self.change_empty(action = 'append',tile = tile)
self.board[x][y] = value
self.change_empty(action = 'remove',tile = (x,y))
self.bump((x,y),(x+a,y+b))
def bump(self,bumper,bumped):
try:
female = self.board[bumped[0]][bumped[1]]
male = self.board[bumper[0]][bumper[1]]
if male == female:
self.board[bumped[0]][bumped[1]] *=2
self.board[bumper[0]][bumper[1]] = 0
self.change_empty(action = 'append', tile = bumper)
except IndexError:
return
def change_empty(self, action, tile):
if action == 'pop':
return self.empty.pop(tile)
x,y = self.change_direction((tile[0],tile[1]))
if action == 'remove':
try:
self.empty.remove((x,y))
except:
print (self.empty)
print(x,y)
print(self.board[x][y])
raise RemoveError
elif action =='append':
self.empty.append((x,y))
else:
raise WrongAction
def insert_new(self):
import random
l = int(len(self.empty))
a,b = self.change_empty(action = 'pop', tile = int((random.random()*100)%l))
coin = int((random.random()*10)%2)
if coin:
self.board[a][b] = 2
return (a,b,2)
else:
self.board[a][b] = 4
return (a,b,4)
def move_board(self,direction):
from game import HISTORY
if direction == 'stay':
return False
prev_board = [row[:] for row in self.board]
s = self.size
if not self.history is None:
direction,next_insert = history[HISTORY[1]].pop(0)
if direction == DOWN:
for row in range(-2,-s-1,-1):
for col in range(0,s):
self.move_tile((row,col),DOWN)
elif direction == UP:
for row in range(1,s):
for col in range(s):
self.move_tile((row,col),UP)
elif direction == LEFT:
for col in range(1,s):
for row in range(s):
self.move_tile((row,col),LEFT)
elif direction == 'RIGHT':
for col in range(-2,-s-1,-1):
for row in range(s):
self.move_tile((row,col),RIGHT)
else:
raise WrongDirection
if (self.debug == False and self.board != prev_board):
if self.history is None:
self.last_added = self.insert_new()
else:
self.last_added = self.insert_new(next_insert[0],next_insert[1],next_insert[2])
self.moves += 1
if len(self.empty) == 0:
if self.is_lost():
self.lost = True
return True
elif (self.board == prev_board):
if self.is_lost():
self.lost = True
return False
def is_lost(self):
l = int(len(self.empty))
if l!= 0:
return False
else:
okay = False
for row in range(self.size - 1):
for col in range(self.size - 1 ):
if self.board[row][col] == self.board[row][col+1]:
okay = True
if self.board[row][col] == self.board[row+1][col]:
okay = True
if not okay:
return True
else:
return False
def get_max_score(self):
return max(max(x) for x in self.board)
def __str__(self):
s=''
for i in range(self.size):
for j in range(self.size):
s = s+(str(self.board[i][j]))
s = s+' '
s = s+ '\n'
return s
def change_direction(self,tile):
if tile[0] < 0 :
x = self.size + tile[0]
else:
x = tile[0]
if tile[1] < 0 :
y = self.size + tile[1]
else:
y = tile[1]
return (x,y)