Created
May 2, 2023 14:13
-
-
Save ChicagoDev/dcb77e524736bbb9371e2125bb4f7b9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Imports --------------- | |
from guizero import App, Box, PushButton, Text | |
# Functions ------------- | |
def clear_board(): | |
new_board = [ [None, None, None], | |
[None, None, None], | |
[None, None, None] ] | |
for numberX in range(3): | |
for numberY in range(3): | |
new_board[numberX][numberY] = PushButton(board, | |
text="", | |
grid=[numberX, numberY], | |
width=8, height=5, | |
command=choose_square, args=[numberX, numberY]) | |
return new_board | |
def choose_square(x, y): | |
board_squares[x][y].text = turn | |
board_squares[x][y].disable() | |
toggle_player() | |
player_turn_text.value = "Its your turn " + turn | |
check_win() | |
def toggle_player(): | |
global turn | |
if turn == "X": | |
turn = "O" | |
else: | |
turn = "X" | |
def check_win(): | |
winner = None | |
# Vertical lines | |
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][0].text in ["X", "O"]: | |
winner = board_squares[1][0] | |
# 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[0][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[0][1] | |
# Diagonals | |
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[0][0] | |
if winner is not None: | |
player_turn_text.value = winner.text + " wins!" | |
# Variables ------------- | |
# App ------------------- | |
app = App("Tic tac toe") | |
turn = "X" | |
# Widgets! | |
board = Box(app, layout="grid") | |
board_squares = clear_board() | |
player_turn_text = Text(app, text="Its your turn " + turn) | |
#input("Button was just created @location: x=" + str(numberX) + ", y=" + str(numberY)) | |
app.display() | |
#sports_list = [ ["Elle", "Luca", "Alina"], ["Sienna", "Juliette", "Flynn"], ["Lyla", "Jacob", "Will"] ] | |
# track_group = 0 | |
# lax_group = 1 | |
# soft_ball_group = 2 | |
# print(sports_list[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment