def __init__(self, chessboard): self.chessboard = chessboard self.player_piece = { Player.HUMAN: ChessBoard.WHITE_PAWN, Player.AI: ChessBoard.BLACK_PAWN } self.move_validator = MoveValidator(self) self.turn = Player.HUMAN self.was_last_move_a_two_step_move = True self.position_of_two_steps_pawn = (4, 'C') self.game_engine = GameEngine(self) self.mini_max = MinMax(self, self.move_validator)
def __init__(self): self.jogo = (['*', '*', '*'], ['*', '*', '*'], ['*', '*', '*']) simbolos = ['X', 'O'] aux = int(input("Selecione 0 para \"X\" e 1 para \"O\" \n")) while (aux != 1 and aux != 0): aux = int(input("Selecione 0 para \"X\" e 1 para \"O\" \n")) self.simbJogador1 = simbolos[aux] self.simbJogador2 = simbolos[not (aux)] self.estadoAtual = No(True, self.jogo) self.minMax = MinMax() self.estadoAtual.imprimir()
def main(): team_ID = int(input('Team ID<< ')) match_ID = input('Match ID<< ') while True: data = json.loads( sp.check_output([ 'curl', '-H', 'Authorization: ' + TOKEN, 'http://' + PATH + match_ID ]).decode()) # print(data) minmax = MinMax(data, team_ID) send(match_ID, minmax.get_action()) time.sleep(3)
class JogoDaVelha: def __init__(self): self.jogo = (['*', '*', '*'], ['*', '*', '*'], ['*', '*', '*']) simbolos = ['X', 'O'] aux = int(input("Selecione 0 para \"X\" e 1 para \"O\" \n")) while (aux != 1 and aux != 0): aux = int(input("Selecione 0 para \"X\" e 1 para \"O\" \n")) self.simbJogador1 = simbolos[aux] self.simbJogador2 = simbolos[not (aux)] self.estadoAtual = No(True, self.jogo) self.minMax = MinMax() self.estadoAtual.imprimir() def acabou(self): return self.estadoAtual.ganhou() or self.estadoAtual.testeTermino() def menu(self): auxY = int(input("Selecione a linha ao qual deseja jogar \n")) while (auxY < 0 and auxY > 2): auxY = int(input("Selecione a linha ao qual deseja jogar \n")) auxX = int(input("Selecione a coluna ao qual deseja jogar \n")) while (auxX < 0 and auxX > 2): auxX = int(input("Selecione a coluna ao qual deseja jogar \n")) return auxY, auxX def proximaJogada(self): x, y = self.menu() while (self.estadoAtual.estado[x][y] != '*'): print("Posição invalida\n") x, y = self.menu() self.estadoAtual.estado[x][y] = self.simbJogador1 self.estadoAtual.imprimir() if (self.estadoAtual.testeTermino()): return self.estadoAtual = self.minMax.miniMaxDecision( copy.deepcopy(self.estadoAtual), self.simbJogador2) self.estadoAtual.imprimir()
class Ai(object): def __init__(self): self.__board = Board(19) self.__x = int() self.__y = int() self.__maxDepth = 2 self.__minmax = MinMax(self.__board) ##stoneOwner 1 is our stone 2 is the enemy stone def put(self, x: int, y: int): self.__board.set(x, y, 1) self.send(str(x) + "," + str(y)) def getBoard(self): return self.__board def setBoard(self, size: int): self.__board = Board(size) self.__minmax = MinMax(self.__board) def send(self, text: str): print(text) sys.stdout.flush() def recv(self): return input().replace("\r", "").replace("\n", "").split(" ") def firstPlay(self): self.__x = int(self.__board.getSize() / 2) self.__y = int(self.__board.getSize() / 2) self.put(self.__x, self.__y) def play(self, xEnemy: int, yEnemy: int): ## todo change this shit soon as you can move = self.__minmax.calculateNextMove(self.__maxDepth) self.put(move.x, move.y)
def play(self, game, opp_move): if len(game.next_moves()) == 0: return None minmax = MinMax(game, self.get_static_eval(), self.max_deep) best_move = minmax() return best_move
if self.board[position].isdigit(): self.board[position] = player break except TypeError: print('Por insira um digito de 1 a 9\n') except ValueError: print('Posição inválida\n') return position if __name__ == '__main__': turn_player_x = False game = TicTacToe() computer = MinMax(game, ('O', 'X')) computer2 = MinMax(game, ('X', 'O')) while not game.is_over(): print(game) if turn_player_x: best_move = computer2.get_decision(game, computer2.players) game.move(best_move, 'x') else: best_move = computer.get_decision(game, computer.players) game.move(best_move, 'O') turn_player_x = not turn_player_x print(game.result())
def __init__(self): self.__board = Board(19) self.__x = int() self.__y = int() self.__maxDepth = 2 self.__minmax = MinMax(self.__board)
def setBoard(self, size: int): self.__board = Board(size) self.__minmax = MinMax(self.__board)
from chessboard import ChessBoard from game import Game from minmax import MinMax if __name__ == '__main__': chess_board = ChessBoard() game = Game(chess_board) game.turn = 1 mini_max = MinMax(game, None) # mini_max.generate_legal_moves(chess_board.chess_board,ChessBoard.BLACK_PAWN) mini_max.find_best_move_using_minimax()
class Game: def __init__(self, chessboard): self.chessboard = chessboard self.player_piece = { Player.HUMAN: ChessBoard.WHITE_PAWN, Player.AI: ChessBoard.BLACK_PAWN } self.move_validator = MoveValidator(self) self.turn = Player.HUMAN self.was_last_move_a_two_step_move = True self.position_of_two_steps_pawn = (4, 'C') self.game_engine = GameEngine(self) self.mini_max = MinMax(self, self.move_validator) def print_current_turn(self): if self.turn == Player.AI: print("It's the computer's turn!") elif self.turn == Player.HUMAN: print("It's your turn!") def print_initial_message(self): print "You play with the white pawns" print "Computer plays with the black pawns" def start_chess_game(self, first=Player.NONE): self.turn = first if self.turn == Player.NONE: self.turn = randint(Player.HUMAN, Player.AI) self.game_state = GameState.PLAYING self.print_initial_message() while self.game_state != GameState.FINISHED: self.print_current_turn() self.chessboard.print_chessboard() if self.turn == Player.HUMAN: move = self.get_user_move() self.make_human_move(move) elif self.turn == Player.AI: best_ai_move = self.mini_max.find_best_move_using_minimax() ChessBoard.move_piece(self.chessboard.chess_board, best_ai_move[0], best_ai_move[1]) if self.game_state == GameState.WRONG_MOVE: print "Enter a correct move!" elif self.game_engine.is_win(self.chessboard.chess_board): if self.turn == Player.HUMAN: print "You won!" else: print "The computer won!" self.game_state = GameState.FINISHED elif self.game_engine.is_draw(self.chessboard.chess_board): print "It's a draw" self.game_state = GameState.FINISHED else: self.turn = (self.turn + 1) % 2 def get_user_move(self): move = raw_input("Enter your move: ") if len(move) != 5: print "Wrong input format for move!" return None moves = move.split(" ") move_tuples = [(), ()] move_tuples[0] = (int(moves[0][0]), MoveValidator.convert_character_to_integer( moves[0][1])) move_tuples[1] = (int(moves[1][0]), MoveValidator.convert_character_to_integer( moves[1][1])) return move_tuples def make_human_move(self, move): try: if move == None: raise Exception if not self.move_validator.is_move_valid( self.chessboard.chess_board, move[0], move[1], ChessBoard.WHITE_PAWN): raise Exception ChessBoard.move_piece(self.chessboard.chess_board, move[0], move[1]) self.game_state = GameState.PLAYING except: self.game_state = GameState.WRONG_MOVE
game = TicTacToe() ai, player2 = setup(conn, game) pc = None print(player2) if player2.upper() == 'X': player1 = 'O' position = pickle.loads(recv_msg(conn)) game.move(position, 'X') print(game) else: player1 = 'X' if ai.upper() == 'A': pc = MinMax(game, (player1, player2)) while not game.is_over(): print(game) if pc is not None: position_player = pc.get_decision(game, pc.players) game.move(position_player, player1) else: position_player = game.turn(player1) send_msg(conn, pickle.dumps(position_player)) print(game) if game.is_over(): break position_enemy = pickle.loads(recv_msg(conn))