-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe_AlphaBeta.py
More file actions
185 lines (157 loc) · 5.12 KB
/
TicTacToe_AlphaBeta.py
File metadata and controls
185 lines (157 loc) · 5.12 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
from random import choice
from math import inf
board = [[0,0,0],
[0,0,0],
[0,0,0]]
def GameBoard(board):
players = {1:'X', -1:'O', 0:' '}
for x in board:
for y in x:
ch = players[y]
print(f'|{ch}|', end='')
print('\n'+'------------')
def Clearboard(board):
for x, row in enumerate(board):
for y, col in enumerate(row):
board[x][y] = 0
def winningPlayer(board, player):
conditions = [[board[0][0], board[0][1], board[0][2]],
[board[1][0], board[1][1], board[1][2]],
[board[2][0], board[2][1], board[2][2]],
[board[0][0], board[1][1], board[2][2]],
[board[0][0], board[1][0], board[2][0]],
[board[0][2], board[1][2], board[2][2]],
[board[0][2], board[1][1], board[2][0]],
[board[0][1], board[1][1], board[2][1]]
]
if [player, player, player] in conditions:
return True
return False
def gameWon(board):
return winningPlayer(board,1) or winningPlayer(board, -1)
def printResult(board):
if winningPlayer(board, 1):
print('X has won the match! \n');
if winningPlayer(board, 2):
print('O has won the match! \n');
else:
print('Draw !!! Good Game!')
def blanks(board):
blank = []
for x, row in enumerate(board):
for y, col in enumerate(row):
if board[x][y] == 0:
blank.append([x,y])
return blank
def boardFull(board):
if len(blanks(board)) == 0:
return True
return False
def setMove(board, x, y, player):
board[x][y] = player
def playerMove(board):
e = True
moves = {1: [0, 0], 2: [0, 1], 3: [0, 2],
4: [1, 0], 5: [1, 1], 6: [1, 2],
7: [2, 0], 8: [2, 1], 9: [2, 2]}
while e:
try:
move = int(input('Enter a number between 1-9: '))
if move < 1 or move > 9:
print('Invalid Move! Try again!')
elif not (moves[move] in blanks(board)):
print('Invalid Move! Try again!')
else:
setMove(board, moves[move][0], moves[move][1], 1)
GameBoard(board)
e = False
except(KeyError, ValueError):
print('Enter a number:')
def getScore(board):
if winningPlayer(board, 1):
return 10
elif winningPlayer(board, -1):
return -10
else:
return 0
def abminimax(board, depth, alpha, beta, player):
row = -1
col = -1
if depth == 0 or gameWon(board):
return [row, col, getScore(board)]
else:
for cell in blanks(board):
setMove(board, cell[0], cell[1], player)
score = abminimax(board, depth - 1, alpha, beta, -player)
if player == 1:
# X is always the max player
if score[2] > alpha:
alpha = score[2]
row = cell[0]
col = cell[1]
else:
if score[2] < beta:
beta = score[2]
row = cell[0]
col = cell[1]
setMove(board, cell[0], cell[1], 0)
if alpha >= beta:
break
if player == 1:
return [row, col, alpha]
else:
return [row, col, beta]
def o_comp(board):
if len(blanks(board)) == 9:
x = choice([0, 1, 2])
y = choice([0, 1, 2])
setMove(board, x, y, -1)
GameBoard(board)
else:
result = abminimax(board, len(blanks(board)), -inf, inf, -1)
setMove(board, result[0], result[1], -1)
GameBoard(board)
def x_comp(board):
if len(blanks(board)) == 9:
x = choice([0, 1, 2])
y = choice([0, 1, 2])
setMove(board, x, y, 1)
GameBoard(board)
else:
result = abminimax(board, len(blanks(board)), -inf, inf, 1)
setMove(board, result[0], result[1], 1)
GameBoard(board)
def makeMove(board, player, mode):
if mode == 1:
if player == 1:
playerMove(board)
else:
o_comp(board)
else:
if player == 1:
o_comp(board)
else:
x_comp(board)
def pvc():
while True:
try:
order = int(input('Enter to play 1st or 2nd: '))
if not (order == 1 or order == 2):
print('Please pick 1 or 2')
else:
break
except(KeyError, ValueError):
print('Enter a number')
Clearboard(board)
if order == 2:
currentPlayer = -1
else:
currentPlayer = 1
while not (boardFull(board) or gameWon(board)):
makeMove(board, currentPlayer, 1)
currentPlayer *= -1
printResult(board)
print("=================================================")
print("TIC-TAC-TOE using MINIMAX with ALPHA-BETA Pruning")
print("=================================================")
pvc()