23
23
1 - Indica que es una casilla ocupada por una tirada del Humano
24
24
-1 - Indica que es una casilla ocupara por una tirada de la IA
25
25
"""
26
+
26
27
import random
27
28
28
29
class Gato :
29
30
def __init__ (self ):
30
31
self .tablero = [
31
- [0 ,0 , 0 ],
32
- [0 ,0 , 0 ],
33
- [0 ,0 , 0 ]
32
+ [0 , 0 , 0 ],
33
+ [0 , 0 , 0 ],
34
+ [0 , 0 , 0 ]
34
35
]
35
36
self .humano = 1
36
37
self .ia = - 1
@@ -45,19 +46,19 @@ def imprime_tablero(self):
45
46
46
47
def humano_elige (self ):
47
48
print ()
48
- resp = input ("Humano elige 'x' o 'o'? " )
49
- if resp .lower () == 'x' :
49
+ resp = input ("Elige 'x' o 'o' ? " )
50
+ if resp .lower () == "x" :
50
51
self .simbs = ["." , "x" , "o" ]
51
52
else :
52
53
self .simbs = ["." , "o" , "x" ]
53
54
print ()
54
55
55
- def tirada (self , p , jugador ):
56
- self .tablero [p [0 ]][p [1 ]] = jugador
56
+ def tirada (self , p , j ):
57
+ self .tablero [p [0 ]][p [1 ]] = j
57
58
58
59
def juega_humano (self ):
59
60
print ()
60
- resp = input ("Turno del humano, elige casilla (ren, col)? " )
61
+ resp = input ("Turno humano (ren, col) ? " )
61
62
p = [int (v ) for v in resp .split ("," )]
62
63
self .tirada (p , self .humano )
63
64
print ()
@@ -68,89 +69,76 @@ def casillas_libres(self):
68
69
if self .tablero [i ][j ] == 0 ]
69
70
70
71
def juega_ia (self ):
71
- print ()
72
- print ("Turno de la IA!" )
72
+ print ("\n Turno de la IA!\n " )
73
73
casillas = self .casillas_libres ()
74
74
75
- tablero_aux = [ren [:] for ren in self .tablero ]
76
75
for c in casillas :
77
76
self .tirada (c , self .ia )
78
77
if self .gana (self .ia ):
79
78
return
80
79
else :
81
- self .tablero = [ ren [:] for ren in tablero_aux ]
80
+ self .tirada ( c , 0 )
82
81
for c in casillas :
83
82
self .tirada (c , self .humano )
84
83
if self .gana (self .humano ):
85
84
self .tirada (c , self .ia )
86
85
return
87
86
else :
88
- self .tablero = [ren [:] for ren in tablero_aux ]
89
-
87
+ self .tirada (c , 0 )
90
88
for c in casillas :
91
89
if c [0 ] in [0 , 2 ] and c [1 ] in [0 , 2 ]:
92
90
self .tirada (c , self .ia )
93
91
return
94
-
95
92
if (1 , 1 ) in casillas :
96
93
self .tirada ((1 , 1 ), self .ia )
97
94
return
98
95
99
96
self .tirada (casillas [0 ], self .ia )
100
- print ()
101
97
102
- def gana (self , jugador ):
103
-
98
+
99
+
100
+ def gana (self , j ):
104
101
for ren in self .tablero :
105
- if sum (ren ) == 3 * jugador :
102
+ if sum (ren ) == 3 * j :
106
103
return True
107
104
for col in zip (* self .tablero ):
108
- if sum (col ) == 3 * jugador :
105
+ if sum (col ) == 3 * j :
109
106
return True
110
107
diag1 = [ren [i ] for i , ren in enumerate (self .tablero )]
111
- if sum (diag1 ) == 3 * jugador :
108
+ if sum (diag1 ) == 3 * j :
112
109
return True
113
110
diag2 = [ren [2 - i ] for i , ren in enumerate (self .tablero )]
114
- if sum (diag2 ) == 3 * jugador :
111
+ if sum (diag2 ) == 3 * j :
115
112
return True
116
113
117
- return False
118
-
119
114
def imprime_resultado (self ):
120
115
print ()
121
116
self .imprime_tablero ()
122
117
print ()
123
118
if self .gana (self .humano ):
124
- print ("El humano gana !" )
119
+ print ("El humano ha ganado !" )
125
120
elif self .gana (self .ia ):
126
121
print ("La IA gana!" )
127
122
else :
128
123
print ("GATOOOOO!" )
129
- print ()
130
-
131
- def es_gato (self ):
132
- return False if self .casillas_libres () else True
133
124
134
125
def run (self ):
135
126
136
127
self .humano_elige ()
137
128
turno = random .choice ([self .humano , self .ia ])
138
- while not self .fin_de_juego and not self . es_gato () :
129
+ while not self .fin_de_juego :
139
130
self .imprime_tablero ()
140
131
if turno == self .humano :
141
132
self .juega_humano ()
142
133
if self .gana (self .humano ):
143
134
self .fin_de_juego = True
144
- else :
145
- turno = self .ia
135
+ turno = self .ia
146
136
else :
147
137
self .juega_ia ()
148
138
if self .gana (self .ia ):
149
139
self .fin_de_juego = True
150
- else :
151
- turno = self .humano
140
+ turno = self .humano
152
141
self .imprime_resultado ()
153
142
154
-
155
143
gato = Gato ()
156
144
gato .run ()
0 commit comments