def game_control(self, game, board): while game.is_terminal(board) is False: if game.get_player_turn().get_type() is Player.RANDOM: posible_movements = game.successors(board, game.get_player_turn()) random = randint(0, len(posible_movements) - 1) move_to_do = posible_movements[random] board.move_piece(move_to_do[0][0][0], move_to_do[0][0][1], move_to_do[0][1][0], move_to_do[0][1][1]) game.change_turn() elif game.get_player_turn().get_type() is not Player.HUMAN: search = SeachMethod(SeachMethod.AIMA_ALPHABETA_SEARCH, 2) move = search.search(board, game) piece = board.get_piece_in_pos(move[0][0], move[0][1]) print str(move[0][0]) + ", " + str(move[0][1]) print piece.to_string() board.move_piece(move[0][0], move[0][1], move[1][0], move[1][1]) game.change_turn()
player_two = Player(players[0], Piece.BLACK) ### CREATING THE GAME INSTANCE ### game = Game(player_one, player_two) ### CHOOSING THE BOARD ### board = Board(6, 4) board.initialize_king_and_rook() game_stats = Stats(game, x) ### MAIN GAME LOOP ### start = time.time() while game.is_terminal(board) is False: search = SeachMethod(SeachMethod.AIMA_ALPHABETA_SEARCH, 2) game_stats.set_search_method(SeachMethod.AIMA_ALPHABETA_SEARCH) move = search.search(board, game) game_stats.add_expanded_nodes(game.get_player_turn(), search.get_expand_nodes()) board.move_piece(move[0][0], move[0][1], move[1][0], move[1][1]) game_stats.add_visit_nodes(game.get_player_turn().get_color()) game.change_turn() print board.to_string() game_stats.set_game_time(time.time() - start) print game.get_player_turn().to_string() + ", YOU HAVE BEEN DEFEATED!" game_stats.set_winner(game.get_player_turn()) ### CREATING THE GAME INSTANCE ### game = Game(player_two, player_one) ### CHOOSING THE BOARD ###
def game_ui_loop(self, game, board, player_one, player_two): ### GUI INITIALIZATION ### pygame.init() screen = pygame.display.set_mode((len(board.get_board()[0])*60, len(board.get_board())*60), 1) pygame.display.set_caption('Radikal Chess') pieces = [{}, {}] pieces[0]["Rbw"] = pygame.image.load("./img/brw.png") pieces[0]["Bbw"] = pygame.image.load("./img/bbw.png") pieces[0]["Kbw"] = pygame.image.load("./img/bkw.png") pieces[0]["Qbw"] = pygame.image.load("./img/bqw.png") pieces[0]["Pbw"] = pygame.image.load("./img/bpw.png") pieces[0]["Rww"] = pygame.image.load("./img/wrw.png") pieces[0]["Bww"] = pygame.image.load("./img/wbw.png") pieces[0]["Kww"] = pygame.image.load("./img/wkw.png") pieces[0]["Qww"] = pygame.image.load("./img/wqw.png") pieces[0]["Pww"] = pygame.image.load("./img/wpw.png") pieces[0]["w"] = pygame.image.load("./img/w.png") pieces[1]["Rbb"] = pygame.image.load("./img/brb.png") pieces[1]["Bbb"] = pygame.image.load("./img/bbb.png") pieces[1]["Kbb"] = pygame.image.load("./img/bkb.png") pieces[1]["Qbb"] = pygame.image.load("./img/bqb.png") pieces[1]["Pbb"] = pygame.image.load("./img/bpb.png") pieces[1]["Rwb"] = pygame.image.load("./img/wrb.png") pieces[1]["Bwb"] = pygame.image.load("./img/wbb.png") pieces[1]["Kwb"] = pygame.image.load("./img/wkb.png") pieces[1]["Qwb"] = pygame.image.load("./img/wqb.png") pieces[1]["Pwb"] = pygame.image.load("./img/wpb.png") pieces[1]["b"] = pygame.image.load("./img/b.png") clock = pygame.time.Clock() mouse_pos = [-1, -1] highlighted_pos = [-1, -1] available_moves = [] adviced_move = [] available_white_moves = [] available_black_moves = [] reset_moves = [-1, -1, -1] start_time = time.time() ### MAIN GAME LOOP ### while game.is_terminal(board) is False: pygame.display.set_caption('Radikal Chess') movement = False while movement is False: clock.tick(5) self.display_board(screen, board, pieces, available_moves, highlighted_pos, available_white_moves, available_black_moves, adviced_move) for event in pygame.event.get(): if event.type is QUIT: break elif event.type is KEYDOWN: if event.key is K_ESCAPE: return elif event.key is K_w: #devolver los movimientos posibles de los blancos if reset_moves[0] is -1: for move in game.successors(board, player_one): available_white_moves.append(move[0]) reset_moves[0] = 0 elif reset_moves[0] is 0: available_white_moves = [] reset_moves[0] = -1 self.display_board(screen, board, pieces, available_moves, highlighted_pos, available_white_moves, available_black_moves, adviced_move) elif event.key is K_b: #devolver los movimientos posibles de los negros if reset_moves[1] is -1: for move in game.successors(board, player_two): available_black_moves.append(move[0]) reset_moves[1] = 0 elif reset_moves[1] is 0: available_black_moves = [] reset_moves[1] = -1 self.display_board(screen, board, pieces, available_moves, highlighted_pos, available_white_moves, available_black_moves, adviced_move) elif event.key is K_a: if game.get_player_turn().get_type() is not Player.HUMAN: break #devolver un advice si el jugador es humano if reset_moves[2] is -1: search = SeachMethod(SeachMethod.AIMA_ALPHABETA_SEARCH, 2) adviced_move = search.search(board, game) reset_moves[2] = 0 elif reset_moves[2] is 0: adviced_move = [] reset_moves[2] = -1 elif event.type is MOUSEBUTTONDOWN: mouse_x_pos = event.pos[0] mouse_y_pos = event.pos[1] mouse_pos[0] = int(math.floor(mouse_y_pos / 60)) mouse_pos[1] = int(math.floor(mouse_x_pos / 60)) available_white_moves = [] available_black_moves = [] if game.get_player_turn().get_type() is Player.HUMAN: if mouse_pos[0] is not -1: if highlighted_pos[0] is mouse_pos[0] and highlighted_pos[1] is mouse_pos[1]: highlighted_pos[0] = -1 available_moves = [] elif mouse_pos in available_moves: movement = board.move_piece(highlighted_pos[0], highlighted_pos[1], mouse_pos[0], mouse_pos[1]) highlighted_pos[0] = -1 available_moves = [] game.change_turn() else: piece = board.get_piece_in_pos(mouse_pos[0], mouse_pos[1]) if piece is not None: if (game.get_player_turn() is player_one and piece.get_color() is Piece.WHITE) or \ (game.get_player_turn() is player_two and piece.get_color() is Piece.BLACK): highlighted_pos[0] = mouse_pos[0] highlighted_pos[1] = mouse_pos[1] available_moves = board.is_legal_movement(mouse_pos[0], mouse_pos[1]) self.display_board(screen, board, pieces, available_moves, highlighted_pos, available_white_moves, available_black_moves, adviced_move) pygame.display.set_caption(str(game.get_player_turn().to_string() + ", YOU HAVE BEEN DEFEATED!")) print board.to_string()