-
Lenguaje
Python
-
Descripción
Juego conocido como TicTacToe, Gato, Triqui, Tres en Raya o TaTeTi. Usario juega con el caracter 'X' vs Computadora que juega con el caracter 'O'.
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
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
# Este codigo esta programado para la version 2.7.12 en plataforma Linux.
#
# La sintaxis de python es variable en cada version, por lo que no se
# recomienda copiar este codigo ya que es muy probable que no funcione y
# tenga que hacerle ajustes.
import os, random, sys
def getch ():
if os.name == 'posix':
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw (sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr (fd, termios.TCSADRAIN, old_settings)
return ch
else:
import msvcrt, sys
sys.stdout.flush()
return msvcrt.getwch()
def imprimir_cuadricula (tictactoe):
os.system ('clear' if os.name == 'posix' else 'cls')
print (u' \u2554'.encode('utf-8') + '{0}{0}{0}{1}{0}{0}{0}{1}{0}{0}{0}'.format (u'\u2550'.encode('utf-8'), u'\u2566'.encode('utf-8')) + u'\u2557'.encode('utf-8'))
print (' {0} 1 {0} 2 {0} 3 {0}'.format(u'\u2551'.encode('utf-8')))
print (u' \u255A'.encode('utf-8') + '{0}{0}{0}{1}{0}{0}{0}{1}{0}{0}{0}'.format (u'\u2550'.encode('utf-8'), u'\u2569'.encode('utf-8')) + u'\u255D\n'.encode('utf-8'))
print ('{1}{0}{3} {1}{0}{0}{0}{2}{0}{0}{0}{2}{0}{0}{0}{3}'.format (u'\u2550'.encode('utf-8'), u'\u2554'.encode('utf-8'), u'\u2566'.encode('utf-8'), u'\u2557'.encode('utf-8')))
for i in range (len (tictactoe)):
if i != 0:
print ('{1}{0}{3} {1}{0}{0}{0}{2}{0}{0}{0}{2}{0}{0}{0}{3}'.format (u'\u2550'.encode('utf-8'), u'\u2560'.encode('utf-8'), u'\u256C'.encode('utf-8'), u'\u2563'.encode('utf-8')))
sys.stdout.write ('{0}{1}{0} {0}'.format (u'\u2551'.encode('utf-8'), i+1))
for j in range (len (tictactoe[i])):
sys.stdout.write (' ' + tictactoe[i][j] + u' \u2551'.encode('utf-8'))
print ('')
print ('{1}{0}{3} {1}{0}{0}{0}{2}{0}{0}{0}{2}{0}{0}{0}{3}\n'.format (u'\u2550'.encode('utf-8'), u'\u255A'.encode('utf-8'), u'\u2569'.encode('utf-8'), u'\u255D'.encode('utf-8')))
def leer_numero (variable):
sys.stdout.write (u'Seleccione el n\u00FAmero de '.encode('utf-8') + variable + ': ')
sys.stdout.flush()
tecla = ' '
while tecla != '1' and tecla != '2' and tecla != '3':
tecla = getch()
print (tecla)
return int (tecla) - 1
tictactoe = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
ganador = ' '
juega_cpu = random.randrange (0, 2) == 1
turnos = 0
while turnos<9 and ganador!='O' and ganador!='X':
if juega_cpu:
i = random.randrange (0, 3)
j = random.randrange (0, 3)
while tictactoe[i][j]!=' ':
i = random.randrange (0, 3)
j = random.randrange (0, 3)
tictactoe[i][j] = 'O'
else:
imprimir_cuadricula (tictactoe)
while True:
i = leer_numero (u'rengl\u00F3n'.encode('utf-8'))
j = leer_numero ('columna')
if tictactoe[i][j]!=' ':
print (u'La casilla seleccionada ya est\u00E1 ocupada.\n'.encode('utf-8'))
else:
break
tictactoe[i][j] = 'X'
i = 0
while ganador==' ' and i<3:
if tictactoe[i][0]!=' ' and tictactoe[i][0]==tictactoe[i][1] and tictactoe[i][1]==tictactoe[i][2]:
ganador = tictactoe[i][0]
i += 1
i = 0
while ganador==' ' and i<3:
if tictactoe[0][i]!=' ' and tictactoe[0][i]==tictactoe[1][i] and tictactoe[1][i]==tictactoe[2][i]:
ganador = tictactoe[0][i]
i += 1
if ganador==' ' and tictactoe[0][0]!=' ' and tictactoe[0][0]==tictactoe[1][1] and tictactoe[1][1]==tictactoe[2][2]:
ganador = tictactoe[0][0]
if ganador==' ' and tictactoe[0][2]!=' ' and tictactoe[0][2]==tictactoe[1][1] and tictactoe[1][1]==tictactoe[2][0]:
ganador = tictactoe[0][2]
juega_cpu = not juega_cpu
turnos += 1
imprimir_cuadricula (tictactoe)
if ganador=='O':
print ('La computadora ha ganado.')
elif ganador=='X':
print ('El usuario ha ganado.')
elif ganador==' ':
print ('Empate.')
#
# La sintaxis de python es variable en cada version, por lo que no se
# recomienda copiar este codigo ya que es muy probable que no funcione y
# tenga que hacerle ajustes.
import os, random, sys
def getch ():
if os.name == 'posix':
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw (sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr (fd, termios.TCSADRAIN, old_settings)
return ch
else:
import msvcrt, sys
sys.stdout.flush()
return msvcrt.getwch()
def imprimir_cuadricula (tictactoe):
os.system ('clear' if os.name == 'posix' else 'cls')
print (u' \u2554'.encode('utf-8') + '{0}{0}{0}{1}{0}{0}{0}{1}{0}{0}{0}'.format (u'\u2550'.encode('utf-8'), u'\u2566'.encode('utf-8')) + u'\u2557'.encode('utf-8'))
print (' {0} 1 {0} 2 {0} 3 {0}'.format(u'\u2551'.encode('utf-8')))
print (u' \u255A'.encode('utf-8') + '{0}{0}{0}{1}{0}{0}{0}{1}{0}{0}{0}'.format (u'\u2550'.encode('utf-8'), u'\u2569'.encode('utf-8')) + u'\u255D\n'.encode('utf-8'))
print ('{1}{0}{3} {1}{0}{0}{0}{2}{0}{0}{0}{2}{0}{0}{0}{3}'.format (u'\u2550'.encode('utf-8'), u'\u2554'.encode('utf-8'), u'\u2566'.encode('utf-8'), u'\u2557'.encode('utf-8')))
for i in range (len (tictactoe)):
if i != 0:
print ('{1}{0}{3} {1}{0}{0}{0}{2}{0}{0}{0}{2}{0}{0}{0}{3}'.format (u'\u2550'.encode('utf-8'), u'\u2560'.encode('utf-8'), u'\u256C'.encode('utf-8'), u'\u2563'.encode('utf-8')))
sys.stdout.write ('{0}{1}{0} {0}'.format (u'\u2551'.encode('utf-8'), i+1))
for j in range (len (tictactoe[i])):
sys.stdout.write (' ' + tictactoe[i][j] + u' \u2551'.encode('utf-8'))
print ('')
print ('{1}{0}{3} {1}{0}{0}{0}{2}{0}{0}{0}{2}{0}{0}{0}{3}\n'.format (u'\u2550'.encode('utf-8'), u'\u255A'.encode('utf-8'), u'\u2569'.encode('utf-8'), u'\u255D'.encode('utf-8')))
def leer_numero (variable):
sys.stdout.write (u'Seleccione el n\u00FAmero de '.encode('utf-8') + variable + ': ')
sys.stdout.flush()
tecla = ' '
while tecla != '1' and tecla != '2' and tecla != '3':
tecla = getch()
print (tecla)
return int (tecla) - 1
tictactoe = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
ganador = ' '
juega_cpu = random.randrange (0, 2) == 1
turnos = 0
while turnos<9 and ganador!='O' and ganador!='X':
if juega_cpu:
i = random.randrange (0, 3)
j = random.randrange (0, 3)
while tictactoe[i][j]!=' ':
i = random.randrange (0, 3)
j = random.randrange (0, 3)
tictactoe[i][j] = 'O'
else:
imprimir_cuadricula (tictactoe)
while True:
i = leer_numero (u'rengl\u00F3n'.encode('utf-8'))
j = leer_numero ('columna')
if tictactoe[i][j]!=' ':
print (u'La casilla seleccionada ya est\u00E1 ocupada.\n'.encode('utf-8'))
else:
break
tictactoe[i][j] = 'X'
i = 0
while ganador==' ' and i<3:
if tictactoe[i][0]!=' ' and tictactoe[i][0]==tictactoe[i][1] and tictactoe[i][1]==tictactoe[i][2]:
ganador = tictactoe[i][0]
i += 1
i = 0
while ganador==' ' and i<3:
if tictactoe[0][i]!=' ' and tictactoe[0][i]==tictactoe[1][i] and tictactoe[1][i]==tictactoe[2][i]:
ganador = tictactoe[0][i]
i += 1
if ganador==' ' and tictactoe[0][0]!=' ' and tictactoe[0][0]==tictactoe[1][1] and tictactoe[1][1]==tictactoe[2][2]:
ganador = tictactoe[0][0]
if ganador==' ' and tictactoe[0][2]!=' ' and tictactoe[0][2]==tictactoe[1][1] and tictactoe[1][1]==tictactoe[2][0]:
ganador = tictactoe[0][2]
juega_cpu = not juega_cpu
turnos += 1
imprimir_cuadricula (tictactoe)
if ganador=='O':
print ('La computadora ha ganado.')
elif ganador=='X':
print ('El usuario ha ganado.')
elif ganador==' ':
print ('Empate.')