Beispiel #1
0
def test_extra_turn_player2():
    game = Game()

    game.take_slot(0)
    game.take_slot(2)

    assert game.get_player_turn() == 1
Beispiel #2
0
def test_load():
    # Load tree
    game = Game()
    game.take_slot(0)

    tree = MancalaTreeBuilder()

    tree.load_tree("do_not_delete.json")

    # Check children exists and first move is equal
    data = tree.root.children[0].get_data()
    assert tree.root.children.__len__(
    ) == 6 and data.state[0][0] == game.state[0][0]
    def load_tree(self, name):
        dict = json.load(open(name))
        self.set_root(Node(Game()))

        # Iterate children function
        def iter_children(children, parent):
            for move, child in children.items():
                move = int(move)

                # Construct game
                game = Game()
                game.state[0] = child['data']['state']['0']
                game.state[1] = child['data']['state']['1']
                game.player_turn = child['data']['player_turn']

                # Add a Leaf or Node to parent
                if 'children' in child:
                    # Node
                    child_node = Node(game)
                    parent.add_child(move, child_node)
                    iter_children(child['children'], child_node)
                else:
                    # Leaf
                    child_node = Leaf(game)
                    parent.add_child(move, child_node)

        iter_children(dict['children'], self.root)
        def iter_children(children, parent):
            for move, child in children.items():
                move = int(move)

                # Construct game
                game = Game()
                game.state[0] = child['data']['state']['0']
                game.state[1] = child['data']['state']['1']
                game.player_turn = child['data']['player_turn']

                # Add a Leaf or Node to parent
                if 'children' in child:
                    # Node
                    child_node = Node(game)
                    parent.add_child(move, child_node)
                    iter_children(child['children'], child_node)
                else:
                    # Leaf
                    child_node = Leaf(game)
                    parent.add_child(move, child_node)
Beispiel #5
0
def test_player1_steal():
    # Run game
    game = Game()

    game.take_slot(4)
    game.take_slot(0)
    game.take_slot(0)  # Player 1 steals 6 from player 2

    state = game.get_state()

    assert state[0][-1] == 8 and state[1][-1] == 0 and state[0][4] == 0 and state[0][5] == 5 and state[1][0] == 0
Beispiel #6
0
def test_player2_steal():
    game = Game()

    game.take_slot(0)
    game.take_slot(4)
    game.take_slot(0)
    game.take_slot(0)  # Player 2 steals 7 from player 1

    state = game.get_state()

    assert state[1][-1] == 9 and state[0][-1] == 0 and state[0][1] == 0 and state[1][4] == 0
Beispiel #7
0
def test_save():
    # Setup tree
    game = Game()

    recursion_limit = 4

    tree = MancalaTreeBuilder(recursion_limit)
    tree.set_root(Node(game))
    tree.build()

    # Save tree with random name
    name = random.random()
    path = "{0}.json".format(name)[2:]
    tree.save_tree(path)

    # Check it exists and has build children
    assert tree.root.children.__len__() == 6 and os.path.isfile(path)

    # Remove file
    os.remove(path)
Beispiel #8
0
    print("Hard: 2")
    print("Very hard (and very slow): 3")
    rec_limit = 2 + check_input("Choose difficulty level: ") * 2

    # Minimax algorithm
    def result_function(node, a):
        children = node.get_children()
        return children[a]

    # Construct minimax object
    minimax = Minimax(evaluation_function,
                      result_function,
                      max_depth=rec_limit)

    # Run game
    game = Game()
    should_end = game.is_terminal_state()

    print("Running game (anti-clockwise)")
    game_seq = []
    while not should_end:
        print_game(game)

        player_turn = game.get_player_turn()
        print("\nIt is player {0}'s turn".format(1 + player_turn))

        slot = None
        if player_turn == 0:
            slot = check_input("Choose which slot to pick up (index at 0): ")
            # Player
        else:
Beispiel #9
0
def test_invalid_pocket():
    game = Game()

    assert not game.take_slot(7) and not game.take_slot(-1)
Beispiel #10
0
def test_terminal_state():
    game = Game()

    # Sequence to end the game
    game.take_slot(2)
    game.take_slot(5)
    game.take_slot(1)
    game.take_slot(5)
    game.take_slot(0)
    game.take_slot(3)
    game.take_slot(2)
    game.take_slot(2)
    game.take_slot(0)
    game.take_slot(4)
    game.take_slot(2)
    game.take_slot(5)

    assert game.is_terminal_state()
Beispiel #11
0
def test_capture_pieces_on_end():
    game = Game()

    # Sequence to end the game
    game.take_slot(2)
    game.take_slot(5)
    game.take_slot(1)
    game.take_slot(5)
    game.take_slot(0)
    game.take_slot(3)
    game.take_slot(2)
    game.take_slot(2)
    game.take_slot(0)
    game.take_slot(4)
    game.take_slot(2)
    game.take_slot(5)

    game.end_game()
    state = game.get_state()
    assert state[0][6] == 42 and state[1][6] == 6 and sum(state[0][0:6] + state[1][0:6]) == 0