示例#1
0
 def setUp(self):
     self.board_3x3 = Board(3)
     self.board_5x5 = Board(5)
     self.game = Game()
     self.game.board = Board(3)
     self.game.player1 = Player("Toothless", "X")
     self.game.player2 = Player("Hiccup", "O")
示例#2
0
 def test_board_equality_returns_false(self):
     pawn_a = Pawn(1, 0, 1, True)
     pawn_b = Pawn(1, 0, 2, True)
     king = King(1, 4, 0, True)
     board_a = Board([pawn_a, king])
     board_b = Board([pawn_b, king])
     self.assertFalse(board_a == board_b)
示例#3
0
 def test_board_eq_4(self):
     board_size = 3
     board_3 = Board(board_size)
     board_3.board = [['x', 'o', '.'], ['.', '.', '.'], ['.', '.', '.']]
     board = Board(board_size)
     board.board = [['x', 'o', '.'], ['.', '.', '.'], ['.', '.', '.']]
     self.assertEqual(board_3, board)
示例#4
0
 def play(self, nested_array):
     if nested_array == None:
         nested_array = Board().draw_board()
     else:
         Board().draw_board()
     if not self.is_human:
         return self.computer(nested_array)
     return self.human(nested_array)
示例#5
0
def test_alive_cell_come_dead():
    board = Board(3, 3)
    board.alive(0, 0)
    board.alive(0, 1)

    evolved_board = board.evolve()

    expected_evolved_board = Board(3, 3)

    assert evolved_board == expected_evolved_board
示例#6
0
    def test_update_board_single_red_bubble(self):
        empty_board = Board()
        curr_board = Board([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0],
                            [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])

        game_state = GameState(curr_board, 1)
        game_state.update_board(2, 2)

        assert game_state.board == empty_board
        assert game_state.result == GameResults.Win
示例#7
0
    def test_update_board_multiple_red_bubbles(self):
        empty_board = Board()
        curr_board = Board([[1, 0, 1, 0, 1], [0, 0, 0, 0, 0], [1, 0, 1, 0, 1],
                            [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 1, 0, 1]])

        game_state = GameState(curr_board, 1)
        game_state.update_board(0, 0)

        assert game_state.board == empty_board
        assert game_state.result == GameResults.Win
示例#8
0
 def test_board_eq_init_4(self):
     board_size = 4
     board_4 = Board(board_size)
     board_4.board = [
         ['.', '.', '.', '.'],
         ['.', '.', '.', '.'],
         ['.', '.', '.', '.'],
         ['.', '.', '.', '.'],
     ]
     board = Board(board_size)
     self.assertEqual(board_4, board)
示例#9
0
    def test_update_board_level_18(self):
        expected_board = Board()

        curr_board = Board([[0, 3, 2, 4, 0], [1, 1, 1, 2, 0], [0, 1, 4, 1, 2],
                            [0, 4, 2, 0, 1], [1, 1, 1, 2, 2], [0, 4, 0, 1, 1]])

        game_state = GameState(curr_board, 1)
        game_state.update_board(4, 2)

        assert game_state.board == expected_board
        assert game_state.result == GameResults.Win
示例#10
0
 def test_get_surrounding(self):
     y = 10
     x = 10
     data = self.get_json_data()
     board = Board()
     tile_map = data["map"]["tiles"]
     result = board.get_surrounding(x, y, tile_map) != False
     self.assertEqual(result, True)
     board = Board()
     tile_map = data["map"]["tiles"]
     result = board.get_surrounding(x,y, tile_map) != False
     self.assertEqual(result, True)
