2019-07-09 17:00:21 +02:00
|
|
|
from sys import exit
|
2019-07-09 16:21:31 +02:00
|
|
|
from tkinter import Tk, Label, Button, Entry, Frame, LabelFrame, StringVar
|
|
|
|
from tkinter.messagebox import showwarning, showerror, askokcancel
|
2019-07-08 21:10:01 +02:00
|
|
|
from Morpion import Board
|
|
|
|
|
|
|
|
DEFAULT_BUTTON = " " # Default Morpion button text
|
|
|
|
|
|
|
|
|
2019-07-09 16:21:31 +02:00
|
|
|
def on_closing():
|
|
|
|
"""When a windows want to close, show a warning and exit correctly the program"""
|
|
|
|
if askokcancel("Quit", "Do you want to quit?"): # Ask to close
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
2019-07-08 21:10:01 +02:00
|
|
|
def result(r: int):
|
|
|
|
"""r (int): 0 = Anyone win, 1 = Player 1 win, 2 = Player 2 win, 3 = Equality
|
|
|
|
Use the result of a party to reset and announce if necessary"""
|
|
|
|
if r: # If 0, nothing to do
|
|
|
|
if r in [1, 2]: # If someone yin, get the last person who play and mark as winner
|
2019-07-08 21:26:09 +02:00
|
|
|
r1, r2, text = 2, 0, f"{board.curr_turn[1]} win !"
|
2019-07-08 21:10:01 +02:00
|
|
|
elif r == 3: # Set equality
|
|
|
|
r1, r2, text = 1, 1, "Equality"
|
2019-07-08 21:26:09 +02:00
|
|
|
board.curr_turn[1].gameover(r1)
|
|
|
|
board.curr_turn[0].gameover(r2)
|
2019-07-08 21:10:01 +02:00
|
|
|
showwarning("Turn end", text) # Announce the result
|
|
|
|
for b in range(9): # Reset all the buttons
|
|
|
|
buttons[b].config(state="normal", text=DEFAULT_BUTTON)
|
|
|
|
scoreboard.set(f"{board.player1}: {board.player1.points}/{board.player2}: {board.player2.points}") # Scoreboard
|
|
|
|
|
|
|
|
|
|
|
|
def case(posi: int):
|
|
|
|
"""posi (int) : Position of the button on the grid
|
|
|
|
Action when a button is used"""
|
|
|
|
buttons[posi].config(state="disabled", text=board.curr_turn[0].pawn) # Disable and set player pawn on the button
|
|
|
|
result(board.turn(posi)) # Make player turn and return the result of the party
|
2019-07-09 16:21:31 +02:00
|
|
|
turn.set(f"{board.curr_turn[0]} turn !") # Update player turn display
|
|
|
|
|
|
|
|
|
|
|
|
def set_players():
|
2019-07-09 16:28:06 +02:00
|
|
|
"""Set players pawns and name before start the party"""
|
|
|
|
if "" in [p1_pawn.get(), p1_name.get(), p2_pawn.get(), p2_name.get()]: # check if any empty entry
|
2019-07-09 16:21:31 +02:00
|
|
|
showerror("Error", "A entry is blank !") # Show a warning
|
2019-07-09 16:28:06 +02:00
|
|
|
elif p1_pawn.get() == p2_pawn.get(): # Check if players pawns are same
|
|
|
|
showerror("Error", "Players pawns are identical !") # Show a warning
|
2019-07-09 16:21:31 +02:00
|
|
|
elif p1_name.get() == p2_name.get(): # Check if players names are same
|
|
|
|
showerror("Error", "Players names are identical !") # Show a warning
|
|
|
|
else: # If everything is fine
|
|
|
|
players.destroy() # Quite the windows to let the party begin
|
|
|
|
|
|
|
|
|
|
|
|
# Create players config window
|
|
|
|
players = Tk()
|
|
|
|
players.title("Players configs")
|
|
|
|
players.protocol("WM_DELETE_WINDOW", on_closing) # Set action when windows is closing
|
2019-07-08 21:10:01 +02:00
|
|
|
|
2019-07-09 16:21:31 +02:00
|
|
|
# Create a frame for players info and submit button
|
|
|
|
players_f = Frame(players)
|
|
|
|
players_f.pack()
|
2019-07-08 21:10:01 +02:00
|
|
|
|
2019-07-09 16:21:31 +02:00
|
|
|
# Create two frame for each player info
|
|
|
|
players_p1f = LabelFrame(players_f, text="Player 1")
|
|
|
|
players_p2f = LabelFrame(players_f, text="Player 2")
|
|
|
|
players_p1f.grid(row=0, column=0)
|
|
|
|
players_p2f.grid(row=0, column=1)
|
2019-07-08 21:10:01 +02:00
|
|
|
|
2019-07-09 16:28:06 +02:00
|
|
|
p1_pawn, p1_name, p2_pawn, p2_name = StringVar(), StringVar(), StringVar(), StringVar() # Setup entry value
|
2019-07-09 16:21:31 +02:00
|
|
|
|
|
|
|
# Generate symmetrically widgets for player infos
|
2019-07-09 16:28:06 +02:00
|
|
|
for i in [[players_p1f, p1_pawn, p1_name], [players_p2f, p2_pawn, p2_name]]: # List contain the frame and the entry var
|
2019-07-09 16:21:31 +02:00
|
|
|
Label(i[0], text="Player name:").pack()
|
|
|
|
Entry(i[0], textvariable=i[2], width=20).pack()
|
2019-07-09 16:28:06 +02:00
|
|
|
Label(i[0], text="Player pawn:").pack()
|
2019-07-09 16:21:31 +02:00
|
|
|
Entry(i[0], textvariable=i[1], width=5).pack()
|
|
|
|
|
|
|
|
Button(players, text="Submit", command=set_players).pack() # Create the submit button
|
|
|
|
|
|
|
|
players.mainloop()
|
|
|
|
|
2019-07-09 16:28:06 +02:00
|
|
|
board = Board(p1_pawn.get(), p1_name.get(), p2_pawn.get(), p2_name.get()) # Create a party board
|
2019-07-09 16:21:31 +02:00
|
|
|
|
|
|
|
# Create party window
|
2019-07-08 21:10:01 +02:00
|
|
|
windows = Tk()
|
|
|
|
windows.title("Morpion")
|
2019-07-09 16:21:31 +02:00
|
|
|
windows.protocol("WM_DELETE_WINDOW", on_closing) # Set action when windows is closing
|
2019-07-08 21:10:01 +02:00
|
|
|
|
|
|
|
Label(windows, text="Morpion").pack() # Add a text
|
|
|
|
|
|
|
|
# Create a frame for the grid
|
|
|
|
f = Frame(windows)
|
|
|
|
f.pack()
|
|
|
|
|
|
|
|
# Setup variable string display
|
|
|
|
turn = StringVar()
|
|
|
|
scoreboard = StringVar()
|
2019-07-09 16:21:31 +02:00
|
|
|
turn.set(f"{board.curr_turn[0]} turn !")
|
|
|
|
scoreboard.set(f"{board.player1}: {board.player1.points}/{board.player2}: {board.player2.points}")
|
2019-07-08 21:10:01 +02:00
|
|
|
|
|
|
|
Label(windows, textvariable=turn).pack()
|
|
|
|
Label(windows, textvariable=scoreboard).pack()
|
|
|
|
|
|
|
|
# Create button on the grid
|
|
|
|
n = 0
|
|
|
|
buttons = list()
|
|
|
|
for line in range(3):
|
|
|
|
for column in range(3):
|
|
|
|
buttons.append(Button(f, text=DEFAULT_BUTTON, command=lambda n=n: case(n))) # Create and add to the list
|
|
|
|
buttons[n].grid(row=line, column=column) # Set the button to the grid
|
|
|
|
n += 1
|
|
|
|
|
|
|
|
windows.mainloop()
|