-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_four.py
230 lines (205 loc) · 8.2 KB
/
connect_four.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import sys
from game_options import game_options
from minimax import MinimaxPlayer
def computer_game_starts():
depth = 5
game_over = False
turn = 0
player_one = 1
player_two = -1
board = Board()
computer = MinimaxPlayer(depth)
while not game_over:
if turn == 0:
while True:
print("Player 1's turn")
column = game_options()
if column in ("Q", "q"):
sys.exit()
elif column in ("R", "r"):
game_over = False
board = Board()
turn = 0
break
else:
column = int(column) - 1
if board.is_column_valid(column) and (0 <= column < 5):
board.update_board(column, player_one)
break
# player = False
else:
print("select a different column")
board.show_board()
if board.vertical_check(player_one) or board.horizontal_check(player_one) or \
board.diagonal_check_down(player_one) or board.diagonal_check_up(player=player_one):
print("Player 1 WON!!!")
game_finished()
if board.is_game_tie():
print("Game Draw")
game_finished()
else:
# player = True
column = computer.get_move(board, player_two)
board.update_board(column, player_two)
board.show_board()
if board.vertical_check(player_two) or board.horizontal_check(player_two) or \
board.diagonal_check_down(player_two) or board.diagonal_check_up(player=player_two):
print("Computer WON!!!")
game_finished()
if board.is_game_tie():
print("Game Draw")
game_finished()
turn += 1
turn = turn % 2
def human_game_starts():
game_over = False
turn = 0
player_one = 1
player_two = -1
board = Board()
while not game_over:
if turn == 0:
# player = True
while True:
print("Player 1's turn")
column = game_options()
if column in ("Q", "q"):
sys.exit()
elif column in ("R", "r"):
game_over = False
board = Board()
turn = 0
break
else:
column = int(column) - 1
# column = int(input("Player 1's turn: "))
if board.is_column_valid(column) and (0 <= column < 5):
board.update_board(column, player_one)
break
# player = False
else:
print("select a different column")
board.show_board()
if board.vertical_check(player_one) or board.horizontal_check(player_one) or \
board.diagonal_check_down(player_one) or board.diagonal_check_up(player=player_one):
print("Player 1 WON!!!")
game_finished()
if board.is_game_tie():
print("Game Draw")
game_finished()
else:
# player = True
while True:
print("Player 2's turn")
column = game_options()
if column in ("Q", "q"):
sys.exit()
elif column in ("R", "r"):
game_over = False
board = Board()
turn = 0
break
else:
column = int(column) - 1
# column = int(input("Player 2's turn: "))
if board.is_column_valid(column) and (0 <= column < 5):
board.update_board(column, player_two)
break
# player = False
else:
print("select a different column")
board.show_board()
if board.vertical_check(player_two) or board.horizontal_check(player_two) \
or board.diagonal_check_down(player_two) or board.diagonal_check_up(player_two):
print("Player 2 WON!!!")
game_finished()
if board.is_game_tie():
print("Game Draw")
game_finished()
turn += 1
turn = turn % 2
def game_finished():
print("Game finished\n\tWould you like to Restart or Quit the game\nEnter Q or R")
while True:
finished_input = input()
if finished_input in ("R", "r"):
start_the_game()
break
elif finished_input in ("q", "Q"):
sys.exit()
else:
print("Choose a valid option")
class Board:
def __init__(self):
self.my_board = [
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', '']
]
self.number_of_rows = len(self.my_board)
self.number_of_columns = len(self.my_board[0])
self.welcome_string = "Welcome to Connect Four Game!! Key Bindings are:\n\tQ = Quit \n\tR = Restart"
print(self.welcome_string)
def update_board(self, column, token):
for row in range(3, -1, -1):
if self.my_board[row][column] == '':
self.my_board[row][column] = token
break
def show_board(self):
for row in self.my_board:
print(row)
def is_column_valid(self, column):
return self.my_board[0][column] == ''
def vertical_check(self, player):
for i_column in range(self.number_of_columns):
for i_row in range(3, -1, -1):
if (self.my_board[i_row][i_column] == player) and (self.my_board[i_row - 1][i_column] == player) and \
self.my_board[i_row - 2][i_column] == player and self.my_board[i_row - 3][i_column] == player:
return True
def horizontal_check(self, player):
for i_row in range(self.number_of_rows):
for i_column in range(self.number_of_columns - 3):
if self.my_board[i_row][i_column] == player and self.my_board[i_row][i_column + 1] == player and \
self.my_board[i_row][i_column + 2] == player and self.my_board[i_row][i_column + 3] == player:
return True
def diagonal_check_down(self, player):
for i_column in range(self.number_of_columns - 3):
for i_row in range(self.number_of_rows - 3):
if self.my_board[i_row][i_column] == player and self.my_board[i_row + 1][i_column + 1] == player and \
self.my_board[i_row + 2][i_column + 2] == player and self.my_board[i_row + 3][
i_column + 3] == player:
return True
def diagonal_check_up(self, player):
for i_row in range(3, self.number_of_rows):
for i_column in range(self.number_of_columns - 3):
if self.my_board[i_row][i_column] == player and self.my_board[i_row - 1][i_column + 1] == player and \
self.my_board[i_row - 2][i_column + 2] == player and self.my_board[i_row - 3][
i_column + 3] == player:
return True
def is_game_tie(self):
for i_row in self.my_board:
if '' in i_row:
return False
return True
def valid_columns_list(self):
valid_columns = []
for i_column in range(self.number_of_columns):
if self.is_column_valid(i_column):
valid_columns.append(i_column)
return valid_columns
def opposite_token(self, token):
if token == 1:
return -1
else:
return 1
def start_the_game():
print("Choose between Computer or Human player")
user_choice = input("\tH for Human\n\tC for computer\n")
if user_choice == "H":
print("\n### Two player mode selected ###")
return human_game_starts()
else:
print("\n### Computer will be playing with you!! ###")
return computer_game_starts()
start_the_game()