示例#11
0
    def test_update_board_level_16(self):
        expected_board = Board([[1, 1, 1, 0, 0], [0, 0, 0, 0, 0],
                                [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                                [0, 0, 0, 0, 0], [1, 0, 0, 0, 0]])

        curr_board = Board([[2, 4, 4, 1, 1], [2, 2, 2, 0, 4], [0, 4, 2, 2, 2],
                            [1, 1, 1, 3, 1], [1, 4, 0, 1, 4], [4, 0, 3, 2, 0]])

        game_state = GameState(curr_board, 1)
        game_state.update_board(3, 2)

        assert game_state.board == expected_board
        assert game_state.result == GameResults.Lose
示例#12
0
    def test_update_board_red_green_bubbles(self):
        expected_board = Board([[0, 0, 0, 0, 0], [0, 1, 0, 1, 0],
                                [1, 1, 0, 1, 1], [0, 2, 0, 2, 0],
                                [0, 1, 0, 1, 0], [0, 0, 0, 0, 0]])

        # level 7
        curr_board = Board([[0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [1, 2, 1, 2, 1],
                            [0, 2, 0, 2, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0]])

        game_state = GameState(curr_board, 2)
        game_state.update_board(2, 2)

        assert game_state.board == expected_board
        assert game_state.result == None
示例#13
0
    def test_empty_cell_exists(self):
        board_size = 3
        valid_move = [0, 0]
        occupied = [0, 1]
        out_of_board = [0, board_size + 2]

        board_available = Board(board_size)
        board_available.board = [['.', 'o', 'x'], ['.', 'x', '.'],
                                 ['x', '.', 'o']]

        board_full = Board(board_size)
        board_full.board = [['x', 'o', 'x'], ['o', 'x', 'o'], ['x', 'x', 'o']]

        self.assertTrue(board_available.empty_cell_exists())
        self.assertFalse(board_full.empty_cell_exists())
示例#14
0
 def test_king_capture_covered_piece(self):
     king = King("e4", "white")
     board = Board([king, Knight("e5", "black"), Bishop("a1", "black")])
     board.create_board()
     output = king.move_to(board, "e5", "black")
     expected_output = (False, "illegal move: king in check")
     self.assertEqual(expected_output, output)
示例#15
0
def play_game(dummy=True,
              weights=None,
              matches=0,
              train=True,
              types=[NEURAL_TYPE, NEURAL_TYPE]):
    board = Board(weights, types)
    red_player = board.players[RED]
    green_player = board.players[GREEN]
    red_won = False
    green_won = False
    moves = 0
    won = False

    while moves < MAX_PLAYS:
        moved, _ = red_player.random_move() if dummy else red_player.best_move(
            learn=False)
        output_board(board, green_player.weights, green_player.moves, matches)
        over, color = is_game_over(board, GREEN)
        if not moved or over:
            break
        moved, match_errors = green_player.best_move(learn=train)
        output_board(board, green_player.weights, green_player.moves, matches)
        over, color = is_game_over(board, GREEN)
        if not moved or over:
            break

        moves = green_player.moves
    if not moves < MAX_PLAYS:
        return TIE, green_player.weights, [0.001]
    elif color == GREEN:
        return WIN, green_player.weights, [0.001]
    elif color == RED:
        return LOSS, green_player.weights, [0.001]
示例#16
0
 def test_game_still_running(self):
     """Verify game still running"""
     board = Board(4, 300, 20, 100, 1000, 1)
     board.actual_round = 999
     self.assertEqual(board.game_still_running(), True)
     board.actual_round = 1000
     self.assertEqual(board.game_still_running(), False)
示例#17
0
 def test_actual_round(self):
     """Verify actual round"""
     board = Board(4, 300, 20, 100, 1000, 1)
     board.actual_round = 2
     self.assertEqual(board.actual_round, 2)
     board.actual_round = 1
     self.assertEqual(board.actual_round, 2)
示例#18
0
 def test_game_end(self):
     # Can't move
     b = Board()
     b._board = [
         2, 4, 2, 4,
         4, 2, 4, 2,
         2, 4, 2, 4,
         4, 2, 4, 2
     ]
     self.assertEqual(True, b.is_terminal())
     # Zero check
     b._board = [
         2, 4, 2, 4,
         4, 2, 4, 2,
         2, 4, 2, 4,
         4, 2, 0, 2
     ]
     self.assertEqual(False, b.is_terminal())
     # Right or left
     b._board = [
         2, 4, 2, 4,
         4, 2, 4, 2,
         2, 4, 8, 4,
         4, 2, 2, 2
     ]
     self.assertEqual(False, b.is_terminal())
     # Up or down
     b._board = [
         2, 4, 2, 4,
         4, 2, 4, 2,
         2, 4, 8, 4,
         4, 2, 8, 2
     ]
     self.assertEqual(False, b.is_terminal())
示例#19
0
    def test_get_words_constrained(self):
        trieRoot = Trie.words()

        board = Board()
        testCoord = Coord(4, 8)
        startWord = "cabriole"
        startCoord = Coord(3, 7)
        board.place(("c", "a", "b", "r", "i", "o", "l", "e"), startCoord, True)

        tiles = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

        start = StartSequence(
            4, 6, [None, "a", None, None, None, None, None, None, None], False)
        plays = trieRoot.get_words_constrained(start, tiles, board)
        plays = [tuple(play) for play in plays]

        self.assertEqual(
            set(plays), {('b', None), ('b', None, 'd'), ('b', None, 'd', 'e'),
                         ('b', None, 'd', 'g', 'e'), ('b', None, 'a'),
                         ('b', None, 'a', 'e', 'd'), ('b', None, 'g'),
                         ('f', None), ('f', None, 'd'), ('f', None, 'd', 'e'),
                         ('f', None, 'd', 'g', 'e'), ('f', None, 'c', 'e'),
                         ('f', None, 'c', 'e', 'd'),
                         ('f', None, 'c', 'a', 'd', 'e'), ('f', None, 'g'),
                         ('d', None), ('d', None, 'b'), ('d', None, 'c', 'e'),
                         ('d', None, 'g'), ('a', None), ('c', None, 'b'),
                         ('c', None, 'f', 'e'), ('c', None, 'd'),
                         ('c', None, 'd', 'e'), ('c', None, 'd', 'g', 'e'),
                         ('c', None, 'g', 'e'), ('c', None, 'g', 'e', 'd'),
                         ('g', None, 'b'), ('g', None, 'e'),
                         ('g', None, 'e', 'd'), ('g', None, 'd')})

        board.place(plays[10], Coord(start.x, start.y), start.ish)
    def __init__(self,
                 window,
                 player1,
                 player2,
                 data_dir_path,
                 Q_learn=None,
                 Q={},
                 alpha=0.3,
                 gamma=0.9):

        self.data_dir_path = data_dir_path
        self.data_file_path = data_dir_path + "/"

        self.window = window
        self.user_interface = UserInterface(window, self)
        self.board = Board()
        self.evaluator = GameEvaluator()
        self.is_q_learn_mode = False

        self.player1 = player1
        self.player2 = player2
        self.current_player = player1
        self.other_player = player2

        self.games, self.game_stats, self.game_story = self.prepare_stats()
        self.Q_learn = QLearning(self.player1, self.player2)
示例#21
0
def solve():
    selected_level = levels_menu()
    algorithm, heuristic = algorithms_menu()

    board_matrix, max_touches = utils.read_level_file(selected_level)
    board = Board(board_matrix)

    game_state = GameState(board, max_touches)

    print_header(game_state)
    utils.print_board(game_state.board)

    start_time = time.time()
    solution = algorithm.execute(game_state, heuristic)
    end_time = time.time()
    total_time = round(end_time - start_time, 2)

    for move in solution:
        move_row, move_col = move
        game_state.update_board(move_row, move_col)
        print("Move: [", move_row, ",", move_col, "]")
        print_header(game_state)
        utils.print_board(game_state.board)

    if game_state.result == GameResults.Win:
        print("**** You Won! ****")
    else:
        print("**** You Lost! ****")

    print("\nSolved in ", total_time, " seconds", sep="")
    print()
示例#22
0
def pacman(input_file):
    """
    Input:
        1. input_file (String) =
            contains the name of a text file you need to read that is in the same directory,
            includes the ".txt" extension (ie. "input.txt")
    Outputs:
        1. final_pos_x (int) = final x location of Pacman
        2. final_pos_y (int) = final y location of Pacman
        3. coins_collected (int) =
            the number of coins that have been collected by Pacman across all movements
    """
    try:
        lines_of_file: List[str] = FileParser().parse(path_to_file=input_file)
        config_manager: ConfigManager = ConfigManager(inputs=lines_of_file)
        board: Board = Board(config=config_manager)
        for move in config_manager.moves:
            if board.allows(move):
                board.make_move(move)
        return (
            board.pacman_x,
            board.pacman_y,
            board.number_of_coins_collected,
        )
    except (IndexError, ValueError):
        return -1, -1, 0
示例#23
0
    def test_is_winning_second_diagonal(self):
        board_size = 3
        board_3 = Board(board_size)
        board_3.board = [['.', 'o', 'x'], ['.', 'x', '.'], ['x', '.', 'o']]

        self.assertTrue(board_3.is_winning_second_diagonal('x'))
        self.assertFalse(board_3.is_winning_second_diagonal('o'))
示例#24
0
 def test_king_capture_uncovered_piece(self):
     king = King("e4", "white")
     board = Board([king, Knight("e5", "black"), Bishop("a2", "black")])
     board.create_board()
     output = king.move_to(board, "e5", "black")
     expected_output = (True, "legal move carried out")
     self.assertEqual(expected_output, output)
示例#25
0
 def test_game_over_full(self):
     board_size = 3
     game_manager = GameManager(self.a1, self.a2, board_size)
     board = Board(board_size)
     board.board = [['o', 'o', 'x'], ['x', 'x', 'o'], ['o', 'x', 'o']]
     game_manager.board = board
     self.assertTrue(game_manager.game_over())
示例#26
0
 def test_game_not_over(self):
     board_size = 3
     game_manager = GameManager(self.a1, self.a2, board_size)
     board = Board(board_size)
     board.board = [['o', '.', '.'], ['x', 'x', '.'], ['o', '.', '.']]
     game_manager.board = board
     self.assertFalse(game_manager.game_over())
示例#27
0
    def test_is_winning_row_3(self):
        board_size = 3
        board_3 = Board(board_size)
        board_3.board = [['x', 'x', 'x'], ['o', '.', '.'], ['.', 'o', '.']]

        self.assertTrue(board_3.is_winning_row('x'))
        self.assertFalse(board_3.is_winning_row('o'))
示例#28
0
    def test_place(self):
        board = Board()

        board.place(1, "X")

        assert board.get(1) == "X"
        assert board.get(2) == None
示例#29
0
def test_out_of_bounds_check():
    b = Board()
    assert b.is_oob(0) == False
    assert b.is_oob(30) == False
    assert b.is_oob(63) == False
    assert b.is_oob(64) == True
    assert b.is_oob(-1) == True
示例#30
0
    def initGameCallBack(self):

        if isinstance(self.interFrame, Frame):
            self.interFrame.destroy()
            self.timer.destroy()
            self.counter.destroy()

        if isinstance(self.gameFrame, Frame):
            self.gameFrame.destroy()

        storage = Storage()
        self.mines = storage.get("mines")
        h = storage.get("height")
        w = storage.get("width")
        self.isGameStarted = False

        self.tiles = [[j for j in range(w)] for i in range(h)]
        self.bombs = set()
        self.bombsPrediction = set()
        self.tilesAmount = w * h

        self.board = Board(w, h, self.mines)

        self.root.bind_class('Label', '<Button-1>', self.openTileEvent)
        self.root.bind_class('Label', '<Button-2>', self.flagTileEvent)
        self.root.bind('<KeyPress>', self.keyboardEvent)

        self.code = ""
        self.__createFrame()
        self.__initInterface()