-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_txt.py
150 lines (124 loc) · 5.93 KB
/
main_txt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Author: Jose L Balcazar, ORCID 0000-0003-4248-4528
Copyleft: MIT License (https://en.wikipedia.org/wiki/MIT_License)
Text handling of a simple tic-tac-toe-like board, 2021.
Intended for Grau en Intel-ligencia Artificial, Programacio i Algorismes 1.
"""
# Import initialization of the separately programmed abstract board:
from abs_board_h import set_board_up
# Colores
from constants import COLORES
# Importar tiempo
import time
# Limpieza consola
import os
# Prepare board:
# this will set up all stones as unplayed, select a first stone to play,
# and obtain functions to handle them as follows:
# the call stones() allows one to loop on all stones,
# the call select_st(i, j) marks as selected the stone at these coordinates,
# the call move_st(i, j)
# if the square at these coordinates is free, moves the selected
# stone there, changes player, unselects the stone and checks for
# end of game; otherwise, does nothing, leaving the stone selected;
# returns: bool "stone still selected", next player (may be the same),
# and bool "end of game"
# the call to draw_txt(end) prints a text-based version of the board
def clear():
# Limpia la pantalla de la consola en windows o linux
os.system('cls' if os.name == 'nt' else 'clear')
def print_header():
# Imprime el encabezado del juego
print(f"\n{COLORES['MAGENTA']}╔══════════════════════╗{COLORES['RESET']}")
print(f"{COLORES['MAGENTA']}║ TIC TAC TOE ║{COLORES['RESET']}")
print(f"{COLORES['MAGENTA']}╚══════════════════════╝{COLORES['RESET']}\n")
def print_turn_indicator(player):
# Imprime el indicador del turno del jugador actual
symbol = f"{COLORES['AZUL']}X{COLORES['RESET']}" if player == "x" else f"{COLORES['ROJO']}O{COLORES['RESET']}"
print(f"{COLORES['MAGENTA']}▶ Turn of {COLORES['RESET']} {symbol}")
def print_input_prompt(message):
# Imprime un mensaje de solicitud de entrada para el usuario
print(f"{COLORES['MAGENTA']}► {message}{COLORES['RESET']}", end=" ")
def play_again():
# Pregunta al usuario si quiere jugar otra vez, y si juega, reinicia el juego, si no imprime el mensaje de despedida
while True:
print(f"\n{COLORES['MAGENTA']}Do you want to play again? (y/n):{COLORES['RESET']} ", end="")
response = input().lower()
clear()
if response in ['y', 'n']:
return response == 'y'
print(f"{COLORES['ROJO']}Please enter 'y' for yes or 'n' for no.{COLORES['RESET']}")
# Animación de carga del juego
def spinner_bar():
for i in range(21):
clear()
progress = f"{COLORES['AMARILLO']}▰" * i + f"{COLORES['BLANCO']}▱" * (20 - i) + f"{COLORES['AMARILLO']}"
print("\n" * 10) # Ajusta el número de líneas nuevas para centrar verticalmente
print(f"{COLORES['AMARILLO']}{' ' * 20}Cargando [{progress}] {i*5}%{COLORES['RESET']}")
print("\n" * 10) # Ajusta el número de líneas nuevas para centrar verticalmente
time.sleep(0.1)
clear()
# Añadido todo dentro de una función para poder reiniciar el juego las veces que sea necesario
def play_game():
spinner_bar()
# Prepare board
stones, select_st, move_st, draw_txt = set_board_up()
# Initial setup
stone_selected = True
curr_player_color = "X"
end = False
# Game loop
print_header()
draw_txt(end)
# El try y except sirven para que no pete el juego si no se introduce el valor correcto, y devuelve un mensaje de error, permite seguir jugando
while not end:
# Bucle para seleccionar la piedra
while not stone_selected:
print_turn_indicator(curr_player_color)
print_input_prompt("Select stone coordinates (row col):")
try:
i, j = input().split()
clear()
print_header()
stone_selected = select_st(int(i), int(j))
draw_txt(end)
except ValueError:
clear()
print_header()
# Mensaje de error si la entrada no es válida
print(f"{COLORES['ROJO']}✘ Invalid input! Please enter two numbers separated by space.{COLORES['RESET']}")
draw_txt(end)
continue
# Bucle para mover la piedra seleccionada
while stone_selected and not end:
print_turn_indicator(curr_player_color)
print_input_prompt("Select destination coordinates (row col):")
try:
i, j = input().split()
clear()
print_header()
stone_selected, curr_player, end = move_st(int(i), int(j))
draw_txt(end)
if not stone_selected:
# Cambiar el color del jugador actual
curr_player_color = "0" if curr_player_color == "x" else "x"
except ValueError:
clear()
print_header()
# Mensaje de error si la entrada no es válida
print(f"{COLORES['ROJO']}✘ Invalid input! Please enter two numbers separated by space.{COLORES['RESET']}")
draw_txt(end)
continue
# Bucle principal para ejecutar el juego, si se pregunta y no se quiere volver a jugar, imprime el mensaje de despedida y sale del juego.
while True:
clear()
play_game()
if not play_again():
clear()
print(f"\n{COLORES['MAGENTA']}Thank you for playing! See you soon!{COLORES['RESET']}\n")
break
print(f"\n{COLORES['MAGENTA']}╔══════════════════════╗{COLORES['RESET']}")
print(f"{COLORES['MAGENTA']}║ GAME OVER! ║{COLORES['RESET']}")
print(f"{COLORES['MAGENTA']}╚══════════════════════╝{COLORES['RESET']}")
input(f"\n{COLORES['MAGENTA']}Press Enter to continue...{COLORES['RESET']}")
clear()