May-22-2020, 04:31 PM
Hi Everyone,
Here is my stab at a Tic-Tac-Toe game using tkinter. Player controls "X" and makes moves by using numeric input. My number one goal here was to have an AI that can't be defeated, so please let me know if you find a sequence that wins! The AI was done through brute force by starting out with a few obvious checks and then adding new ones as I found new ways to win a game, so it is somewhat haphazardly cobbled together. I did make one pass through the code to get rid of global variables and consolidate some code into functions, so it is now less of a jumbled mess than it previously was.
Any and all critiques or suggestions are welcome!
Here is my stab at a Tic-Tac-Toe game using tkinter. Player controls "X" and makes moves by using numeric input. My number one goal here was to have an AI that can't be defeated, so please let me know if you find a sequence that wins! The AI was done through brute force by starting out with a few obvious checks and then adding new ones as I found new ways to win a game, so it is somewhat haphazardly cobbled together. I did make one pass through the code to get rid of global variables and consolidate some code into functions, so it is now less of a jumbled mess than it previously was.
Any and all critiques or suggestions are welcome!
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
import sys from tkinter import * from tkinter import font as tkfont class Gameboard: def __init__( self ): self .tk = Tk() self .tk.title( "Tic-Tac-Toe" ) self .tk.resizable( 0 , 0 ) self .tk.wm_attributes( "-topmost" , 1 ) self .wld = [ 0 ] * 3 self .gameover = False self .gameclose = False self .gamecanvas = None self .startgame() self .tk.mainloop() def startgame( self ): if self .gameover = = True and self .gamecanvas is not None : self .gamecanvas.destroy() self .gameover = False self .board = [ 0 ] * 9 self .turncount = 1 self .gamecanvas = Canvas( self .tk, width = 400 , height = 400 ) self .gamecanvas.pack() self .gamecanvas.bind_all( '<KeyPress-y>' , self .restart) self .gamecanvas.bind_all( '<KeyPress-Y>' , self .restart) self .gamecanvas.bind_all( '<KeyPress-n>' , self .endgame) self .gamecanvas.bind_all( '<KeyPress-N>' , self .endgame) self .gamecanvas.bind_all( '<KeyPress-0>' , self .playZero) self .gamecanvas.bind_all( '<KeyPress-1>' , self .playOne) self .gamecanvas.bind_all( '<KeyPress-2>' , self .playTwo) self .gamecanvas.bind_all( '<KeyPress-3>' , self .playThree) self .gamecanvas.bind_all( '<KeyPress-4>' , self .playFour) self .gamecanvas.bind_all( '<KeyPress-5>' , self .playFive) self .gamecanvas.bind_all( '<KeyPress-6>' , self .playSix) self .gamecanvas.bind_all( '<KeyPress-7>' , self .playSeven) self .gamecanvas.bind_all( '<KeyPress-8>' , self .playEight) self .gamecanvas.bind_all( '<KeyPress-8>' , self .playEight) self .gamecanvas.bind_all( '<KeyPress-Return>' , self .closegame) labels = plotBoard( self .board) showBoard( self .gamecanvas,labels) def restart( self ,evt): if self .gameover = = True : self .startgame() def endgame( self ,evt): if self .gameover = = True : self .gamecanvas.delete( 'all' ) smallfont = tkfont.Font(family = "Arial" , size = 15 ) self .gamecanvas.create_text( 200 , 50 , font = smallfont, text = 'Thanks for playing!' , fill = 'blue' ) winstext = ( 'Wins: ' + str ( self .wld[ 0 ])) lossestext = ( 'Losses: ' + str ( self .wld[ 1 ])) drawstext = ( 'Draws: ' + str ( self .wld[ 2 ])) self .gamecanvas.create_text( 200 , 100 , font = smallfont, text = winstext, fill = 'blue' ) self .gamecanvas.create_text( 200 , 150 , font = smallfont, text = lossestext, fill = 'blue' ) self .gamecanvas.create_text( 200 , 200 , font = smallfont, text = drawstext, fill = 'blue' ) self .gamecanvas.create_text( 200 , 300 , font = smallfont, text = 'Hit Enter to leave.' , fill = 'blue' ) self .gameclose = True def closegame( self ,evt): if self .gameover = = True and self .gameclose = = True : self .tk.destroy() sys.exit() def advanceTurn( self ): self .turncount + = 1 labels = plotBoard( self .board) showBoard( self .gamecanvas,labels) winner = checkWin( self .board, self .turncount, self .wld) if winner in [ 'O' , 'X' , 'D' ]: winMessage( self .gamecanvas,winner) self .gameover = True else : self .board = aiMove( self .board, self .turncount) self .turncount + = 1 labels = plotBoard( self .board) showBoard( self .gamecanvas,labels) winner = checkWin( self .board, self .turncount, self .wld) if winner in [ 'O' , 'X' , 'D' ]: winMessage( self .gamecanvas,winner) self .gameover = True def playZero( self ,evt): if self .board[ 0 ] = = 0 and self .gameover = = False : self .board[ 0 ] = 1 self .advanceTurn() def playOne( self ,evt): if self .board[ 1 ] = = 0 and self .gameover = = False : self .board[ 1 ] = 1 self .advanceTurn() def playTwo( self ,evt): if self .board[ 2 ] = = 0 and self .gameover = = False : self .board[ 2 ] = 1 self .advanceTurn() def playThree( self ,evt): if self .board[ 3 ] = = 0 and self .gameover = = False : self .board[ 3 ] = 1 self .advanceTurn() def playFour( self ,evt): if self .board[ 4 ] = = 0 and self .gameover = = False : self .board[ 4 ] = 1 self .advanceTurn() def playFive( self ,evt): if self .board[ 5 ] = = 0 and self .gameover = = False : self .board[ 5 ] = 1 self .advanceTurn() def playSix( self ,evt): if self .board[ 6 ] = = 0 and self .gameover = = False : self .board[ 6 ] = 1 self .advanceTurn() def playSeven( self ,evt): if self .board[ 7 ] = = 0 and self .gameover = = False : self .board[ 7 ] = 1 self .advanceTurn() def playEight( self ,evt): if self .board[ 8 ] = = 0 and self .gameover = = False : self .board[ 8 ] = 1 self .advanceTurn() def plotBoard(board): ## check values of the board and assign number, X, or O to a list currentBoard = [''] * 9 for i in range ( 0 , 9 ): if board[i] = = 0 : currentBoard[i] = str (i) elif board[i] = = 1 : currentBoard[i] = 'X' elif board[i] = = 2 : currentBoard[i] = 'O' return currentBoard def showBoard(canvas,board): ## draw the board canvas.delete( 'all' ) canvas.create_line( 150 , 50 , 150 , 350 ) canvas.create_line( 250 , 50 , 250 , 350 ) canvas.create_line( 50 , 150 , 350 , 150 ) canvas.create_line( 50 , 250 , 350 , 250 ) canvas.create_text( 100 , 100 , text = board[ 0 ], font = ( 'Times' , 80 )) canvas.create_text( 200 , 100 , text = board[ 1 ], font = ( 'Times' , 80 )) canvas.create_text( 300 , 100 , text = board[ 2 ], font = ( 'Times' , 80 )) canvas.create_text( 100 , 200 , text = board[ 3 ], font = ( 'Times' , 80 )) canvas.create_text( 200 , 200 , text = board[ 4 ], font = ( 'Times' , 80 )) canvas.create_text( 300 , 200 , text = board[ 5 ], font = ( 'Times' , 80 )) canvas.create_text( 100 , 300 , text = board[ 6 ], font = ( 'Times' , 80 )) canvas.create_text( 200 , 300 , text = board[ 7 ], font = ( 'Times' , 80 )) canvas.create_text( 300 , 300 , text = board[ 8 ], font = ( 'Times' , 80 )) def checkWin(board,turncount,wld): ##check for X win if ((board[ 0 ] = = board[ 1 ] = = board[ 2 ] = = 1 ) \ or (board[ 0 ] = = board[ 4 ] = = board[ 8 ] = = 1 ) \ or (board[ 0 ] = = board[ 3 ] = = board[ 6 ] = = 1 ) \ or (board[ 3 ] = = board[ 4 ] = = board[ 5 ] = = 1 ) \ or (board[ 6 ] = = board[ 4 ] = = board[ 2 ] = = 1 ) \ or (board[ 6 ] = = board[ 7 ] = = board[ 8 ] = = 1 ) \ or (board[ 1 ] = = board[ 4 ] = = board[ 7 ] = = 1 ) \ or (board[ 2 ] = = board[ 5 ] = = board[ 8 ] = = 1 )): wld[ 0 ] + = 1 return 'X' ##check for O win elif ((board[ 0 ] = = board[ 1 ] = = board[ 2 ] = = 2 ) \ or (board[ 0 ] = = board[ 4 ] = = board[ 8 ] = = 2 ) \ or (board[ 0 ] = = board[ 3 ] = = board[ 6 ] = = 2 ) \ or (board[ 3 ] = = board[ 4 ] = = board[ 5 ] = = 2 ) \ or (board[ 6 ] = = board[ 4 ] = = board[ 2 ] = = 2 ) \ or (board[ 6 ] = = board[ 7 ] = = board[ 8 ] = = 2 ) \ or (board[ 1 ] = = board[ 4 ] = = board[ 7 ] = = 2 ) \ or (board[ 2 ] = = board[ 5 ] = = board[ 8 ] = = 2 )): wld[ 1 ] + = 1 return 'O' ##check for draw elif turncount = = 10 : wld[ 2 ] + = 1 return 'D' else : return None def aiMove(board,turncount): ## on second turn, take middle square if open, or top left corner if X took middle if turncount = = 2 : if board[ 4 ] = = 0 : board[ 4 ] = 2 else : board[ 0 ] = 2 ## on any turn beyond second, check if O can win, then check if X can win ## and make the appropriate move to win or block elif (board[ 3 ] = = board[ 6 ] = = 2 and board[ 0 ] = = 0 )\ or (board[ 1 ] = = board[ 2 ] = = 2 and board[ 0 ] = = 0 )\ or (board[ 4 ] = = board[ 8 ] = = 2 and board[ 0 ] = = 0 ): board[ 0 ] = 2 elif (board[ 0 ] = = board[ 2 ] = = 2 and board[ 1 ] = = 0 )\ or (board[ 4 ] = = board[ 7 ] = = 2 and board[ 1 ] = = 0 ): board[ 1 ] = 2 elif (board[ 0 ] = = board[ 1 ] = = 2 and board[ 2 ] = = 0 )\ or (board[ 6 ] = = board[ 4 ] = = 2 and board[ 2 ] = = 0 )\ or (board[ 5 ] = = board[ 8 ] = = 2 and board[ 2 ] = = 0 ): board[ 2 ] = 2 elif (board[ 0 ] = = board[ 6 ] = = 2 and board[ 3 ] = = 0 )\ or (board[ 4 ] = = board[ 5 ] = = 2 and board[ 3 ] = = 0 ): board[ 3 ] = 2 elif (board[ 0 ] = = board[ 8 ] = = 2 and board[ 4 ] = = 0 )\ or (board[ 3 ] = = board[ 5 ] = = 2 and board[ 4 ] = = 0 )\ or (board[ 6 ] = = board[ 2 ] = = 2 and board[ 4 ] = = 0 )\ or (board[ 1 ] = = board[ 7 ] = = 2 and board[ 4 ] = = 0 ): board[ 4 ] = 2 elif (board[ 3 ] = = board[ 4 ] = = 2 and board[ 5 ] = = 0 )\ or (board[ 2 ] = = board[ 8 ] = = 2 and board[ 5 ] = = 0 ): board[ 5 ] = 2 elif (board[ 0 ] = = board[ 3 ] = = 2 and board[ 6 ] = = 0 )\ or (board[ 4 ] = = board[ 2 ] = = 2 and board[ 6 ] = = 0 )\ or (board[ 7 ] = = board[ 8 ] = = 2 and board[ 6 ] = = 0 ): board[ 6 ] = 2 elif (board[ 1 ] = = board[ 4 ] = = 2 and board[ 7 ] = = 0 )\ or (board[ 6 ] = = board[ 8 ] = = 2 and board[ 7 ] = = 0 ): board[ 7 ] = 2 elif (board[ 0 ] = = board[ 4 ] = = 2 and board[ 8 ] = = 0 )\ or (board[ 6 ] = = board[ 7 ] = = 2 and board[ 8 ] = = 0 )\ or (board[ 2 ] = = board[ 5 ] = = 2 and board[ 8 ] = = 0 ): board[ 8 ] = 2 elif (board[ 3 ] = = board[ 6 ] = = 1 and board[ 0 ] = = 0 )\ or (board[ 1 ] = = board[ 2 ] = = 1 and board[ 0 ] = = 0 )\ or (board[ 4 ] = = board[ 8 ] = = 1 and board[ 0 ] = = 0 ): board[ 0 ] = 2 elif (board[ 0 ] = = board[ 2 ] = = 1 and board[ 1 ] = = 0 )\ or (board[ 4 ] = = board[ 7 ] = = 1 and board[ 1 ] = = 0 ): board[ 1 ] = 2 elif (board[ 0 ] = = board[ 1 ] = = 1 and board[ 2 ] = = 0 )\ or (board[ 6 ] = = board[ 4 ] = = 1 and board[ 2 ] = = 0 )\ or (board[ 5 ] = = board[ 8 ] = = 1 and board[ 2 ] = = 0 ): board[ 2 ] = 2 elif (board[ 0 ] = = board[ 6 ] = = 1 and board[ 3 ] = = 0 )\ or (board[ 4 ] = = board[ 5 ] = = 1 and board[ 3 ] = = 0 ): board[ 3 ] = 2 elif (board[ 0 ] = = board[ 8 ] = = 1 and board[ 4 ] = = 0 )\ or (board[ 3 ] = = board[ 5 ] = = 1 and board[ 4 ] = = 0 )\ or (board[ 6 ] = = board[ 2 ] = = 1 and board[ 4 ] = = 0 )\ or (board[ 1 ] = = board[ 7 ] = = 1 and board[ 4 ] = = 0 ): board[ 4 ] = 2 elif (board[ 3 ] = = board[ 4 ] = = 1 and board[ 5 ] = = 0 )\ or (board[ 2 ] = = board[ 8 ] = = 1 and board[ 5 ] = = 0 ): board[ 5 ] = 2 elif (board[ 0 ] = = board[ 3 ] = = 1 and board[ 6 ] = = 0 )\ or (board[ 4 ] = = board[ 2 ] = = 1 and board[ 6 ] = = 0 )\ or (board[ 7 ] = = board[ 8 ] = = 1 and board[ 6 ] = = 0 ): board[ 6 ] = 2 elif (board[ 1 ] = = board[ 4 ] = = 1 and board[ 7 ] = = 0 )\ or (board[ 6 ] = = board[ 8 ] = = 1 and board[ 7 ] = = 0 ): board[ 7 ] = 2 elif (board[ 0 ] = = board[ 4 ] = = 1 and board[ 8 ] = = 0 )\ or (board[ 6 ] = = board[ 7 ] = = 1 and board[ 8 ] = = 0 )\ or (board[ 2 ] = = board[ 5 ] = = 1 and board[ 8 ] = = 0 ): board[ 8 ] = 2 ## If we get to this point, it is at least the 4th turn and there ## is no win possible for either player, so we start checking for ## more specific scenarios ## If it's the 4th turn and X went in the center first, ## then O is in top left and any move by X other than ## bottom right should have already been countered ## thanks to win condition checks elif (board[ 4 ] = = board[ 8 ] = = 1 and turncount = = 4 and board[ 6 ] = = 0 ): board[ 6 ] = 2 ## check if 7 and 5 are occupied and 8 is open (any other scenario ## of two side squares occupied by X is already countered) elif (board[ 7 ] = = board[ 5 ] = = 1 and board[ 8 ] = = 0 ): board[ 8 ] = 2 ## check for circumstance where X played a corner followed by non-adjacent ## side square, and take the opposite corner elif (board[ 2 ] = = 1 and (board[ 7 ] = = 1 or board[ 3 ] = = 1 ) and board[ 6 ] = = 0 ): board[ 6 ] = 2 elif (board[ 0 ] = = 1 and (board[ 7 ] = = 1 or board[ 5 ] = = 1 ) and board[ 8 ] = = 0 ): board[ 8 ] = 2 ## check for circumstance where X occupies opposite corners and ## play a side to prevent any win opportunities elif (board[ 0 ] = = 1 and board[ 8 ] = = 1 and board[ 7 ] = = 0 ): board[ 7 ] = 2 elif (board[ 2 ] = = 1 and board[ 6 ] = = 1 and board[ 5 ] = = 0 ): board[ 5 ] = 2 ## if we get to this point, just take the first open square we can find else : move = 0 for i in range ( 0 , 9 ): if board[i] = = 0 : move = i break board[move] = 2 return board def winMessage(canvas,winner): fatfont = tkfont.Font(family = "Arial" , size = 75 , weight = "bold" ) slimfont = tkfont.Font(family = "Arial" , size = 45 ) smallfont = tkfont.Font(family = "Arial" , size = 15 ) if winner = = 'X' : canvas.create_text( 200 , 100 , font = fatfont, text = 'YOU' , fill = 'red' ) canvas.create_text( 200 , 175 , font = fatfont, text = 'WON!' , fill = 'red' ) canvas.create_text( 200 , 270 , font = slimfont, text = 'IMPOSSIBLE!' , fill = 'red' ) canvas.create_text( 200 , 360 , font = smallfont, text = 'Enter Y to play again or N to quit.' , fill = 'black' ) elif winner = = 'O' : canvas.create_text( 200 , 35 , font = slimfont, text = 'O WINS!' , fill = 'blue' ) canvas.create_text( 200 , 160 , font = slimfont, text = 'GAME' , fill = 'blue' ) canvas.create_text( 200 , 240 , font = slimfont, text = 'OVER' , fill = 'blue' ) canvas.create_text( 200 , 360 , font = smallfont, text = 'Enter Y to play again or N to quit.' , fill = 'black' ) elif winner = = 'D' : canvas.create_text( 200 , 35 , font = slimfont, text = 'DRAW' , fill = 'blue' ) canvas.create_text( 200 , 160 , font = slimfont, text = 'GAME' , fill = 'blue' ) canvas.create_text( 200 , 240 , font = slimfont, text = 'OVER' , fill = 'blue' ) canvas.create_text( 200 , 360 , font = smallfont, text = 'Enter Y to play again or N to quit.' , fill = 'black' ) Gameboard() |