Commit 756da7f6 authored by Fedrico Otaran's avatar Fedrico Otaran
Browse files

Agrego un handler para chequear si hay ganador en cada jugada

parent 05c27303
import PySimpleGUI as sg
from itertools import cycle
from src.windows import board
from src.handlers import board as board_handler
def start():
"""
Lanza la ejecución de la ventana del tablero
"""
window = loop()
window.close()
def loop():
"""
Loop de la ventana del tablero que capta sus eventos
"""
player_1 = {"name": "Maria", "value": "X"}
player_2 = {"name": "Marta", "value": "O"}
turn = cycle([player_1, player_2])
......@@ -25,6 +32,10 @@ def loop():
if event.startswith("cell"):
player = next(turn)
window[event].update(player["value"])
board_data, winner = board_handler.play(player, window, event, board_data)
if winner:
sg.popup(f"¡Felicitaciones! Ha ganado {player['name']}")
break
return window
WIN_PLAYS = [
[(0, 0), (1, 1), (2, 2)], # diagonal
[(0, 2), (1, 1), (2, 0)], # diagonal
[(0, 0), (0, 1), (0, 2)], # vertical
[(1, 0), (1, 1), (1, 2)], # vertical
[(2, 0), (2, 1), (2, 2)], # vertical
[(0, 0), (1, 0), (2, 0)], # horizontal
[(0, 1), (1, 1), (2, 1)], # horizontal
[(0, 2), (1, 2), (2, 2)], # horizontal
]
def play(player, window, event, board_data):
"""
Ejecuta una jugada sobre el tablero para un jugador:
- Actualiza el tablero visual
- Agrega el valor en board_data
- Chequea si hay un ganador
"""
window[event].update(player["value"])
_prefix, x, y = event.split("-")
board_data[int(x)][int(y)] = player["value"]
winner = check_win(board_data, player["value"])
return board_data, winner
def check_win(board, value):
"""
Chequea si se cumple alguna de las jugadas ganadoras
"""
for win_play in WIN_PLAYS:
if check_play(win_play, board, value):
return True
def check_play(win_play, board, value):
"""
Chequea la si una jugada ganadora esta completa
"""
# res = []
# for x, y in win_play:
# res.append(board[x][y] == value)
# return all(res)
return all(board[x][y] == value for x, y in win_play)
\ No newline at end of file
......@@ -2,6 +2,9 @@ import PySimpleGUI as sg
def build(player_1, player_2, board_data):
"""
Construye la ventana del tablero del juego
"""
layout = [
[sg.Text("Jugador 1: " + player_1["name"], key="-P1-", text_color="darkblue")],
[sg.Text("Jugador 2: " + player_2["name"], key="-P2-", text_color="white")],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment