Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
python2021
Code
ps_gui_example
Commits
756da7f6
Commit
756da7f6
authored
Apr 18, 2021
by
Fedrico Otaran
Browse files
Agrego un handler para chequear si hay ganador en cada jugada
parent
05c27303
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/component/board.py
View file @
756da7f6
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
src/handlers/board.py
0 → 100644
View file @
756da7f6
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
src/windows/board.py
View file @
756da7f6
...
...
@@ -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"
)],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment