-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4.py
executable file
·265 lines (237 loc) · 8.56 KB
/
connect4.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python
import sys
import copy
import time
from getch import getch
from minimaxAIParallel import run_AI as startPar
from minimaxAISequential import run_AI as startSeq
from minimaxCommon import evaluateBoard
from randomAI import run_AI as startRandom
from gameover import gameOver
from random import randint
class Connect4(object):
HEIGHT = 6
WIDTH = 7
FIRST = True
#index by (row,col), (0,0) is top left
board = [[" " for x in range(WIDTH)] for y in range(HEIGHT)]
activePlayer = 0
def __init__(self):
pass
def start(self):
print "Welcome to Connect 4!"
self.activePlayer = randint(0,1)
gameType = self.get_game_type()
if gameType == "1":
self.start_human_human_game()
elif gameType == "2":
self.start_human_computer_game(False)
elif gameType == "3":
self.start_human_computer_game(True)
elif gameType == "4":
self.start_computer_computer_game()
else:
print "Unexpected game type, exiting.."
sys.exit()
def print_winner(self,p):
if p == 0:
print "PLAYER 1 (X) WON!\n"
else:
print "PLAYER 2 (O) WON!\n"
def get_game_type(self):
while(True):
print ("\nChoose your game type:\n1. Human vs Human\n2. Human vs Serial Computer" +
"\n3. Human vs Parallel Computer\n4. Computer vs Computer")
print ">>",
gameChoice = getch()
if gameChoice not in ["1", "2", "3", "4"]:
print "You must enter the number for the desired game type"
continue
else:
break
return gameChoice
def print_game_board(self):
print "\nCURRENT GAME BOARD:\n"
print " 0 1 2 3 4 5 6 "
for i,row in enumerate(self.board):
print " --- --- --- --- --- --- ---"
print "|",
for j,val in enumerate(row):
if j != len(row)-1:
if val != " ":
print val + " |",
else:
print val + " |",
else:
if val != " ":
print val + " |"
else:
print val + " |"
print " --- --- --- --- --- --- ---\n"
def make_move(self, col):
# first player is player 0
if self.activePlayer == 0:
valToWrite = "X"
else:
valToWrite = "O"
for i in range(self.HEIGHT):
if self.board[i][col] == " ":
if i == 5:
self.board[i][col] = valToWrite
return True
else:
if i == 0:
return False
else:
self.board[i-1][col] = valToWrite
return True
def start_human_human_game(self):
printBoard = True
tie = True
# game loop
while(True):
if printBoard:
self.print_game_board()
print "Enter column number to drop piece"
if self.activePlayer == 0:
print "Player 1 (X) >> ",
else:
print "Player 2 (O) >> ",
colChoice = getch()
if colChoice == "k":
sys.exit(0)
elif colChoice not in [str(x) for x in range(self.WIDTH)]:
print "You must enter a valid column"
printBoard = False
continue
else:
moveSuccess = self.make_move(int(colChoice))
if moveSuccess:
evaluateBoard(self.board, self.activePlayer)
if gameOver(self.board, self.activePlayer):
self.print_winner(self.activePlayer)
tie = False
break
self.activePlayer = (self.activePlayer + 1) % 2
printBoard = True
else:
print "Invalid move, column full"
printBoard = False
continue
#final game board
self.print_game_board()
if tie:
print "GAME WAS A TIE\n"
def start_human_computer_game(self, is_parallel):
printBoard = True
tie = True
# game loop
while(True):
if printBoard:
self.print_game_board()
count = 0
for y in range(self.WIDTH):
if self.board[0][y] == "X" or self.board[0][y] == "O":
count += 1
isFull = (count==self.WIDTH)
if isFull:
break
if self.activePlayer == 0:
print "Enter column number to drop piece"
print "Player 1 (X) >> "
colChoice = getch()
self.FIRST = False
else:
print "Player 2 (O) >> "
if self.FIRST:
colChoice = "3"
self.FIRST = False
else:
tempBoard = copy.deepcopy(self.board)
st = time.time()
if is_parallel:
colChoice = startPar(tempBoard, self.activePlayer)
else:
colChoice = startSeq(tempBoard, self.activePlayer)
et = time.time()
print "[INFO] Time taken: " + str(et - st)
print "[INFO] AI is playing column " + colChoice
if colChoice == "k":
sys.exit(0)
elif colChoice not in [str(x) for x in range(self.WIDTH)]:
print "You must enter a valid column"
printBoard = False
continue
else:
moveSuccess = self.make_move(int(colChoice))
if moveSuccess:
if gameOver(self.board, self.activePlayer):
self.print_winner(self.activePlayer)
tie = False
break
self.activePlayer = (self.activePlayer + 1) % 2
printBoard = True
else:
print "Invalid move, column full"
printBoard = False
continue
self.print_game_board()
if tie:
print "GAME WAS A TIE\n"
def start_computer_computer_game(self):
printBoard = True
tie = True
# game loop
while(True):
if printBoard:
self.print_game_board()
count = 0
for y in range(self.WIDTH):
if self.board[0][y] == "X" or self.board[0][y] == "O":
count += 1
isFull = (count==self.WIDTH)
if isFull:
break
tempBoard = copy.deepcopy(self.board)
if self.activePlayer == 0:
if self.FIRST:
colChoice = "3"
self.FIRST = False
else:
st = time.time()
colChoice = startPar(tempBoard, self.activePlayer)
et = time.time()
else:
if self.FIRST:
colChoice = "3"
self.FIRST = False
else:
st = time.time()
colChoice = startSeq(tempBoard, self.activePlayer)
et = time.time()
if colChoice == "k":
sys.exit(0)
elif colChoice not in [str(x) for x in range(self.WIDTH)]:
print "[ERROR] AI player #" + str(self.activePlayer) + " did not choose a valid column"
printBoard = False
continue
else:
moveSuccess = self.make_move(int(colChoice))
if moveSuccess:
if gameOver(self.board, self.activePlayer):
self.print_winner(self.activePlayer)
tie = False
break
self.activePlayer = (self.activePlayer + 1) % 2
printBoard = True
else:
print "[ERROR] AI player #" + str(self.activePlayer) + " did not make a valid move, column full"
printBoard = False
continue
#final game board
self.print_game_board()
if tie:
print "GAME WAS A TIE\n"
if __name__ == "__main__":
game = Connect4()
game.start()