-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToeBacktracking.py
110 lines (89 loc) · 3.05 KB
/
TicTacToeBacktracking.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
def is_winner(board, player):
# Comprobación de las filas
for i in range(3):
if all(board[i][j] == player for j in range(3)):
return True
# Comprobación de las columnas
for j in range(3):
if all(board[i][j] == player for i in range(3)):
return True
# Comprobación de las diagonales
if all(board[i][i] == player for i in range(3)):
return True
if all(board[i][2-i] == player for i in range(3)):
return True
return False
def is_board_full(board):
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
return False
return True
def print_board(board):
for i in range(3):
print("|", end="")
for j in range(3):
print(board[i][j], end="|")
print()
print("-------")
def play(board, player):
if is_winner(board, 'X'):
return -1
elif is_winner(board, 'O'):
return 1
elif is_board_full(board):
return 0
best_score = float('-inf') if player == 'X' else float('inf')
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = player
score = play(board, 'O' if player == 'X' else 'X')
board[i][j] = ' '
if player == 'X':
best_score = max(best_score, score)
else:
best_score = min(best_score, score)
return best_score
# Función principal
def tic_tac_toe():
board = [[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']]
print("¡Bienvenido al juego Tic Tac Toe!")
while True:
print_board(board)
row = int(input("Ingrese la fila (0-2): "))
col = int(input("Ingrese la columna (0-2): "))
if board[row][col] == ' ':
board[row][col] = 'X'
if is_winner(board, 'X'):
print_board(board)
print("¡Has ganado!")
break
elif is_board_full(board):
print_board(board)
print("¡Empate!")
break
best_score = float('-inf')
best_move = None
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'O'
score = play(board, 'X')
board[i][j] = ' '
if score > best_score:
best_score = score
best_move = (i, j)
board[best_move[0]][best_move[1]] = 'O'
if is_winner(board, 'O'):
print_board(board)
print("¡Has perdido!")
break
elif is_board_full(board):
print_board(board)
print("¡Empate!")
break
# Ejecución del juego
tic_tac_toe()