-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe_reverse_terminal.py
173 lines (160 loc) · 5.18 KB
/
tictactoe_reverse_terminal.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
def displayBoard(board):
for column in range(3):
for row in range(3):
print(board[column][row], end='')
print('')
def makeMove(board, player, r, c):
board[r][c] = player
def switchPlayer(player):
if player == 'X':
return 'O'
else:
return 'X'
def eval(board):
if checkWin(board, 'X'):
return -1
elif checkWin(board, 'O'):
return 1
elif checkTie(board):
return 0
else:
return 10
def checkWin(board, player):
win = False
# check for horizontal win
for row in range(3):
count = 0
for column in range(3):
if board[row][column] != player:
break
else:
count += 1
if count == 3:
win = True
break
# check for vertical win
if not win:
for column in range(3):
count = 0
for row in range(3):
if board[row][column] != player:
break
else:
count += 1
if count == 3:
win = True
break
# check for diagonal win
if not win:
if board[0][0] == player and board[1][1] == player and board[2][2] == player:
win = True
elif board[2][0] == player and board[1][1] == player and board[0][2] == player:
win = True
else:
win = False
return win
def checkTie(board):
for i in range(3):
for j in range(3):
if board[i][j] == '-':
return False
return True
def bestMove(board, depth, player):
# maximizing player
if player == 'X':
bestScore = -float("inf")
# go through possible positions and find best score
for row in range(3):
for column in range(3):
if board[row][column] == '-':
board[row][column] = player
score = minimax(board, depth-1, switchPlayer(player))
board[row][column] = '-'
if score > bestScore:
bestScore = score
move = (row,column)
return move
# minimizing player
else:
bestScore = float("inf")
# go through possible positions and find best score
for row in range(3):
for column in range(3):
if board[row][column] == '-':
board[row][column] = player
score = minimax(board, depth-1, switchPlayer(player))
board[row][column] = '-'
if score < bestScore:
bestScore = score
move = (row,column)
return move
def minimax(board, depth, player):
tiePoints = 0
staticEval = eval(board)
# player has winning position
if depth == 0 or staticEval != 10:
return staticEval
# maximizing player
elif player == 'X':
bestScore = -float("inf")
# go through possible positions and find best score
for row in range(3):
for column in range(3):
if board[row][column] == '-':
board[row][column] = player
score = minimax(board, depth-1, switchPlayer(player))
board[row][column] = '-'
if score > bestScore:
bestScore = score
return bestScore
# minimizing player
else:
bestScore = float("inf")
# go through possible positions and find best score
for row in range(3):
for column in range(3):
if board[row][column] == '-':
board[row][column] = player
score = minimax(board, depth-1, switchPlayer(player))
board[row][column] = '-'
if score < bestScore:
bestScore = score
return bestScore
board = [['-' for j in range(3)] for i in range(3)]
human = ''
while human == '':
human = input("Which piece will human play? [X, O]")
if human != 'X' and human != 'O':
human = ''
ai = switchPlayer(human)
turn = 'X'
count = 0
run = True
while run:
print("%s's Turn" %turn)
displayBoard(board)
if turn == human:
notAvailable = True
while notAvailable:
move = [int(x) for x in input('Type coordinates of move separated by space: ').split()]
if board[move[0]][move[1]] == '-':
notAvailable = False
makeMove(board, human, move[0], move[1])
if checkWin(board, human):
displayBoard(board)
print("AI wins!")
run = False
break
else:
aimove = bestMove(board, 100,ai)
makeMove(board, ai, aimove[0], aimove[1])
if checkWin(board, ai):
displayBoard(board)
print("Human wins!")
run = False
break
if checkTie(board):
print("Tie")
run = False
turn = switchPlayer(turn)
count += 1