def __init__(self, search_depth=2, timeout=50., heuristic=heuristic_1): self.game = Quixo() self.player = None self.timer_threshold = timeout self.search_depth = search_depth self.heuristic = heuristic self.time_left = None
def test_the_board_is_created_empty(self): expected_empty_structure = [['W', 'W', 'W', 'W', 'W'], ['W', 'W', 'W', 'W', 'W'], ['W', 'W', 'W', 'W', 'W'], ['W', 'W', 'W', 'W', 'W'], ['W', 'W', 'W', 'W', 'W']] self.assertEqual(to_simple_structure(Quixo()), expected_empty_structure)
def to_game(matrix): quixo = Quixo() for i_row in range(len(matrix)): for i_column in range(len(matrix[i_row])): cell = quixo.board[i_row][i_column] current_value = matrix[i_row][i_column] cell.update_symbol('empty' if current_value == 'W' else current_value) return quixo
from quixo import Quixo from quixo import QuixoGame if __name__ == '__main__': g = QuixoGame() alphabeta = Quixo() result = alphabeta.alphabeta(g, 1, 10, -10, 1) g.print_board() print(result)
from six.moves import input from quixo import InvalidMove from quixo import Quixo if __name__ == '__main__': turn = 1 quixo = Quixo() while True: quixo.game.print_board() turn_name = 'o' if turn == 1 else 'x' if turn == 1: row = input(f'Humano {turn_name} saca ficha: ') reinsert = input(f'Humano {turn_name} mete en: ') try: quixo.opponentPlay((int(row), int(reinsert))) except InvalidMove as e: print(e.message) continue else: quixo.playerPlay() if quixo.game.get_winner(): quixo.game.print_board() print(f'Ganó {quixo.game.get_winner()}') break turn *= -1
class QuixoPlayer: def __init__(self, search_depth=2, timeout=50., heuristic=heuristic_1): self.game = Quixo() self.player = None self.timer_threshold = timeout self.search_depth = search_depth self.heuristic = heuristic self.time_left = None def set_player(self, player): self.player = player def playerPlay(self): if self.player == None: self.set_player('X') move_start = time_millis() # time_left represent the left time, calculating the passed time (time_millis() - move_start) time_left = lambda: self.timer_threshold - (time_millis() - move_start) focus, target = self.get_move(time_left) self.game.apply_move(self.player, (focus, target)) return focus + 1, target + 1 # To support the standard order def oponentPlay(self, move): focus, target = move if self.player == None: self.set_player('O') oponent_player = 'X' if self.player == 'O' else 'O' if not (focus - 1, target - 1) in self.game.all_valid_moves(oponent_player): raise Exception('Invalid move') self.game.apply_move( oponent_player, (focus - 1, target - 1)) # To support the standard order def is_time_over(self): if self.time_left() <= 0: raise SearchTimeout() def get_move(self, time_left): self.time_left = time_left best_move = self.game.all_valid_moves(self.player)[0] depth = 1 try: while (depth <= self.search_depth): move = self.alphabeta(self.game, depth, self.player) best_move = move depth += 1 if self.time_left() <= 0: return best_move return best_move except SearchTimeout: return best_move def alphabeta(self, game, depth, player, alpha=-inf, beta=inf): if player == 'X': legal_moves = game.all_valid_moves('X') if not legal_moves: return (-1, -1) best_current_move = legal_moves[0] for move in legal_moves: self.is_time_over() child = deepcopy(game) child.apply_move('X', move) score = self.max_value(child, depth - 1, alpha, beta) if score > alpha: alpha = score best_current_move = move return best_current_move else: legal_moves = game.all_valid_moves('O') if not legal_moves: return (-1, -1) best_current_move = legal_moves[0] for move in legal_moves: self.is_time_over() child = deepcopy(game) child.apply_move('O', move) score = self.min_value(child, depth - 1, alpha, beta) if score < beta: beta = score best_current_move = move return best_current_move def min_value(self, game, depth, alpha, beta): if depth == 0: return self.heuristic(game) legal_moves = game.all_valid_moves('X') for move in legal_moves: self.is_time_over() child = deepcopy(game) child.apply_move('X', move) score = self.max_value(child, depth - 1, alpha, beta) if score < beta: beta = score if beta <= alpha: break return beta def max_value(self, game, depth, alpha, beta): if depth == 0: return self.heuristic(game) legal_moves = game.all_valid_moves('O') for move in legal_moves: self.is_time_over() child = deepcopy(game) child.apply_move('O', move) score = self.min_value(child, depth - 1, alpha, beta) if score > alpha: alpha = score if alpha >= beta: break return alpha
from quixo import Quixo from match import Match import numpy as np game = Quixo(5, 5) state = game.initial_state() match = Match(game, state, 20) match.set_players() match.start() # # count = 100 # wincount = [0, 0] # draw = 0 # for i in range(count): # game = Quixo(5, 5) # # state = game.initial_state() # # match = Match(game, state, 20) # match.playersvs("random", "random") # match.start()
from quixo import Easy, Quixo if __name__ == '__main__': while True: plays = 1 hardPlayer = Quixo() # 1 | o easyPlayer = Quixo(Easy) # -1 | x res_dic = { 'o': 'Hard', 'x': 'Easy', 'Draw': 'Nadie' } turn = 1 while True: # hardPlayer.game.print_board() if turn == 1: move = hardPlayer.playerPlay() easyPlayer.opponentPlay(move) else: move = easyPlayer.playerPlay() hardPlayer.opponentPlay(move) turn = turn * -1 plays +=1 if hardPlayer.game.get_winner(): hardPlayer.game.print_board() winner = hardPlayer.game.get_winner() print(f'Ganador: {winner} ({res_dic[winner]}) en {plays} jugadas') break