-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
357 lines (299 loc) · 13.2 KB
/
main.py
File metadata and controls
357 lines (299 loc) · 13.2 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from random import randint
from lib import Constants
from player import Player, State
class Pit:
def __init__(self, numStones, is_player1=True, is_pocket=False):
self.numStones = numStones
self.is_player1 = is_player1
self.is_pocket = is_pocket
def setNumStones(self, newNumStones):
self.numStones = newNumStones
def setToPlayer2(self):
self.is_player1 = False
def setToPlayer1(self):
self.is_player1 = True
def setIsPocket(self, newBool):
self.is_pocket = newBool
def putInAStone(self):
self.numStones = self.numStones + 1
def getNumStones(self):
return self.numStones
def getIsPlayer1(self):
return self.is_player1
def getIsPocket(self):
return self.is_pocket
def initGame(board, p1, p2, num_stones):
# reset
#board = []
for i in range(Constants.NUM_PITS):
board.append(Pit(num_stones))
# by default, all pits belong to player 1
p1.setPits(Constants.P1_PITS)
p1.setPockets(Constants.P1_POCKETS)
p2.setOppoPockets(Constants.P1_POCKETS)
p2.setOppoPits(Constants.P1_PITS)
for i in Constants.P1_POCKETS:
board[i].setNumStones(0)
board[i].setIsPocket(True)
p2.setPits(Constants.P2_PITS)
p2.setPockets(Constants.P2_POCKETS)
p1.setOppoPockets(Constants.P2_POCKETS)
p1.setOppoPits(Constants.P2_PITS)
for i in Constants.P2_POCKETS:
board[i].setToPlayer2()
board[i].setNumStones(0)
board[i].setIsPocket(True)
def printBoard(board):
print(" \033[1;34;17m| " +
str(board[Constants.P2_POCKETS[0]].getNumStones()) + " |")
print(" | " + str(board[Constants.P2_PITS[0]].getNumStones()) +
" | " + str(board[Constants.P2_PITS[1]].getNumStones()) +
" |")
print(" | " + str(board[Constants.P2_PITS[2]].getNumStones()) +
" | " + str(board[Constants.P2_PITS[3]].getNumStones()) +
" |")
print("\033[1;31;17m| | " + str(board[Constants.P1_PITS[0]].getNumStones()) +
" | " + str(board[Constants.P1_PITS[1]].getNumStones()) +
" | " + str(board[Constants.P1_PITS[2]].getNumStones()) +
" |\033[1;34;17m " + str(board[Constants.P2_PITS[4]].getNumStones()) +
" \033[1;31;17m| " + str(board[Constants.P1_PITS[3]].getNumStones()) +
" | " + str(board[Constants.P1_PITS[4]].getNumStones()) +
" | |")
print("\033[1;31;17m| " + str(board[Constants.P1_POCKETS[0]].getNumStones()) + " |"
" " +
"| " + str(board[Constants.P1_POCKETS[1]].getNumStones()) + " |")
print("| | " + str(board[Constants.P1_PITS[5]].getNumStones()) +
" | " + str(board[Constants.P1_PITS[6]].getNumStones()) +
" |\033[1;34;17m " + str(board[Constants.P2_PITS[5]].getNumStones()) +
"\033[1;31;17m | " + str(board[Constants.P1_PITS[7]].getNumStones()) +
" | " + str(board[Constants.P1_PITS[8]].getNumStones()) +
" | " + str(board[Constants.P1_PITS[9]].getNumStones()) +
" | |")
print("\033[1;34;17m | " + str(board[Constants.P2_PITS[6]].getNumStones()) +
" | " + str(board[Constants.P2_PITS[7]].getNumStones()) +
" |")
print(" | " + str(board[Constants.P2_PITS[8]].getNumStones()) +
" | " + str(board[Constants.P2_PITS[9]].getNumStones()) +
" |")
print(" | " +
str(board[Constants.P2_POCKETS[1]].getNumStones()) + " |\033[0m")
def updateBoard(board, move):
for i in range(len(board)):
board[i].setNumStones(move.getBoard()[i])
def isOutOfMoves(board, pits):
for pit in pits:
if not board[pit].getNumStones() == 0:
return False
return True
def startGameCvC():
board = []
player_1 = Player(1)
player_2 = Player(2)
p1_nodes = 0
p2_nodes = 0
print("\033[1;31;17mPlayer 1\033[0m uses the Mini-Max Algorithm.")
print("\033[1;34;17mPlayer 2\033[0m uses the Alpha-Beta Algorithm.")
#num_stones = randint(1, Constants.MAX_STONES)
num_stones = Constants.NUM_STONES
initGame(board, player_1, player_2, num_stones)
printBoard(board)
rand = randint(0, 1)
if rand == 0:
print("\033[1;31;17mPlayer 1\033[0m makes the first move.")
else:
print("\033[1;34;17mPlayer 2\033[0m makes the first move.")
while rand == 0:
if isOutOfMoves(board, Constants.P1_PITS):
print("\033[1;31;17mPlayer 1\033[0m is out of moves, skip to player 2:")
else:
move, node_count = player_1.minimax_decision(board)
#move, node_count = player_1.alpha_beta_search(board)
p1_nodes = p1_nodes + node_count
print("\033[1;31;17mPlayer 1\033[0m chose to move the stones in pit #" +
str(move.getPitIndex()) + ":")
updateBoard(board, move)
printBoard(board)
if isOutOfMoves(board, Constants.P1_PITS) and isOutOfMoves(board, Constants.P2_PITS):
break
if isOutOfMoves(board, Constants.P2_PITS):
print(
"\033[1;34;17mPlayer 2\033[0m is out of moves, skip to player 1:")
else:
#move, node_count = player_2.minimax_decision(board)
move, node_count = player_2.alpha_beta_search(board)
p2_nodes = p2_nodes + node_count
print("\033[1;34;17mPlayer 2\033[0m chose to move the stones in pit #" +
str(move.getPitIndex()) + ":")
updateBoard(board, move)
printBoard(board)
if isOutOfMoves(board, Constants.P1_PITS) and isOutOfMoves(board, Constants.P2_PITS):
break
while rand == 1:
if isOutOfMoves(board, Constants.P2_PITS):
print("\033[1;34;17mPlayer 2\033[0m is out of moves, skip to player 1:")
else:
#move, node_count = player_2.minimax_decision(board)
move, node_count = player_2.alpha_beta_search(board)
p2_nodes = p2_nodes + node_count
print("\033[1;34;17mPlayer 2\033[0m chose to move the stones in pit #" +
str(move.getPitIndex()) + ":")
updateBoard(board, move)
printBoard(board)
if isOutOfMoves(board, Constants.P1_PITS) and isOutOfMoves(board, Constants.P2_PITS):
break
if isOutOfMoves(board, Constants.P1_PITS):
print(
"\033[1;31;17mPlayer 1\033[0m is out of moves, skip to player 2:")
else:
move, node_count = player_1.minimax_decision(board)
#move, node_count = player_1.alpha_beta_search(board)
p1_nodes = p1_nodes + node_count
print("\033[1;31;17mPlayer 1\033[0m chose to move the stones in pit #" +
str(move.getPitIndex()) + ":")
updateBoard(board, move)
printBoard(board)
if isOutOfMoves(board, Constants.P1_PITS) and isOutOfMoves(board, Constants.P2_PITS):
break
p1_score = board[Constants.P1_POCKETS[0]].getNumStones() + board[Constants.P1_POCKETS[1]].getNumStones()
p2_score = board[Constants.P2_POCKETS[0]].getNumStones() + board[Constants.P2_POCKETS[1]].getNumStones()
if p1_score > p2_score:
print("\033[1;31;17mPlayer 1\033[0m Won!")
elif p2_score > p1_score:
print("\033[1;34;17mPlayer 2\033[0m Won!")
else:
print("It's a tie!")
print("\033[1;31;17mPlayer 1\033[0m searched " + str(p1_nodes) + " nodes.")
print("\033[1;34;17mPlayer 2\033[0m searched " + str(p2_nodes) + " nodes.")
def isValidPit(board, pit_index, pit_list):
if pit_index in pit_list:
if not board[pit_index].getNumStones() == 0:
return True
return False
def startGamePvC():
board = []
player_1 = Player(1)
player_2 = Player(2)
p2_nodes = 0
alg_choice = -1
valid_choice = False
while not valid_choice:
print("Please choose an algorithm that the computer should use:")
print("1 - Mini-Max")
print("2 - Alpha-Beta")
alg_choice = int(input())
if alg_choice == 1:
print("\033[1;31;17mThe Computer\033[0m uses the Mini-Max Algorithm.")
valid_choice = True
elif alg_choice == 2:
print("\033[1;34;17mThe Computer\033[0m uses the Alpha-Beta Algorithm.")
valid_choice = True
else:
print("Invalid choice. Please type 1 or 2.")
valid_choice = False
#num_stones = randint(1, Constants.MAX_STONES)
num_stones = Constants.NUM_STONES
initGame(board, player_1, player_2, num_stones)
printBoard(board)
rand = randint(0, 1)
if rand == 0:
print("\033[1;31;17mThe player\033[0m makes the first move.")
else:
print("\033[1;34;17mThe Computer\033[0m makes the first move.")
while rand == 0:
if isOutOfMoves(board, Constants.P1_PITS):
print(
"\033[1;31;17mThe player\033[0m is out of moves, skip to the Computer:")
else:
pit_choice = -1
pit_valid = False
while not pit_valid:
print("\033[1;31;17mThe player\033[0m's turn. Please choose a pit.")
pit_choice = int(input())
if isValidPit(board, pit_choice, Constants.P1_PITS):
pit_valid = True
else:
print("Invalid choice, please try again.")
print("\033[1;31;17mThe player\033[0m chose to move the stones in pit #" +
str(pit_choice) + ":")
move = State(None, 0, pit_choice, board[pit_choice].getNumStones(), None)
tempBoard = []
for i in range(len(board)):
tempBoard.append(board[i].getNumStones())
newBoard = player_1.updateBoard(move, tempBoard)
move.setBoard(newBoard)
updateBoard(board, move)
printBoard(board)
if isOutOfMoves(board, Constants.P1_PITS) and isOutOfMoves(board, Constants.P2_PITS):
break
if isOutOfMoves(board, Constants.P2_PITS):
print(
"\033[1;34;17mThe Computer\033[0m is out of moves, skip to the player:")
else:
print("\033[1;34;17mThe Computer\033[0m's turn.")
if alg_choice == 1:
move, node_count = player_2.minimax_decision(board)
elif alg_choice == 2:
move, node_count = player_2.alpha_beta_search(board)
p2_nodes = p2_nodes + node_count
print("\033[1;34;17mThe Computer\033[0m chose to move the stones in pit #" +
str(move.getPitIndex()) + ":")
updateBoard(board, move)
printBoard(board)
if isOutOfMoves(board, Constants.P1_PITS) and isOutOfMoves(board, Constants.P2_PITS):
break
while rand == 1:
if isOutOfMoves(board, Constants.P2_PITS):
print(
"\033[1;34;17mThe Computer\033[0m is out of moves, skip to the player:")
else:
print("\033[1;34;17mThe Computer\033[0m's turn.")
if alg_choice == 1:
move, node_count = player_2.minimax_decision(board)
elif alg_choice == 2:
move, node_count = player_2.alpha_beta_search(board)
p2_nodes = p2_nodes + node_count
print("\033[1;34;17mThe Computer\033[0m chose to move the stones in pit #" +
str(move.getPitIndex()) + ":")
updateBoard(board, move)
printBoard(board)
if isOutOfMoves(board, Constants.P1_PITS) and isOutOfMoves(board, Constants.P2_PITS):
break
if isOutOfMoves(board, Constants.P1_PITS):
print(
"\033[1;31;17mThe player\033[0m is out of moves, skip to the Computer:")
else:
pit_choice = -1
pit_valid = False
while not pit_valid:
print(
"\033[1;31;17mThe player\033[0m's turn. Please choose a pit.")
pit_choice = int(input())
if isValidPit(board, pit_choice, Constants.P1_PITS):
pit_valid = True
else:
print("Invalid choice, please try again.")
print("\033[1;31;17mThe player\033[0m chose to move the stones in pit #" +
str(pit_choice) + ":")
move = State(None, 0, pit_choice,
board[pit_choice].getNumStones(), None)
tempBoard = []
for i in range(len(board)):
tempBoard.append(board[i].getNumStones())
newBoard = player_1.updateBoard(move, tempBoard)
move.setBoard(newBoard)
updateBoard(board, move)
printBoard(board)
if isOutOfMoves(board, Constants.P1_PITS) and isOutOfMoves(board, Constants.P2_PITS):
break
p1_score = board[Constants.P1_POCKETS[0]].getNumStones(
) + board[Constants.P1_POCKETS[1]].getNumStones()
p2_score = board[Constants.P2_POCKETS[0]].getNumStones(
) + board[Constants.P2_POCKETS[1]].getNumStones()
if p1_score > p2_score:
print("\033[1;31;17mThe Player\033[0m Won!")
elif p2_score > p1_score:
print("\033[1;34;17mThe Computer\033[0m Won!")
else:
print("It's a tie!")
startGameCvC()
#startGamePvC()