Apuntes_Python/05_diez_proyectos/05-tic_tac_toe/player.py
2022-12-24 22:41:20 -03:00

85 lines
2.6 KiB
Python

import math
import random
class Player:
def __init__(self, bando):
# bando puedes ser X o O
self.bando = bando
# Asegurar que cada jugador haga su jugada
def get_move(delf, game):
pass
class RandomPcPlayer(Player):
def __init__(self, bando):
super().__init__(bando)
def get_move(self, game):
square = random.choice(game.available_moves())
return square
class HumanPlayer(Player):
def __init__(self, bando):
super().__init__(bando)
def get_move(self, game):
valid_square = False
val = None
while not valid_square:
square = input('Turno [' + self.bando + ']. Ingresa una jugada (1-9):')
# Validar entrada
try:
val = int(square)
val -= 1
if val not in game.available_moves():
raise ValueError
valid_square = True
except ValueError:
print('Movimiento no valido. Intenta otra vez.')
return val
class GeniusPcPlayer(Player):
def __init__(self, bando):
super().__init__(bando)
def get_move(self, game):
if len(game.available_moves()) == 9:
square = random.choice(game.available_moves())
else:
square = self.minimax(game, self.bando)['position']
return square
def minimax(self, state, player):
max_player = self.bando
other_player = 'O' if player == 'X' else 'X'
if state.current_winner == other_player:
return {'position':None,
'score': 1 * (state.num_empty_squares() + 1) if other_player == max_player else -1 * (
state.num_empty_squares() + 1)
}
elif not state.empty_squares():
return {'position': None, 'score': 0}
if player == max_player:
best = {'position': None, 'score': -math.inf}
else:
best = {'position': None, 'score': math.inf}
for possible_move in state.available_moves():
state.make_move(possible_move, player)
sim_score = self.minimax(state, other_player)
state.board[possible_move] = ' '
state.current_winner = None
sim_score['position'] = possible_move
if player == max_player:
if sim_score['score'] > best['score']:
best = sim_score
else:
if sim_score['score'] < best['score']:
best = sim_score
return best