def __init__(self, screen): """Init Game with all needed attributes. """ self.screen = screen self.gui = gui.Gui(self.screen) self.gui.draw_background() self.game_running = True self.game_move_number = 0 self.game_board = check_board_state.create_board() self.game_arbiter = check_board_state.CheckBoardState( self.screen, self.game_board) self.played_moves = [] self.player1 = players.HumanPlayer(constants.PLAYER1_NAME, constants.WHITE) self.player2 = players.AiPlayer(self.screen, self.game_board, constants.BLACK) self.player_on_move = self.player1 self.game_mode = constants.STANDARD self.gui_board = [[ gui.Square(self.screen) for i in range(constants.BOARD_SIZE) ] for j in range(constants.BOARD_SIZE)] self.gui_on_move = gui.OnMove(self.screen) self.button_new_game = gui.ButtonRightMenu(self.screen, 0, constants.RESTART) self.button_menu = gui.ButtonRightMenu(self.screen, 1, constants.MENU) self.button_white_stone = gui.ButtonChooseColor(self.screen, 0) self.button_black_stone = gui.ButtonChooseColor(self.screen, 1) self.button_ai_opponent = gui.ButtonChooseOpponent(self.screen, 0) self.button_ai_player = gui.ButtonChooseOpponent(self.screen, 1) self.button_standard_game_mode = gui.ButtonChooseMode(self.screen, 0) self.button_swap2_game_mode = gui.ButtonChooseMode(self.screen, 1) self.last_move = LastMove
def test_check_win(self): """Checking if method check_win() returns True if put five consecutive stones.""" self.board[1][13] = constants.WHITE self.board[2][12] = constants.WHITE self.board[3][11] = constants.WHITE self.board[4][10] = constants.WHITE self.board[5][9] = constants.WHITE result = check_board_state.CheckBoardState(self.board).check_win() self.assertTrue(result)
def test_check_cols(self): """Checking if method check_cols() returns True if put five consecutive stones in col.""" self.board[1][5] = constants.WHITE self.board[2][5] = constants.WHITE self.board[3][5] = constants.WHITE self.board[4][5] = constants.WHITE self.board[5][5] = constants.WHITE result = check_board_state.CheckBoardState(self.board).check_cols() self.assertTrue(result, True)
def test_check_rows(self): """Checking if method check_rows() returns True if put five consecutive stones in row.""" self.board[5][1] = constants.BLACK self.board[5][2] = constants.BLACK self.board[5][3] = constants.BLACK self.board[5][4] = constants.BLACK self.board[5][5] = constants.BLACK result = check_board_state.CheckBoardState(self.board).check_rows() self.assertTrue(result, True)
def make_move(self, game_board, played_moves, black_color=True): """Method returns best move as tuple (i,j), first using forced_move() else mini_max().""" self.game_board = game_board self.played_moves_in_game = played_moves self.arbiter = check_board_state.CheckBoardState( self.screen, self.game_board) self.squares_with_neighbours[0].clear() for i, j in self.played_moves_in_game: self.add_neighbours_squares(i, j, 0, self.played_moves_in_game) best_score = -math.inf if self.forced_move() is not False: i, j = self.forced_move() self.squares_with_neighbours_sorted[0].clear() return i, j self.mini_max(self.game_board, 0, 0, True, -math.inf, math.inf) start_time = time.time() self.threatening_squares = self.arbiter.good_moves evaluated_min_max_set = sort_moves_by_evaluation( self.squares_with_neighbours_sorted[0], black_color) for i, j, score in self.threatening_squares: for item in evaluated_min_max_set: if i == item[0] and j == item[1]: evaluated_min_max_set.remove(item) print("All considered moves: ", self.threatening_squares + evaluated_min_max_set) evaluated_min_max_set = self.threatening_squares + evaluated_min_max_set for i, j, score in evaluated_min_max_set: if self.game_board[i][j] == constants.EMPTY and ( time.time() - start_time < MAX_MOVE_TIME_SECONDS): self.game_board[i][j] = constants.BLACK self.add_neighbours_squares(i, j, 1, self.played_moves_in_game) score = self.mini_max(self.game_board, 1, MAX_DEPTH, False) self.remove_neighbours_squares(i, j, 1) self.game_board[i][j] = constants.EMPTY print("At board[{}][{}] evaluation={}".format(i, j, score)) if score == BLACK_WIN: best_move = i, j break if score > best_score: best_score = score best_move = i, j print("Played move on board[{}][{}] evaluation={}".format( best_move[0], best_move[1], best_score)) self.squares_with_neighbours_sorted[0].clear() return best_move
def __init__(self, screen, board, stone_color, name="AI"): """Init with board from game module and all needed attributes to make ai optimal. Attributes: arbiter: Class which allows to check if game is finished squares_with_neighbours: Set contains all squares with neighbours , list index is depth during mini_max algorithm we also checked squares with neighbours squares_with_neighbours_sorted:Above set sorted by mini_max evaluation after one move self.played_moves_in_game: all moves played in game during adding new squares to squares_with_neighbours need to remove played moves from this set threatening_squares: from CheckBoardState moves to deal with open three stones situation. """ super().__init__(name, stone_color) self.screen = screen self.game_board = board self.arbiter = check_board_state.CheckBoardState( self.screen, self.game_board) self.squares_with_neighbours = [set() for i in range(MAX_DEPTH + 1)] self.squares_with_neighbours_sorted = [ set() for i in range(MAX_DEPTH + 1) ] self.predicted_moves_in_mini_max = set() self.played_moves_in_game = PlayedMovesInGame self.threatening_squares = []
def test_check_draw(self): """Checking check_draw() returns True if fill board with one color (only 5 stones wins).""" self.board = [[constants.WHITE for i in range(constants.BOARD_SIZE)] for j in range(constants.BOARD_SIZE)] result = check_board_state.CheckBoardState(self.board).check_draw() self.assertTrue(result)