-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8. Tic-Tac-Toe.py
205 lines (183 loc) · 6.4 KB
/
8. Tic-Tac-Toe.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
import time
tablero = [
[' ', '|', ' ', '|', ' '],
['-', '+', '-', '+', '-'],
[' ', '|', ' ', '|', ' '],
['-', '+', '-', '+', '-'],
[' ', '|', ' ', '|', ' ']
]
#Titulo juego
def imprimir_titulo(titulo, caracter="*"):
print(caracter * len(titulo))
print(titulo)
print(caracter * len(titulo),)
print()
# print en formato tablero:
def imprimir_tablero(tablero):
for fila in tablero:
for elem in range(len(fila)):
if elem == len(fila) -1:
print(fila[elem], end='\n')
else:
print(fila[elem], end=' ')
#cambia el tablero segun jugadas
def cambiar_tablero(tablero, posicion, turno_jugador):
if turno_jugador:
simbolo = 'x'
else:
simbolo = 'o'
#ubicar simbolo x/o en la posicion que el jugador quiera:
if posicion == 1:
if tablero[4][0] == ' ':
tablero[4][0] = simbolo
return 0
else:
return 'Ese lugar esta ocupado'
elif posicion == 2:
if tablero[4][2] == ' ':
tablero[4][2] = simbolo
return 0
else:
return 'Ese lugar esta ocupado'
elif posicion == 3:
if tablero[4][4] == ' ':
tablero[4][4] = simbolo
return 0
else:
return 'Ese lugar esta ocupado'
elif posicion == 4:
if tablero[2][0] == ' ':
tablero[2][0] = simbolo
return 0
else:
return 'Ese lugar esta ocupado'
elif posicion == 5:
if tablero[2][2] == ' ':
tablero[2][2] = simbolo
return 0
else:
return 'Ese lugar esta ocupado'
elif posicion == 6:
if tablero[2][4] == ' ':
tablero[2][4] = simbolo
return 0
else:
return 'Ese lugar esta ocupado'
elif posicion == 7:
if tablero[0][0] == ' ':
tablero[0][0] = simbolo
return 0
else:
return 'Ese lugar esta ocupado'
elif posicion == 8:
if tablero[0][2] == ' ':
tablero[0][2] = simbolo
return 0
else:
return 'Ese lugar esta ocupado'
elif posicion == 9:
if tablero[0][4] == ' ':
tablero[0][4] = simbolo
return 0
else:
return 'Ese lugar esta ocupado'
else:
return 'Ese lugar está fuera del tablero'
def quien_gano(tablero):
#chequear fila seguida de simbolo en todas direcciones
for simbolo in ['x', 'o']:
fila_0 = tablero[0][0] == simbolo and tablero[0][2] == simbolo and tablero[0][4] == simbolo
fila_2 = tablero[2][0] == simbolo and tablero[2][2] == simbolo and tablero[2][4] == simbolo
fila_4 = tablero[4][0] == simbolo and tablero[4][2] == simbolo and tablero[4][4] == simbolo
columna_0 = tablero[0][0] == simbolo and tablero[2][0] == simbolo and tablero[4][0] == simbolo
columna_2 = tablero[0][2] == simbolo and tablero[2][2] == simbolo and tablero[4][2] == simbolo
columna_4 = tablero[0][4] == simbolo and tablero[2][4] == simbolo and tablero[4][4] == simbolo
diagonal_1 = tablero[4][0] == simbolo and tablero[2][2] == simbolo and tablero[0][4] == simbolo
diagonal_2 = tablero[4][4] == simbolo and tablero[2][2] == simbolo and tablero[0][0] == simbolo
if fila_0 or fila_2 or fila_4 or columna_0 or columna_2 or columna_4 or diagonal_1 or diagonal_2:
if simbolo == 'x':
return 1
elif simbolo == 'o':
return 2
break
turno1 = True
jugador_1 = ''
jugador_2 = ''
cant_turnos = 0
""" def resetear_tablero(tablero):
for simbolo in ['x', 'o']:
if tablero[4][0] == simbolo:
tablero[4][0] = ' '
elif tablero[4][2] == simbolo:
tablero[4][2] = ' '
elif tablero[4][4] == simbolo:
tablero[4][4] = ' '
elif tablero[2][0] == simbolo:
tablero[2][0] = ' '
elif tablero[2][2] == simbolo:
tablero[2][2] = ' '
elif tablero[2][4] == simbolo:
tablero[2][4] = ' '
elif tablero[0][0] == simbolo:
tablero[0][0] = ' '
elif tablero[0][2] == simbolo:
tablero[0][2] = ' '
elif tablero[0][4] == simbolo:
tablero[0][4] = ' '
cant_turnos = 0
def jugar_de_nuevo():
otra_vez = input("¿Jugamos de nuevo? [si / no] ")
if otra_vez.lower()=="si" or otra_vez.lower() == "s":
os.system('cls')
time.sleep(1)
resetear_tablero(tablero)
if otra_vez.lower() == "no" or otra_vez.lower() == "n":
print("Gracias por jugar.")
time.sleep(1)
os.system('cls')
exit()
"""
imprimir_titulo("Tatetí")
imprimir_tablero(tablero)
while cant_turnos < 9:
if jugador_1 == '':
print('El primer jugador utiliza "x" para jugar. Ingresa tu nombre:')
jugador_1 = input()
print('El segundo jugador utiliza "o" para jugar. Ingresa tu nombre:')
jugador_2 = input()
else:
if turno1:
print(f"{jugador_1} elegí una posicion entre 1-9 (el primer cuadrito de abajo es la posición 1)")
else:
print(f"{jugador_2} elegí una posicion entre 1-9 (el primer cuadrito de abajo es la posición 1)")
posiciones = int(input())
valor = cambiar_tablero(tablero, posiciones, turno1)
if valor == 0:
turno1 = not turno1
cant_turnos+=1
imprimir_tablero(tablero)
if quien_gano(tablero) == 1:
print(f"Gano {jugador_1}!")
time.sleep(1)
print("Gracias por jugar.")
time.sleep(1)
os.system('cls')
exit()
elif quien_gano(tablero) == 2:
print(f"Gano {jugador_2}!")
time.sleep(1)
print("Gracias por jugar.")
time.sleep(1)
os.system('cls')
exit()
else:
print(valor)
if cant_turnos == 9:
print("Empate...")
print("Gracias por jugar.")
time.sleep(1)
os.system('cls')
exit()
imprimir_tablero(tablero)
# jugar_de_nuevo()