-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToeAI.py
158 lines (138 loc) · 5.01 KB
/
TicTacToeAI.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
import time
import random
def makeBoard():
return [' ' for _ in range(9)]
def printBoard(board):
print("-------------------")
for i in range(3):
for row in [board[3 * i: 3 * i + 3]]:
print('| ' + ' | '.join(row) + ' |')
print("-------------------")
def insertInBoard(board, position, entry):
board[position] = entry
def undoInsert(board, position):
board[position] = ' '
def isBoardEmpty(board):
for entry in board:
if entry == ' ':
return False
return True
def validLocations(board):
valid = []
for i, entry in enumerate(board):
if entry == ' ':
valid.append(i)
return valid
def hasWon(board, player):
# horizontal
for i in range(3):
for row in [board[3 * i: 3 * i + 3]]:
if all([entry == player for entry in row]):
return True
# vertical
for i in range(3):
col = [board[i], board[i + 3], board[i + 6]]
if all([entry == player for entry in col]):
return True
# positive sloping diagonal
diagonalPos = [board[0], board[4], board[8]]
if all([entry == player for entry in diagonalPos]):
return True
# negatively sloping diagonal
diagonalNeg = [board[2], board[4], board[6]]
if all([entry == player for entry in diagonalNeg]):
return True
return False
def getPlayerMove(board, player):
playerMove = -1
while playerMove - 1 not in validLocations(board):
playerMove = int(input('Select your move [1 - 9]: '))
if playerMove - 1 in validLocations(board):
insertInBoard(board, playerMove - 1, player)
printBoard(board)
if hasWon(board, player):
print(player, " has won!")
exit()
return
else:
print('Invalid choice\n')
def getComputerMove(board, computer, computerAI):
print('Computer has played . . . ')
time.sleep(0.5)
computerMove = bestMove(board, computer) if computerAI else random.choice(validLocations(board))
insertInBoard(board, computerMove, computer)
printBoard(board)
if hasWon(board, computer):
print(computer, " has won!")
exit()
def bestMove(board, entry):
bestScore = float('-inf')
for move in validLocations(board):
insertInBoard(board, move, entry)
score = minimax(board, entry, float('-inf'), float('inf'), False)
undoInsert(board, move)
if score > bestScore:
bestScore = score
bestMove = move
return bestMove
def minimax(board, maximizingPlayer, alpha, beta, maximizing):
minimizingPlayer = 'O' if maximizingPlayer == 'X' else 'X'
# check if we have a win or draw
if hasWon(board, maximizingPlayer):
return +1 * (len(validLocations(board)) + 1)
elif hasWon(board, minimizingPlayer):
return -1 * (len(validLocations(board)) + 1)
elif len(validLocations(board)) == 0:
return 0
if maximizing:
value = float('-inf')
for move in validLocations(board):
insertInBoard(board, move, maximizingPlayer)
value = max(value, minimax(board, maximizingPlayer, alpha, beta, False))
undoInsert(board, move)
alpha = max(alpha, value)
if alpha >= beta:
break
return value
else:
value = float('inf')
for move in validLocations(board):
insertInBoard(board, move, minimizingPlayer)
value = min(value, minimax(board, maximizingPlayer, alpha, beta, True))
undoInsert(board, move)
beta = min(beta, value)
if beta <= alpha:
break
return value
def main():
board = makeBoard()
printBoard(board)
player = ''
while player != 'O' and player != 'X':
player = input('Select X or O: ').upper()
if player != 'O' and player != 'X':
print('Invalid choice\n')
computer = 'X' if player == 'O' else 'O'
computerAI = ''
while computerAI != 'y' and computerAI != 'n':
computerAI = input('Do you want to play the AI? [ y / n ]: ').lower()
if computerAI != 'y' and computerAI != 'n':
print('Invalid choice\n')
computerAI = True if computerAI == 'y' else False
firstMove = ''
while firstMove != 'y' and firstMove != 'n':
firstMove = input('Do you want to start first? [ y / n ]: ').lower()
if firstMove != 'y' and firstMove != 'n':
print('Invalid choice\n')
while not isBoardEmpty(board):
if firstMove == 'y':
printBoard(board)
getPlayerMove(board, player)
firstMove = ''
getComputerMove(board, computer, computerAI)
if isBoardEmpty(board):
break
getPlayerMove(board, player)
print("Game has ended in a draw")
if __name__ == '__main__':
main()