Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from guizero import App, Box, PushButton, Text, Drawing, Combo, Slider
- #imports
- #from guizero import App
- #function
- def clear_board():
- new_board = [[None, None, None],[None, None, None],[None, None, None]]
- for x in range(3):
- for y in range(3):
- button = PushButton(board, text = "", grid = [x,y], width=3, command = chose_square, args=[x,y])
- new_board[x][y] = button
- return new_board
- def chose_square(x,y):
- board_squares[x][y].text=turn
- board_squares[x][y].disable()
- toggle_player()
- check_win()
- ## i moved this from below
- def toggle_player():
- global turn
- if turn == "X":
- turn = "O"
- else:
- turn = "X"
- message.value = "And now its your turn, " + turn
- def check_win():
- winner = None
- # Verticle lines ## i changed these to elif's
- if(board_squares[0][0].text == board_squares[0][1]
- .text == board_squares[0][2].text ) and board_squares[0][2].text in ["X", "O"]:
- winner = board_squares[0][0]
- elif(board_squares[1][0].text == board_squares[1][1]
- .text == board_squares[1][2].text ) and board_squares[1][1].text in ["X", "O"]:
- winner = board_squares[1][2]
- elif(board_squares[2][0].text == board_squares[2][1]
- .text == board_squares[2][2].text ) and board_squares[2][2].text in ["X", "O"]:
- winner = board_squares[2][2]
- elif(board_squares[0][0].text == board_squares[1][1]
- .text == board_squares[2][2].text ) and board_squares[2][2].text in ["X", "O"]:
- winner = board_squares[2][2]
- elif(board_squares[2][0].text == board_squares[1][1]
- .text == board_squares[0][2].text ) and board_squares[0][2].text in ["X", "O"]:
- winner = board_squares[0][2]
- #horizontal lines
- elif (board_squares[0][0].text == board_squares[1][0].text == board_squares[2][0].text)and board_squares [2][0].text in ["X", "O"]:
- winner = board_squares[2][0]
- elif (board_squares[0][1].text == board_squares[1][1].text == board_squares[2][1].text)and board_squares [2][1].text in ["X", "O"]:
- winner = board_squares[2][1]
- elif (board_squares[0][2].text == board_squares[1][2].text == board_squares[2][2].text)and board_squares [2][2].text in ["X", "O"]:
- winner = board_squares[2][2]
- if winner is not None:
- message.value = winner.text + " wins!"
- elif moves_taken() == 9:
- message.value = "It's a draw"
- def moves_taken():
- moves = 0
- for row in board_squares:
- for col in row:
- if col.text == "X" or col.text == "O":
- moves = moves + 1
- return moves
- #variables
- turn = "X"
- #App
- app = App("Tic Tac Toe")
- board = Box(app, layout="grid")
- board_squares = clear_board()
- message =Text(app, text ="Its your turn now, " + turn )
- #display
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement