Skip to content

Commit

Permalink
Add 1 player game to GUI (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZengLawrence authored Dec 28, 2024
1 parent d6b98fa commit 32e6696
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"padx",
"pady"
]
}
106 changes: 106 additions & 0 deletions dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""Module to show dialogs to select the number of players and the player's side."""

import tkinter as tk

class PlayerDialog:
"""Dialog to select the number of players."""

def __init__(self, parent):
"""
Initialize the dialog.
Args:
parent (tk.Tk): The parent window.
"""
self.top = tk.Toplevel(parent)
self.top.title("Choose Mode")
self.top.geometry("300x100")
self.result = None

tk.Label(self.top, text="Select the number of players:").pack(pady=10)
tk.Button(self.top, text="1 Player", command=self.one_player).pack(side=tk.LEFT, padx=20)
tk.Button(self.top, text="2 Players", command=self.two_players).pack(side=tk.RIGHT, padx=20)

def one_player(self):
"""Set the result to 1 player and close the dialog."""
self.result = 1
self.top.destroy()

def two_players(self):
"""Set the result to 2 players and close the dialog."""
self.result = 2
self.top.destroy()

def show_player_dialog(parent):
"""
Show the player dialog and return the result.
Args:
parent (tk.Tk): The parent window.
Returns:
int: The number of players selected (1 or 2).
"""
dialog = PlayerDialog(parent)
parent.wait_window(dialog.top)
return dialog.result

class SideDialog:
"""Dialog to select the player's side (X or O)."""

def __init__(self, parent):
"""
Initialize the dialog.
Args:
parent (tk.Tk): The parent window.
"""
self.top = tk.Toplevel(parent)
self.top.title("Choose side")
self.top.geometry("300x100")
self.result = None

tk.Label(self.top, text="Select your side:").pack(pady=10)
tk.Button(self.top, text="X", command=self.choose_x).pack(side=tk.LEFT, padx=20)
tk.Button(self.top, text="O", command=self.choose_o).pack(side=tk.RIGHT, padx=20)

def choose_x(self):
"""Set the result to 'x' and close the dialog."""
self.result = 'x'
self.top.destroy()

def choose_o(self):
"""Set the result to 'o' and close the dialog."""
self.result = 'o'
self.top.destroy()

def show_side_dialog(parent):
"""
Show the side dialog and return the result.
Args:
parent (tk.Tk): The parent window.
Returns:
str: The side selected ('x' or 'o').
"""
dialog = SideDialog(parent)
parent.wait_window(dialog.top)
return dialog.result

if __name__ == "__main__":
root = tk.Tk()
root.title("Main Application")

def on_start():
"""Start the game."""
result = show_player_dialog(root)
if result:
print(f"Selected: {result}")
side = show_side_dialog(root)
if side:
print(f"Selected: {side}")

tk.Button(root, text="Start Game", command=on_start).pack(pady=20)

root.mainloop()
8 changes: 8 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from tkinter import Tk
from tkinter import ttk
from game import Game
from dialog import show_player_dialog, show_side_dialog

def init_button(frame, text, game, col, row):
"""Initialize button in board"""
Expand Down Expand Up @@ -49,6 +50,7 @@ def __init__(self, root=None):
game = Game()
self.__layout(game)
game.register(self.refresh)
self.game = game

def __layout(self, game):
_frm = ttk.Frame(self.root, padding=10)
Expand All @@ -61,6 +63,12 @@ def __layout(self, game):

def run(self):
"""Run the app"""
if show_player_dialog(self.root) == 1:
side = show_side_dialog(self.root)
players = [side] if side else None
self.game.start(players)
else:
self.game.start()
self.root.mainloop()

def refresh(self, game):
Expand Down

0 comments on commit 32e6696

Please sign in to comment.