1+ import time
2+ from os import system
3+ ## import time
4+ #from os import system
5+
6+ turno = ["X" , "0" ]
7+
8+ tablero = [
9+ [" " , " " , " " ],
10+ [" " , " " , " " ],
11+ [" " , " " , " " ],
12+ ]
13+
14+ def mostrar_tablero_cordenadas ():
15+ tablero_ini = '''Tablero con cordenadas validas:
16+ ["1,1", "1,2", "1,3"],
17+ ["2,1", "2,2", "2,3"],
18+ ["3,1", "3,2", "3,3"],
19+ '''
20+ print (tablero_ini )
21+
22+
23+
24+ def mostrar_tablero ():
25+
26+ print ("\n 1 2 3" )
27+ f = 0
28+ for fila in tablero :
29+ f = f + 1
30+ print (f ,fila )
31+
32+ def actualizar_tablero (posicion , jugador ):
33+ tablero [posicion [0 ]][posicion [1 ]] = jugador
34+
35+ def procesar_posicion (posicion ):
36+ fila , columna = posicion .split ("," )
37+ return [int (fila )- 1 , int (columna )- 1 ]
38+
39+ def posicion_correcta (posicion ):
40+ if 0 <= posicion [0 ] <= 2 and 0 <= posicion [1 ] <= 2 :
41+ if tablero [posicion [0 ]][posicion [1 ]] == " " :
42+ return True
43+ return False
44+
45+ def ha_ganado (j ):
46+ #compara las filas del tablero
47+ if tablero [0 ] == [j ,j ,j ] or tablero [1 ] == [j ,j ,j ] or tablero [2 ] == [j ,j ,j ]:
48+ return True
49+ #compara las columnas
50+ elif tablero [0 ][0 ] == j and tablero [1 ][0 ] == j and tablero [2 ][0 ] == j :
51+ return True
52+ elif tablero [0 ][1 ] == j and tablero [1 ][1 ] == j and tablero [2 ][1 ] == j :
53+ return True
54+ elif tablero [0 ][2 ] == j and tablero [1 ][2 ] == j and tablero [2 ][2 ] == j :
55+ return True
56+ #compara las diagonales
57+ elif tablero [0 ][0 ] == j and tablero [1 ][1 ] == j and tablero [2 ][2 ] == j :
58+ return True
59+ elif tablero [0 ][2 ] == j and tablero [1 ][1 ] == j and tablero [2 ][0 ] == j :
60+ return True
61+ return False
62+
63+ def siguiente_turno (lista_jugadas ):
64+ from random import randint
65+ fila = randint (1 , 9 )
66+ jugador = '0'
67+ cpu = 1
68+ jugador3 = 'CPU'
69+
70+ pre , lista_jugadas = guardar_jugada (fila , lista_jugadas )
71+ if pre :
72+ tabla = {1 :(0 ,0 ) , 2 :(0 ,1 ) , 3 :(0 ,2 ), 4 :(1 ,0 ) , 5 :(1 ,1 ) , 6 :(1 ,2 ), 7 :(2 ,0 ), 8 :(2 ,1 ) , 9 :(2 ,2 )}
73+ num = tabla .get (fila )
74+ num = list (num )
75+ posicion_l = num
76+ actualizar_tablero (posicion_l , '0' )
77+ time .sleep (0.5 ) # Sleep for 3 seconds
78+ print ("Turno CPU" )
79+ time .sleep (0.5 ) # Sleep for 3 seconds
80+ mostrar_tablero ()
81+ print ('\n ' )
82+ time .sleep (1 ) # Sleep for 3 seconds
83+ else :
84+ siguiente_turno (lista_jugadas )
85+ if ha_ganado (jugador ):
86+ print ("1 {} ha ganado!!!" .format (jugador3 ))
87+ imprimir_per ()
88+ cpu = 1
89+ return cpu
90+
91+ def num_jugada (jugada ):
92+ tabla = {1 :(0 ,0 ) , 2 :(0 ,1 ) , 3 :(0 ,2 ), 4 :(1 ,0 ) , 5 :(1 ,1 ) , 6 :(1 ,2 ), 7 :(2 ,0 ), 8 :(2 ,1 ) , 9 :(2 ,2 )}
93+
94+ for key , value in tabla .items ():
95+ if list (value ) == jugada :
96+ return (key )
97+
98+ def guardar_jugada (num ,lista_jugadas ):
99+ x = 0
100+ for numero in lista_jugadas :
101+ if numero == num :
102+ x = 1
103+ if x == 1 :
104+ lista_jugadas .remove (num )
105+ return (True ,lista_jugadas )
106+ else :
107+ return (False ,lista_jugadas )
108+
109+ def imprimir_log ():
110+ print ('''
111+ ( ) ( )
112+ )\))( ( ( /( ( ( )\ ) ) ( /(
113+ ((_)()\ )\ ( )\()) ))\ )( (()/( ( /( )\()) (
114+ (_()((_)((_) )\ (_))/ /((_)(()\ /(_))_ )(_))(_))/ )\
115+ | \/ | (_)((_)| |_ (_)) ((_) (_)) __|((_)_ | |_ ((_)
116+ | |\/| | | |(_-<| _|/ -_) | '_| | (_ |/ _` || _|/ _ \
117+ |_| |_| |_|/__/ \__|\___| |_| \___|\__,_| \__|\___/ ''' )
118+ input ('Pulse enter para CONTINUAR.....' )
119+ system ("clear" )
120+ return
121+
122+ def imprimir_vic ():
123+ print ('''
124+ _._ _,-'""`-._
125+ (,-.`._,'( |\`-/|
126+ `-.-' \ )-`( , o o)
127+ `- \`_`"'-
128+ )
129+ ( ( ( ( /( ( ( )
130+ )\ )\ )\ ( )\()) ( )( )\ ( /(
131+ ((_)((_)((_) )\ (_))/ )\ (()\((_) )(_))
132+ \ \ / / (_) ((_)| |_ ((_) ((_)(_)((_)_
133+ \ V / | |/ _| | _|/ _ \| '_|| |/ _` |
134+ \_/ |_|\__| \__|\___/|_| |_|\__,_|''' )
135+ #input('Pulse enter para CONTINUAR.....')
136+ #system("clear")
137+ return
138+
139+ def imprimir_per ():
140+ print ('''\n |\__/,|
141+ (`\
142+ _.|o o |_ ) )
143+ -(((---(((--------
144+ (
145+ )\ ) ( )
146+ (()/( ( ( )\ ) ( ( /( (
147+ /(_)) ))\ )( (()/( )\ ( )\()) ))\
148+ (_)) /((_)(()\ ((_))((_) )\ (_))/ /((_)
149+ | _ \(_)) ((_) _| | (_)((_)| |_ (_))
150+ | _// -_) | '_|/ _` | | |(_-<| _|/ -_)
151+ |_| \___| |_| \__,_| |_|/__/ \__|\___| ''' )
152+ #input('Pulse enter para CONTINUAR.....')
153+ #system("clear")
154+ return
155+
156+ def imprimir_emp ():
157+ print ('''\n |\ _,,,---,,_
158+ ZZZzz /,`.-'`' -. ;-;;,_
159+ |,4- ) )-,_. ,\ ( `'-'
160+ '---''(_/--' `-'\_)
161+ )
162+ ( ) ) ( /( (
163+ )\ ( ` ) ( /( )\()) ))\
164+ ((_) )\ ' /(/( )(_))(_))/ /((_)
165+ | __| _((_)) ((_)_\ ((_)_ | |_ (_))
166+ | _| | ' \()| '_ \)/ _` || _|/ -_)
167+ |___||_|_|_| | .__/ \__,_| \__|\___|
168+ |_| ''' )
169+ #input('Pulse enter para CONTINUAR.....')
170+ #system("clear")
171+ return
172+
173+ def imprimir_log2 ():
174+ print ('''\n ░░░▄▀▌░▄▀▌░░░░░░░░░░░░
175+ ░▄██▀▀▀█▀▀▀▄╔╦╗╔╗╔╗╗╗╗
176+ ▐███░▐░█░▐░█║║║╠╝║║║║║
177+ ███████╥████╝╝╝╚╝╚╝╩╩╝
178+ █████╚═╩═╝██░░░░░░░░░░''' )
179+ input ('Pulse enter para CONTINUAR.....' )
180+ system ("clear" )
181+ return
182+
183+ def juego ():
184+ system ("clear" )
185+ imprimir_log2 ()
186+ mostrar_tablero_cordenadas ()
187+ print ('\n ' )
188+ jugador2 = input ('\n Ingresa nombre del jugador \t ' )
189+ mostrar_tablero ()
190+ lista_jugadas = [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ]
191+ lista_jugadas_guardadas = []
192+ x = "X"
193+ jugador = 'X'
194+ cpu = 0
195+ while True :
196+ posicion = input ("\n Turno {}, elige una posicion (fila, columna) de 1 a 3. 'salir' para salir \t " .format (jugador2 ))
197+ posicion = posicion .lower ()
198+ if posicion == 'salir' :
199+ print ("Adios!!!" )
200+ break
201+ try :
202+ posicion_l = procesar_posicion (posicion )
203+ except :
204+ print ("Error, posicion {} no es válida. " .format (posicion ))
205+ continue
206+ if posicion_correcta (posicion_l ):
207+ actualizar_tablero (posicion_l , jugador )
208+ mostrar_tablero ()
209+ time .sleep (1 ) # Sleep for 3 seconds
210+ print ('\n ' )
211+ num = num_jugada (posicion_l )
212+ pre ,lista_jugadas = guardar_jugada (num ,lista_jugadas )
213+ if len (lista_jugadas ) == 0 :
214+ if ha_ganado (jugador ):
215+ print ("2 {} ha ganado!!!" .format (jugador2 ))
216+ imprimir_vic ()
217+ break
218+ else :
219+ print ("Empate" )
220+ imprimir_emp ()
221+ break
222+ if ha_ganado (jugador ):
223+ print ("3 {} ha ganado!!!" .format (jugador2 ))
224+ imprimir_vic ()
225+ break
226+ cpu = siguiente_turno (lista_jugadas )
227+ if cpu == 1 :
228+ break
229+
230+
231+ else :
232+ print ("Posicion {} no válida" .format (posicion ))
233+
234+ juego ()
0 commit comments