Jul-17-2018, 07:57 AM
(This post was last modified: Jul-17-2018, 08:00 AM by juliabrushett.)
Hello!
I am trying to make a Tic-Tac-Toe game, and I am having trouble with the 'X' and 'O' characters showing up in my program. My code is below:
small change in code: lines 24-26 are now
I am trying to make a Tic-Tac-Toe game, and I am having trouble with the 'X' and 'O' characters showing up in my program. My code is below:
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 |
from tkinter import * grid = [[ 0 for i in range ( 3 )] for j in range ( 3 )] numClicks = 0 isDone = False isXTurn = True def restart() : print ( "Restart Game" ) def resetGame() : global isXTurn global isDone global numClicks for i in range ( 3 ) : for j in range ( 3 ) : grid[rw][col].config(text = " " ) def markSpace(rw, col) : global isXTurn global isDone global numClicks numClicks = 0 isXTurn = True isDone = False space = grid[rw][col].cget( "text" ) if (isDone = = True ) : return elif (space = = " " ) : if (space = = isXTurn) : grid[rw][col].config(text = "X" , fg = "red" ) lblStatus.config(text = "O\'s Turn" ) else : grid[rw][col].config(text = "O" , fg = "blue" ) lblStatus.config(text = "X\'s Turn" ) else : lblStatus.config(text = "Invalid Move" ) return def gameOver(rw, col) : global numClicks global isDone winner = ' ' if (grid[ 0 ][ 0 ].cget( 'text' ) = = grid[ 1 ][ 1 ].cget( 'text' ) and grid[ 1 ][ 1 ].cget( 'text' ) = = grid[ 2 ][ 2 ].cget( 'text' )) : winner = grid[ 0 ][ 0 ].cget( 'text' ) elif (grid[ 2 ][ 0 ].cget( 'text' ) = = grid[ 1 ][ 1 ].cget( 'text' ) and grid[ 1 ][ 1 ].cget( 'text' ) = = grid[ 0 ][ 2 ].cget( 'text' )) : winner = grid[ 2 ][ 1 ].cget( 'text' ) else : for r in range ( 0 , 3 ) : if (grid[r][ 0 ].cget( 'text' ) ! = ' ' and grid[r][ 0 ].cget( 'text' ) = = grid[r][ 1 ].cget( 'text' ) and grid[r][ 1 ].cget( 'text' ) = = grid[r][ 2 ].cget( 'text' )): winner = grid[r][ 0 ].cget( 'text' ) elif (grid[ 0 ][r].cget( 'text' ) ! = ' ' and grid[ 0 ][r].cget( 'text' ) = = grid[ 1 ][r].cget( 'text' ) and grid[ 1 ][r].cget( 'text' ) = = grid[ 2 ][r].cget( 'text' )): winner = grid[ 0 ][r].cget( 'text' ) isDone = True if (winner = = ' ' and numClicks > = 9 ) : lblStatus.config(text = 'Tie Game' ) elif (winner ! = ' ' ) : lblStatus.config(text = winner + ' Wins!' ) else : lblStatus.config(text = 'X\'s Turn' if isXTurn else 'O\'s Turn' ) isDone = False main = Tk() main.title( "Tic-Tac-Toe" ) main.geometry( "355x420" ) topFrame = Frame(main, width = 320 , height = 40 ) topFrame.place(x = 12 , y = 12 ) lablStatus = Label(topFrame, text = "O's Turn" ) lablStatus.config(fg = "blue" , bg = "yellow" , font = "serif 16 bold" ) lablStatus.pack(side = TOP) mainFrame = Frame(main, width = 330 , height = 330 ) mainFrame.config(bg = "black" ) mainFrame.place(x = 10 , y = 42 ) for rw in range ( 0 , 3 ) : for col in range ( 0 , 3 ) : grid[rw][col] = Button(mainFrame, text = " " , relief = 'solid' ) grid[rw][col].config(font = "monospace 36 bold" , fg = 'red' , height = 1 , width = 3 ) grid[rw][col].place(x = rw * 105 + 10 , y = col * 105 + 10 ) btnRestart = Button(main, text = "Restart" , command = resetGame, width = 30 ) btnRestart.place(x = 45 , y = 380 ) main.mainloop() |
small change in code: lines 24-26 are now
1 2 3 |
numClicks + = 1 isXTurn = not isXTurn gameOver(rw, col) |