Jul-17-2018, 07:57 AM
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:
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
numClicks += 1 isXTurn = not isXTurn gameOver(rw, col)