forked from weboski/connect_five
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnectFive.py
More file actions
131 lines (112 loc) · 4.26 KB
/
ConnectFive.py
File metadata and controls
131 lines (112 loc) · 4.26 KB
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
"""
This is a ConnectFiveGame class, which represents the model in the Connect 5
"Gomoku" game
Author: Phan Trung Kien, Jacob
"""
from ConnectFiveBoard import ConnectFiveBoard
class ConnectFiveGame:
board: ConnectFiveBoard
P1 = 'X'
P2 = 'O'
def __init__(self, board: ConnectFiveBoard) -> None:
"""
initialize a new ConnectFiveGame object
== Precondition ==
"""
self.board = board
def other_player(self, player):
"""
Returns the opponent of player
"""
if player == self.P1:
return self.P2
elif player == self.P2:
return self.P1
return None
def uniform_chips(self, row: int, col: int, drow: int, dcol: int):
"""
Return the player that has 5 in a row starting at position (row, col)
in direction (drow, dcol)
If neither player has 5 in a row then return None
:param row: the row of the current position
:param col: the col of the current position
:param drow: the vertical direction
:param dcol: the horizontal direction
"""
if self.board.get_chip(row, col) != " " and self.board.valid(row, col):
# initialize the current chip we are comparing
crnt_row = row
crnt_col = col
current_chip = self.board.get_chip(crnt_row, crnt_col)
count = 1
# continue until edge of board is hit
while self.board.valid(crnt_row + drow, crnt_col + dcol):
# get next chip in drow, dcol direction that we want to compare
next_chip = self.board.get_chip(crnt_row + drow,
crnt_col + dcol)
# update counter if next chip is the same as the current chip
# and update the current chip we are comparing.
if next_chip == current_chip:
count += 1
crnt_row = crnt_row + drow
crnt_col = crnt_col + dcol
current_chip = self.board.get_chip(crnt_row, crnt_col)
# chip is not the same, check if unbroken line of 5 of one chip
else:
if count == 5:
return current_chip
else:
return None
# edge has been reached, check unbroken line of 5 of one chip
if count == 5:
return current_chip
return None
def has_move(self):
"""
Checks if player has a valid move on the board
"""
for row in range(self.board.get_dimension()):
for col in range(self.board.get_dimension()):
# player can move if there is an empty space
if self.board.get_chip(row, col) == " ":
return True
return False
def get_count(self, player):
"""
Return how many chips of type player are on the board
:param player:
:return:
"""
count = 0
for i in range(len(self.row)):
for j in range(len(self.col)):
if self.board[i][j] == player:
count += 1
return count
def check_winner(self):
"""
Check if there is a winner (i.e If a player has 5 chips in a row on the board)
"""
for row in range(self.board.get_dimension()):
for col in range(self.board.get_dimension()):
for drow in range(-1, 2):
for dcol in range(-1, 2):
if drow != 0 or dcol != 0:
winner = self.uniform_chips(row, col, drow, dcol)
if winner is not None:
return winner
return None
def is_game_over(self):
"""
Return True if no player has a move or one of the
players has 5 of their chips in an unbroken line on the board
"""
winner = self.check_winner()
if self.has_move() is False or winner is not None:
return True
return False
def move(self, row, col, player):
"""
Place player chip at (row, col) on the board
"""
self.board.place_token(player, row, col)