-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtictactoe.py
205 lines (167 loc) · 5.35 KB
/
tictactoe.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
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
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 11 21:58:38 2020
@author: Dogukan Dogru
"""
def printTable(table):
print()
print(" 1 2 3")
for i in range(3):
print(str(i+1) + " ", end=" "),
for k in range(3):
print(table[i][k], end=" "),
print("")
print()
def isMoveValid(table, move1, move2):
if table[move1][move2] == "-":
return True
else:
return False
def playX(table, move1, move2):
if isMoveValid(table, move1, move2) == True:
table[move1][move2] = "X"
return True
else:
return False
def playO(table, move1, move2):
if isMoveValid(table, move1, move2) == True:
table[move1][move2] = "O"
return True
else:
return False
def playBestMove(table):
bestScore = -99
move1 = -1
move2 = -1
for i in range(0, 3):
for j in range(0, 3):
# is the spot available?
if str(table[i][j]) == "-":
table[i][j] = "X"
score = minimax(table, 0, False)
# print(str(i+1) + " " + str(j+1) + " " + str(score))#positions and their points
table[i][j] = "-"
if score > bestScore:
bestScore = score
move1 = i
move2 = j
playX(table, move1, move2)
def minimax(table, depth, isMaximizing):
if checkTable(table) != "":
if "X" in str(checkTable(table)):
return 1
elif "O" in str(checkTable(table)):
return -1
else:
return 0
if isMaximizing == True:
bestScore = -99
for i in range(0, 3):
for j in range(0, 3):
# is the spot available ?
if str(table[i][j]) == "-":
table[i][j] = "X"
score = minimax(table, depth+1, False)
table[i][j] = "-"
bestScore = max(score, bestScore)
return bestScore
else:
bestScore = 99
for i in range(0, 3):
for j in range(0, 3):
# is the spot available ?
if str(table[i][j]) == "-":
table[i][j] = "O"
score = minimax(table, depth+1, True)
table[i][j] = "-"
bestScore = min(score, bestScore)
return bestScore
def checkRows(table):
constX = "['X', 'X', 'X']"
constO = "['O', 'O', 'O']"
if str(table[0]) == constX or str(table[1]) == constX or str(table[2]) == constX:
return "X"
elif str(table[0]) == constO or str(table[1]) == constO or str(table[2]) == constO:
return "O"
def checkColumns(table):
column1 = table[0][0] + table[1][0] + table[2][0]
column2 = table[0][1] + table[1][1] + table[2][1]
column3 = table[0][2] + table[1][2] + table[2][2]
constX = "XXX"
constO = "OOO"
if column1 == constX or column2 == constX or column3 == constX:
return "X"
elif column1 == constO or column2 == constO or column3 == constO:
return "O"
def checkDiagonals(table):
diagonal1 = table[0][0] + table[1][1] + table[2][2]
diagonal2 = table[2][0] + table[1][1] + table[0][2]
constX = "XXX"
constO = "OOO"
if diagonal1 == constX or diagonal2 == constX:
return "X"
elif diagonal1 == constO or diagonal2 == constO:
return "O"
def checkTable(table):
result1 = str(checkRows(table))
result2 = str(checkColumns(table))
result3 = str(checkDiagonals(table))
if result1 == "X" or result1 == "O":
return "Winner is " + result1
elif result2 == "X" or result2 == "O":
return "Winner is " + result2
elif result3 == "X" or result3 == "O":
return "Winner is " + result3
else:
return ""
table = [["-", "-", "-"], ["-", "-", "-"], ["-", "-", "-"]]
print("Welcome to TicTacToe. Choose the game mode")
print("1- Player vs Computer")
print("2- Player vs Player")
gameMode = input()
if gameMode == "1":
i = 0
while i < 9:
if i % 2 == 0:
print("Computer's Move")
playBestMove(table)
elif i % 2 == 1:
print("Your Move : ", end=" "),
move = input()
move1 = int(move[0])-1
move2 = int(move[2])-1
playO(table, move1, move2)
i = i+1
printTable(table)
print("*******************************")
if checkTable(table) != "":
print(checkTable(table))
break
elif gameMode == "2":
printTable(table)
i = 0
print("*******************************")
while i < 9:
if i % 2 == 0:
print("1. Player's Move:", end=" "),
move = input()
move1 = int(move[0])-1
move2 = int(move[2])-1
if playX(table, move1, move2) == False:
print("Move is invalid.")
i = i-1
elif i % 2 == 1:
print("2. Player's Move:", end=" "),
move = input()
move1 = int(move[0])-1
move2 = int(move[2])-1
if playO(table, move1, move2) == False:
print("Move is invalid.")
i = i-1
i = i+1
printTable(table)
print("*******************************")
if checkTable(table) != "":
print(checkTable(table))
break
print("Game Over")