1
1
from random import choice
2
2
from math import inf
3
3
4
- board = [[0 , 0 , 0 ],
5
- [0 , 0 , 0 ],
6
- [0 , 0 , 0 ]]
4
+ board = [[0 , 0 , 0 ], [0 , 0 , 0 ], [0 , 0 , 0 ]]
5
+
7
6
8
7
def Gameboard (board ):
9
- chars = {1 : 'X' , - 1 : 'O' , 0 : ' ' }
8
+ chars = {1 : "X" , - 1 : "O" , 0 : " " }
10
9
for x in board :
11
10
for y in x :
12
11
ch = chars [y ]
13
- print (f'| { ch } |' , end = '' )
14
- print ('\n ' + '---------------' )
15
- print ('===============' )
12
+ print (f"| { ch } |" , end = "" )
13
+ print ("\n " + "---------------" )
14
+ print ("===============" )
15
+
16
16
17
17
def Clearboard (board ):
18
18
for x , row in enumerate (board ):
19
19
for y , col in enumerate (row ):
20
20
board [x ][y ] = 0
21
21
22
+
22
23
def winningPlayer (board , player ):
23
- conditions = [[board [0 ][0 ], board [0 ][1 ], board [0 ][2 ]],
24
- [board [1 ][0 ], board [1 ][1 ], board [1 ][2 ]],
25
- [board [2 ][0 ], board [2 ][1 ], board [2 ][2 ]],
26
- [board [0 ][0 ], board [1 ][0 ], board [2 ][0 ]],
27
- [board [0 ][1 ], board [1 ][1 ], board [2 ][1 ]],
28
- [board [0 ][2 ], board [1 ][2 ], board [2 ][2 ]],
29
- [board [0 ][0 ], board [1 ][1 ], board [2 ][2 ]],
30
- [board [0 ][2 ], board [1 ][1 ], board [2 ][0 ]]]
24
+ conditions = [
25
+ [board [0 ][0 ], board [0 ][1 ], board [0 ][2 ]],
26
+ [board [1 ][0 ], board [1 ][1 ], board [1 ][2 ]],
27
+ [board [2 ][0 ], board [2 ][1 ], board [2 ][2 ]],
28
+ [board [0 ][0 ], board [1 ][0 ], board [2 ][0 ]],
29
+ [board [0 ][1 ], board [1 ][1 ], board [2 ][1 ]],
30
+ [board [0 ][2 ], board [1 ][2 ], board [2 ][2 ]],
31
+ [board [0 ][0 ], board [1 ][1 ], board [2 ][2 ]],
32
+ [board [0 ][2 ], board [1 ][1 ], board [2 ][0 ]],
33
+ ]
31
34
32
35
if [player , player , player ] in conditions :
33
36
return True
34
37
35
38
return False
36
39
40
+
37
41
def gameWon (board ):
38
42
return winningPlayer (board , 1 ) or winningPlayer (board , - 1 )
39
43
44
+
40
45
def printResult (board ):
41
46
if winningPlayer (board , 1 ):
42
- print (' X has won! ' + ' \n ' )
47
+ print (" X has won! " + " \n " )
43
48
44
49
elif winningPlayer (board , - 1 ):
45
- print ('O \ ' s have won! ' + ' \n ' )
50
+ print ("O 's have won! " + " \n " )
46
51
47
52
else :
48
- print ('Draw' + '\n ' )
53
+ print ("Draw" + "\n " )
54
+
49
55
50
56
def blanks (board ):
51
57
blank = []
@@ -56,32 +62,44 @@ def blanks(board):
56
62
57
63
return blank
58
64
65
+
59
66
def boardFull (board ):
60
67
if len (blanks (board )) == 0 :
61
68
return True
62
69
return False
63
70
71
+
64
72
def setMove (board , x , y , player ):
65
73
board [x ][y ] = player
66
74
75
+
67
76
def playerMove (board ):
68
77
e = True
69
- moves = {1 : [0 , 0 ], 2 : [0 , 1 ], 3 : [0 , 2 ],
70
- 4 : [1 , 0 ], 5 : [1 , 1 ], 6 : [1 , 2 ],
71
- 7 : [2 , 0 ], 8 : [2 , 1 ], 9 : [2 , 2 ]}
78
+ moves = {
79
+ 1 : [0 , 0 ],
80
+ 2 : [0 , 1 ],
81
+ 3 : [0 , 2 ],
82
+ 4 : [1 , 0 ],
83
+ 5 : [1 , 1 ],
84
+ 6 : [1 , 2 ],
85
+ 7 : [2 , 0 ],
86
+ 8 : [2 , 1 ],
87
+ 9 : [2 , 2 ],
88
+ }
72
89
while e :
73
90
try :
74
- move = int (input (' Enter a number between 1-9: ' ))
91
+ move = int (input (" Enter a number between 1-9: " ))
75
92
if move < 1 or move > 9 :
76
- print (' Invalid Move! Try again!' )
93
+ print (" Invalid Move! Try again!" )
77
94
elif not (moves [move ] in blanks (board )):
78
- print (' Invalid Move! Try again!' )
95
+ print (" Invalid Move! Try again!" )
79
96
else :
80
97
setMove (board , moves [move ][0 ], moves [move ][1 ], 1 )
81
98
Gameboard (board )
82
99
e = False
83
- except (KeyError , ValueError ):
84
- print ('Enter a number!' )
100
+ except (KeyError , ValueError ):
101
+ print ("Enter a number!" )
102
+
85
103
86
104
def getScore (board ):
87
105
if winningPlayer (board , 1 ):
@@ -93,6 +111,7 @@ def getScore(board):
93
111
else :
94
112
return 0
95
113
114
+
96
115
def abminimax (board , depth , alpha , beta , player ):
97
116
row = - 1
98
117
col = - 1
@@ -127,6 +146,7 @@ def abminimax(board, depth, alpha, beta, player):
127
146
else :
128
147
return [row , col , beta ]
129
148
149
+
130
150
def o_comp (board ):
131
151
if len (blanks (board )) == 9 :
132
152
x = choice ([0 , 1 , 2 ])
@@ -139,6 +159,7 @@ def o_comp(board):
139
159
setMove (board , result [0 ], result [1 ], - 1 )
140
160
Gameboard (board )
141
161
162
+
142
163
def x_comp (board ):
143
164
if len (blanks (board )) == 9 :
144
165
x = choice ([0 , 1 , 2 ])
@@ -151,6 +172,7 @@ def x_comp(board):
151
172
setMove (board , result [0 ], result [1 ], 1 )
152
173
Gameboard (board )
153
174
175
+
154
176
def makeMove (board , player , mode ):
155
177
if mode == 1 :
156
178
if player == 1 :
@@ -164,16 +186,17 @@ def makeMove(board, player, mode):
164
186
else :
165
187
x_comp (board )
166
188
189
+
167
190
def pvc ():
168
191
while True :
169
192
try :
170
- order = int (input (' Enter to play 1st or 2nd: ' ))
193
+ order = int (input (" Enter to play 1st or 2nd: " ))
171
194
if not (order == 1 or order == 2 ):
172
- print (' Please pick 1 or 2' )
195
+ print (" Please pick 1 or 2" )
173
196
else :
174
197
break
175
- except (KeyError , ValueError ):
176
- print (' Enter a number' )
198
+ except (KeyError , ValueError ):
199
+ print (" Enter a number" )
177
200
178
201
Clearboard (board )
179
202
if order == 2 :
@@ -187,6 +210,7 @@ def pvc():
187
210
188
211
printResult (board )
189
212
213
+
190
214
# Driver Code
191
215
print ("=================================================" )
192
216
print ("TIC-TAC-TOE using MINIMAX with ALPHA-BETA Pruning" )
0 commit comments