board.py 1007 Bytes
Newer Older
Fedrico Otaran's avatar
Fedrico Otaran committed
1
import PySimpleGUI as sg
2
from itertools import cycle
Fedrico Otaran's avatar
Fedrico Otaran committed
3
from src.windows import board
4
from src.handlers import board as board_handler
Fedrico Otaran's avatar
Fedrico Otaran committed
5
6
7


def start():
8
9
10
    """
    Lanza la ejecución de la ventana del tablero
    """
Fedrico Otaran's avatar
Fedrico Otaran committed
11
12
13
14
15
    window = loop()
    window.close()


def loop():
16
17
18
    """
    Loop de la ventana del tablero que capta sus eventos
    """
19
20
21
22
23
24
25
    player_1 = {"name": "Maria", "value": "X"}
    player_2 = {"name": "Marta", "value": "O"}
    turn = cycle([player_1, player_2])

    board_data = [[" "] * 3 for _i in range(3)]

    window = board.build(player_1, player_2, board_data)
Fedrico Otaran's avatar
Fedrico Otaran committed
26
27
28
29
30
31
32
33

    while True:
        event, _values = window.read()

        if event in (sg.WINDOW_CLOSED, "Exit", "-exit-"):
            break

        if event.startswith("cell"):
34
            player = next(turn)
35
36
37
38
39
            board_data, winner = board_handler.play(player, window, event, board_data)

            if winner:
                sg.popup(f"¡Felicitaciones! Ha ganado {player['name']}")
                break
Fedrico Otaran's avatar
Fedrico Otaran committed
40
41

    return window