Skip to content

Instantly share code, notes, and snippets.

@horstjens
Last active February 20, 2023 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horstjens/4c6f86a647cb380dd57f91bda939dbd9 to your computer and use it in GitHub Desktop.
Save horstjens/4c6f86a647cb380dd57f91bda939dbd9 to your computer and use it in GitHub Desktop.
tictactoe_flet
import flet as ft
import random
class Game:
game_turn = 1
player = "Player One"
char = "X"
def flip():
Game.game_turn += 1
if Game.player == "Player One":
Game.player = "Player Two"
Game.char = "O"
else:
Game.player = "Player One"
Game.char = "X"
def main(page: ft.Page):
page.title = "TicTacToe"
page.vertical_alignment = ft.MainAxisAlignment.CENTER
def check_for_win():
if any((
button1.text == button2.text == button3.text == Game.char,
button4.text == button5.text == button6.text == Game.char,
button7.text == button8.text == button9.text == Game.char,
button1.text == button4.text == button7.text == Game.char,
button2.text == button5.text == button8.text == Game.char,
button3.text == button6.text == button9.text == Game.char,
button1.text == button5.text == button9.text == Game.char,
button7.text == button5.text == button3.text == Game.char,
)):
label1.value = f"{Game.player} wins the game"
page.update()
for b in [button1, button2, button3, button4, button5,
button6, button7, button8, button9]:
if b.text == " ":
b.text = "."
return True
return False
def clickme1(e):
if button1.text != " ":
return
button1.text = Game.char
if check_for_win():
return
flip()
label1.value = Game.player
page.update()
# TODO: disable
def clickme2(e):
if button2.text != " ":
return
button2.text = Game.char
if check_for_win():
return
flip()
label1.value = Game.player
page.update()
# TODO: disable
def clickme3(e):
if button3.text != " ":
return
button3.text = Game.char
if check_for_win():
return
flip()
label1.value = Game.player
page.update()
# TODO: disable
def clickme4(e):
if button4.text != " ":
return
button4.text = Game.char
if check_for_win():
return
flip()
label1.value = Game.player
page.update()
# TODO: disable
def clickme5(e):
if button5.text != " ":
return
button5.text = Game.char
if check_for_win():
return
flip()
label1.value = Game.player
page.update()
# TODO: disable
def clickme6(e):
if button6.text != " ":
return
button6.text = Game.char
if check_for_win():
return
flip()
label1.value = Game.player
page.update()
# TODO: disable
def clickme7(e):
if button7.text != " ":
return
button7.text = Game.char
if check_for_win():
return
flip()
label1.value = Game.player
page.update()
# TODO: disable
def clickme8(e):
if button8.text != " ":
return
button8.text = Game.char
if check_for_win():
return
flip()
label1.value = Game.player
page.update()
# TODO: disable
def clickme9(e):
if button9.text != " ":
return
button9.text = Game.char
if check_for_win():
return
flip()
label1.value = Game.player
page.update()
# TODO: disable
button1 = ft.FilledButton(" ", on_click=clickme1)
button2 = ft.FilledButton(" ", on_click=clickme2)
button3 = ft.FilledButton(" ", on_click=clickme3)
button4 = ft.FilledButton(" ", on_click=clickme4)
button5 = ft.FilledButton(" ", on_click=clickme5)
button6 = ft.FilledButton(" ", on_click=clickme6)
button7 = ft.FilledButton(" ", on_click=clickme7)
button8 = ft.FilledButton(" ", on_click=clickme8)
button9 = ft.FilledButton(" ", on_click=clickme9)
label1 = ft.Text(value="Player One",
text_align=ft.TextAlign.CENTER, width=200)
page.add(ft.Row(
[label1],
alignment=ft.MainAxisAlignment.CENTER,
))
page.add(
ft.Row(
[button1,button2,button3],
alignment=ft.MainAxisAlignment.CENTER,
),
ft.Row(
[button4,button5,button6],
alignment=ft.MainAxisAlignment.CENTER,
),
ft.Row(
[button7,button8,button9],
alignment=ft.MainAxisAlignment.CENTER,
),
)
ft.app(target=main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment