-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
176 lines (142 loc) · 4.95 KB
/
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
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
import sys
class Board:
HUMAN = 2
CPU = 1
HEIGHT = 6
WIDTH = 7
board = []
def __init__(self):
for i in range(self.HEIGHT):
self.board.append([])
for j in range(self.WIDTH):
self.board[i].append(0)
def from_file(self, file_name):
with open(file_name, 'r') as file:
row = 0
for line in file.readlines():
line = line.rstrip()
self.board.append([])
if line == '':
break
for i, value in enumerate(line.split(' ')):
self.board[row][i] = int(value)
row += 1
def from_string(self, string):
cnt = 0
for i in range(self.HEIGHT):
for j in range(self.WIDTH):
self.board[i][j] = string[cnt]
cnt += 1
return self
def to_screen(self):
for i in range(self.HEIGHT):
for j in range(self.WIDTH):
sys.stdout.write(str(self.board[i][j]) + " ")
sys.stdout.write('\n')
sys.stdout.flush()
def move(self, column, player):
if not self.move_legal(column):
raise Exception()
current_row = self.HEIGHT - 1
for i in range(self.HEIGHT):
if self.board[i][column] != 0:
current_row = i - 1
break
self.board[current_row][column] = player
def undo_move(self, column):
for i in range(self.HEIGHT):
if self.board[i][column] != 0:
self.board[i][column] = 0
def find_winner(self, last_move_col):
last_move_row = 0
last_player = 0
for i in range(self.HEIGHT):
if self.board[i][last_move_col] != 0:
last_move_row = i
last_player = self.board[last_move_row][last_move_col]
break
winner = self.check_column(last_move_col, last_player)
if winner != 0:
return winner
winner = self.check_rows(last_move_row, last_player)
if winner != 0:
return winner
winner = self.check_right_diagonal(
last_move_row, last_move_col, last_player
)
if winner != 0:
return winner
winner = self.check_left_diagonal(
last_move_row, last_move_col, last_player
)
if winner != 0:
return winner
return 0
def check_left_diagonal(self, last_move_row, last_move_col, last_player):
start_row = last_move_row
start_col = last_move_col
counter = 0
for j in range(max(self.HEIGHT, self.WIDTH)):
if last_move_col - j < 0 or last_move_row - j < 0:
break
start_row = last_move_row - j
start_col = last_move_col - j
for j in range(max(self.HEIGHT, self.WIDTH)):
if start_col + j >= self.WIDTH or start_row + j >= self.HEIGHT:
break
if self.board[start_row + j][start_col + j] == last_player:
counter += 1
if counter == 4:
return last_player
else:
counter = 0
return 0
def check_right_diagonal(self, last_move_row, last_move_col, last_player):
start_row = last_move_row
start_col = last_move_col
counter = 0
for j in range(max(self.HEIGHT, self.WIDTH)):
if last_move_col + j >= self.WIDTH or last_move_row - j < 0:
break
start_row = last_move_row + j
start_col = last_move_col - j
for j in range(max(self.HEIGHT, self.WIDTH)):
if start_col - j < 0 or start_row + j >= self.HEIGHT:
break
if self.board[start_row + j][start_col - j] == last_player:
counter += 1
if counter == 4:
return last_player
else:
counter = 0
return 0
def check_rows(self, last_move_row, last_player):
counter = 0
for j in range(self.WIDTH):
if self.board[last_move_row][j] == last_player:
counter += 1
if counter == 4:
return last_player
else:
counter = 0
return 0
def check_column(self, last_move_col, last_player):
counter = 0
for j in range(self.HEIGHT):
if self.board[j][last_move_col] == last_player:
counter += 1
if counter == 4:
return last_player
else:
counter = 0
return 0
def move_legal(self, column):
if column > 7 or column < 1 or self.board[0][column] != 0:
return False
return True
def to_string(self):
data = ''
for i in range(self.HEIGHT):
for j in range(self.WIDTH):
data += str(self.board[i][j])
return data