-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
154 lines (140 loc) · 6.2 KB
/
main.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
def printtictactoetable(tictactoetable):
print(tictactoetable[1] + '|' + tictactoetable[2] + '|' + tictactoetable[3])
print('-+-+-')
print(tictactoetable[4] + '|' + tictactoetable[5] + '|' + tictactoetable[6])
print('-+-+-')
print(tictactoetable[7] + '|' + tictactoetable[8] + '|' + tictactoetable[9])
print("\n")
def spaceIsFree(position):
if tictactoetable[position] == ' ':
return True
else:
return False
def insertLetter(letter, position):
if spaceIsFree(position):
tictactoetable[position] = letter
printtictactoetable(tictactoetable)
if (checkDraw()):
print("Draw!")
exit()
if checkForWin():
if letter == 'X':
print("Computer wins!")
exit()
else:
print("Player wins!")
exit()
return
else:
print("Can't insert there! Position already occupied")
position = int(input("Please enter different position: "))
insertLetter(letter, position)
return
def checkForWin():
if (tictactoetable[1] == tictactoetable[2] and tictactoetable[1] == tictactoetable[3] and tictactoetable[1] != ' '):
return True
elif (tictactoetable[4] == tictactoetable[5] and tictactoetable[4] == tictactoetable[6] and tictactoetable[4] != ' '):
return True
elif (tictactoetable[7] == tictactoetable[8] and tictactoetable[7] == tictactoetable[9] and tictactoetable[7] != ' '):
return True
elif (tictactoetable[1] == tictactoetable[4] and tictactoetable[1] == tictactoetable[7] and tictactoetable[1] != ' '):
return True
elif (tictactoetable[2] == tictactoetable[5] and tictactoetable[2] == tictactoetable[8] and tictactoetable[2] != ' '):
return True
elif (tictactoetable[3] == tictactoetable[6] and tictactoetable[3] == tictactoetable[9] and tictactoetable[3] != ' '):
return True
elif (tictactoetable[1] == tictactoetable[5] and tictactoetable[1] == tictactoetable[9] and tictactoetable[1] != ' '):
return True
elif (tictactoetable[7] == tictactoetable[5] and tictactoetable[7] == tictactoetable[3] and tictactoetable[7] != ' '):
return True
else:
return False
def checkWhichMarkWon(mark):
if tictactoetable[1] == tictactoetable[2] and tictactoetable[1] == tictactoetable[3] and tictactoetable[1] == mark:
return True
elif (tictactoetable[4] == tictactoetable[5] and tictactoetable[4] == tictactoetable[6] and tictactoetable[4] == mark):
return True
elif (tictactoetable[7] == tictactoetable[8] and tictactoetable[7] == tictactoetable[9] and tictactoetable[7] == mark):
return True
elif (tictactoetable[1] == tictactoetable[4] and tictactoetable[1] == tictactoetable[7] and tictactoetable[1] == mark):
return True
elif (tictactoetable[2] == tictactoetable[5] and tictactoetable[2] == tictactoetable[8] and tictactoetable[2] == mark):
return True
elif (tictactoetable[3] == tictactoetable[6] and tictactoetable[3] == tictactoetable[9] and tictactoetable[3] == mark):
return True
elif (tictactoetable[1] == tictactoetable[5] and tictactoetable[1] == tictactoetable[9] and tictactoetable[1] == mark):
return True
elif (tictactoetable[7] == tictactoetable[5] and tictactoetable[7] == tictactoetable[3] and tictactoetable[7] == mark):
return True
else:
return False
def checkDraw():
for key in tictactoetable.keys():
if (tictactoetable[key] == ' '):
return False
return True
def playerMove():
position = int(input("Enter the position for 'O': "))
insertLetter(player, position)
return
def compMove():
bestScore = -800
bestMove = 0
for key in tictactoetable.keys():
if (tictactoetable[key] == ' '):
tictactoetable[key] = bot
score = minimax(tictactoetable, 0, False)
tictactoetable[key] = ' '
if (score > bestScore):
bestScore = score
bestMove = key
insertLetter(bot, bestMove)
return
***********************************************************************************************************************************************
def minimax(tictactoetable, depth, isMaximizing):
if (checkWhichMarkWon(bot)):
return 1
elif (checkWhichMarkWon(player)):
return -1
elif (checkDraw()):
return 0
************************************************************************************************************************************************
if (isMaximizing):
bestScore = -800
for key in tictactoetable.keys():
if (tictactoetable[key] == ' '):
tictactoetable[key] = bot
score = minimax(tictactoetable, depth + 1, False)
tictactoetable[key] = ' '
if (score > bestScore):
bestScore = score
return bestScore
**************************************************************************************************************************************************
else:
bestScore = 800
for key in tictactoetable.keys():
if (tictactoetable[key] == ' '):
tictactoetable[key] = player
score = minimax(tictactoetable, depth + 1, True)
tictactoetable[key] = ' '
if (score < bestScore):
bestScore = score
return bestScore
************************************************************************************************************************************************
tictactoetable = {1: ' ', 2: ' ', 3: ' ',
4: ' ', 5: ' ', 6: ' ',
7: ' ', 8: ' ', 9: ' '}
printtictactoetable(tictactoetable)
print("Computer goes first! Good luck.")
print("Positions are as follow:")
print("1, 2, 3 ")
print("4, 5, 6 ")
print("7, 8, 9 ")
print("\n")
player = 'O'
bot = 'X'
global firstComputerMove
firstComputerMove = True
while not checkForWin():
compMove()
playerMove()