-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudge.py
153 lines (137 loc) · 4.87 KB
/
judge.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
__author__ = 'RodXander'
from copy import deepcopy
from exceptions import *
class Judge:
def __init__(self, dimension):
self.board = []
for i in range(dimension):
self.board.append([0] * dimension)
self.board[0][0] = 2
self.score = 0
def __str__(self):
s = ''
for i in range(len(self.board)):
s += str(self.board[i]) + "\n"
return s
#permited values: n, e, s, w
def play(self, move):
self.score = 0
self.move(move)
self.locate_new_box(move)
def move(self, move):
if move == 'n' or move == 's':
for c in range(len(self.board)):
column = self.get_column(c)
column = self.remove_zeros(column)
column = self.sum_equals(column, move == 's', True)
self.fill_column(c, column, move == 's')
elif move == 'w' or move == 'e':
for r in range(len(self.board)):
row = self.get_row(r)
row = self.remove_zeros(row)
row = self.sum_equals(row, move == 'e', True)
self.fill_row(r, row, move == 'e')
else:
raise InvalidParam(move)
def locate_new_box(self, move):
if move == 'n':
for r in range(len(self.board) - 1, -1, -1):
for c in range(len(self.board) - 1, -1, -1):
if self.board[r][c] == 0:
self.board[r][c] = 2
return True
elif move == 'e':
for c in range(len(self.board)):
for r in range(len(self.board) - 1, -1, -1):
if self.board[r][c] == 0:
self.board[r][c] = 2
return True
elif move == 's':
for r in range(len(self.board)):
for c in range(len(self.board)):
if self.board[r][c] == 0:
self.board[r][c] = 2
return True
elif move == 'w':
for c in range(len(self.board) - 1, -1, -1):
for r in range(len(self.board)):
if self.board[r][c] == 0:
self.board[r][c] = 2
return True
else:
raise InvalidParam()
return False
def is_game_over(self):
for i in range(len(self.board)):
for j in range(len(self.board)):
if self.board[i][j] == 0:
return False
if i > 0 and self.board[i-1][j] == self.board[i][j]:
return False
if j > 0 and self.board[i][j-1] == self.board[i][j]:
return False
return True
def is_game_finished(self):
for i in range(len(self.board)):
for j in range(len(self.board)):
if self.board[i][j] == 2048:
return True
return False
def get_column(self, c):
column = []
for i in range(len(self.board)):
column.append(self.board[i][c])
return column
def get_row(self, r):
row = []
for i in range(len(self.board)):
row.append(self.board[r][i])
return row
def fill_column(self, column_index, column, reverse=False):
for row in range(len(self.board)):
self.board[row if not reverse else -1 - row][column_index] = \
column[row if not reverse else -1 - row] if row < len(column) else 0
def fill_row(self, row_index, row, reverse=False):
for column in range(len(self.board)):
self.board[row_index][column if not reverse else -1 - column] = \
row[column if not reverse else -1 - column] if column < len(row) else 0
def remove_zeros(self, l):
_l = []
for i in range(len(l)):
if l[i] > 0:
_l.append(l[i])
return _l
def sum_equals(self, l, reverse, score=False):
if not l:
return []
if reverse:
_l = [l[-1]]
i = -2
while i >= -len(l):
if l[i] == l[i+1]:
_l[-1] *= 2
if score:
self.score += _l[-1]
i -= 1
if i >= -len(l):
_l.append(l[i])
else:
_l.append(l[i])
i -= 1
_l.reverse()
return _l
else:
_l = [l[0]]
i = 1
while i < len(l):
if l[i] == l[i - 1]:
_l[-1] *= 2
if score:
self.score += _l[-1]
i += 1
if i < len(l):
_l.append(l[i])
else:
_l.append(l[i])
i += 1
return _l