-
Notifications
You must be signed in to change notification settings - Fork 2
/
tic-tac-toe.py
140 lines (113 loc) · 3.54 KB
/
tic-tac-toe.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
import random, os
board = [['-' for _ in range(3)] for _ in range(3)]
scores = {
'X' : -1,
'O' : 1,
'tie' : 0
}
# --------------------------------------------- #
def whoHasWon():
for i in range(3):
if board[i][0] == board[i][1] == board[i][2]:
return str(board[i][0])
if board[0][i] == board[1][i] == board[2][i]:
return str(board[0][i])
if board[0][0] == board[1][1] == board[2][2] :
return str(board[0][0])
elif board[2][0] == board[1][1] == board[0][2] :
return str(board[2][0])
else:
for i in range(3):
for j in range(3):
if board[i][j] == '-' :
return '-'
return 'tie'
def checkIfGameOver():
# print board
os.system('cls')
for i in range(3):
for j in range(3):
print(board[i][j],end="\t")
print()
# check if game is over
gameover = 'tie'
for i in range(3):
if board[i][0] == board[i][1] == board[i][2]:
gameover = str(board[i][0])
if board[0][i] == board[1][i] == board[2][i]:
gameover = str(board[0][i])
if gameover == 'tie':
if board[0][0] == board[1][1] == board[2][2] :
gameover = str(board[0][0])
elif board[2][0] == board[1][1] == board[0][2] :
gameover = str(board[2][0])
else:
for i in range(3):
for j in range(3):
if board[i][j] == '-' :
gameover = '-'
if gameover == 'tie':
print("tie")
exit()
elif gameover == '-':
pass
else:
print(gameover+" wins!!")
exit()
# --------------------------------------------- #
# *********************************************** #
def placeAiMove():
bestScore = -99999
bestMove = [ 0,0 ]
for i in range(3):
for j in range(3):
# is spot available???
if board[i][j] == '-':
board[i][j] = 'O'
score = minimax(board, 0, False)
board[i][j] = '-'
if score > bestScore :
bestScore = score
bestMove[0] = i
bestMove[1] = j
return bestMove[0], bestMove[1]
def minimax(board, depth, isMax):
result = whoHasWon()
if result != '-':
return scores[str(result)]
if isMax:
bestScore = -99999
for i in range(3):
for j in range(3):
if board[i][j] == '-':
board[i][j] = 'O'
score = minimax(board, depth+1, False)
board[i][j] = '-'
bestScore = max( score, bestScore )
return bestScore
else:
bestScore = 99999
for i in range(3):
for j in range(3):
if board[i][j] == '-':
board[i][j] = 'X'
score = minimax(board, depth+1, True)
board[i][j] = '-'
bestScore = min( score, bestScore )
return bestScore
# *********************************************** #
# Loop for the turns
# --------------------------------------------- #
while True:
# ---------
checkIfGameOver()
# ---------
print("x, y : ",end="")
x, y = [int(x) for x in input().split()]
board[x-1][y-1] = 'X'
# ---------
checkIfGameOver()
# ---------
placex, placey = placeAiMove()
board[placex][placey] = 'O'
# --------------------------------------------- #