-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.py
51 lines (38 loc) · 1.29 KB
/
ui.py
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
import tkinter as tk
from board import Board
root = tk.Tk()
root.title("TicTacToe!")
# we'll want some kind of game board widget
# top: current player
game = Board()
def player_to_text(player: int) -> str:
if player == Board.CROSS:
return "crosses"
if player == Board.NOUGHT:
return "noughts"
return " "
current_player = tk.Label(root, text=f"Current player: {game.player_to_text(game.turn)}")
current_player.pack()
# middle: grid of cells
# - each cell is a button
# - when clicked, it calls a function
# - the function updates the board
# - the function updates the current player (ui registers a callback for this?)
# - the function checks for a win or draw
# - the function updates the ui
# - invalid location buttons are disabled
CELL_SPACING = 4
board_ui = tk.Frame(root, bg="black")
board_ui.pack()
from random import choice
for row in range(3):
for col in range(3):
cell = tk.Button(board_ui, text=game.get_tile(row, col))
cell.grid(row=row, column=col, padx=CELL_SPACING, pady=CELL_SPACING)
# bottom: status message
status_message = tk.Label(root, text="Status: Playing")
status_message.pack()
root.lift() # bring the window to the front
root.attributes('-topmost',True)
root.after_idle(root.attributes,'-topmost',False)
root.mainloop()