Пример #1
0
 def calculate_moves(self, dices: Die, board) -> [(int, int)]:
     print("throw", dices)
     moves_options = generate_moves(self, Die(dices.first, dices.second), board)
     moves = []
     if len(moves_options) > 0:
         moves = choice(moves_options)
     self.slow(board)
     return moves
Пример #2
0
 def test_parallel_move_dark_checker_from_bar_in(self):
     self.game.board.board = self.game.board.clear_board()
     self.game.board.place_at(0, self.black.color, 1)
     print(self.game.board)
     moves = generate_moves(self.black, Die(2, 3), self.game.board)
     expected_moves = [[(0, 22), (22, 20)], [(0, 23), (23, 20)]]
     pprint.pprint(moves)
     self.assertEqual(moves, expected_moves)
Пример #3
0
    def test_two_out_can_move_only_one(self):
        self.game.board.board = self.game.board.clear_board()
        self.game.board.place_at(0, self.black.color, 1)
        self.game.board.place_at(0, self.black.color, 1)

        self.game.board.place_at(19, self.white.color, 1)
        self.game.board.place_at(19, self.white.color, 1)

        moves = generate_moves_serial(self.black, Die(2, 6), self.game.board)
        expected_moves = [[(0, 23)]]
        self.assertEqual(moves, expected_moves)
Пример #4
0
    def test_black_move_out(self):
        rand_black = RandomPlayer(Checker.BLACK)
        human_white = RandomPlayer(Checker.WHITE)

        game = Game(player_1=human_white,
                    player_2=rand_black,
                    create_protocol=False)
        game.board.board = game.board.clear_board()
        game.current_player = rand_black
        game.current_dice = Die(4, 6)
        game.board.place_at(19, Checker.BLACK, 1)
        game.board.place_at(22, Checker.BLACK, 1)

        game.board.place_at(18, Checker.WHITE, 4)

        game.run()

        moves = generate_moves_serial(self.black, Die(4, 6),
                                      game.board.get_view(True))
        print(moves)
Пример #5
0
    def test_black_move_out_4(self):
        self.game.board.board = self.game.board.clear_board()
        self.game.board.place_at(22, self.black.color, 4)
        self.game.board.place_at(21, self.black.color, 2)
        self.game.current_player = self.black

        self.game.board.place_at(23, self.white.color, 6)
        moves = generate_moves_serial(self.black, Die(4, 4),
                                      self.game.board.get_view(True))
        moves = gui.HumanPlayer.map_out_moves([(3, 0), (4, 0), (3, 0), (4, 0)],
                                              moves)
        expected_moves = [(3, -1), (3, -1), (4, 0), (4, 0)]
        self.assertEqual(sorted(moves), expected_moves)
Пример #6
0
    def calculate_moves(self, dices: Die, board) -> [(int, int)]:
        print("throw", dices)
        moves_options = generate_moves(self, Die(dices.first, dices.second), board)
        if len(moves_options) == 0:
            return moves_options

        future_boards = self.get_future_boards(moves_options, board)

        node_data = [self.mapper.to_nodes(b, self.color) for b in future_boards]
        win_chance_white: [float] = self.ai.predict(node_data)

        extreme = max(win_chance_white)
        if self.color == Checker.BLACK:
            extreme = min(win_chance_white)

        return moves_options[win_chance_white.index(extreme)]
Пример #7
0
    def readProtocol(self):
        lines = self.protocol_file.readlines()

        count = 0
        # Strips the newline character
        for line in lines:
            count += 1
            line = line.strip()
            if len(line) < 4:  # to short for further checks
                continue
            # if line[0] == ';' or not line[0].isdigit() or 'match' in line:
            #    continue
            if not (line[0].isdigit() and line[1] == ')') \
                    and not (line[0].isdigit() and line[1].isdigit() and line[2] == ')') \
                    and not (line[0].isdigit() and line[1].isdigit() and line[2].isdigit() and line[3] == ')'):
                continue
            protocol_logger.debug("Line{}: {}".format(count, line.strip()))

            # delete turn number
            # print(line)
            line = (line.split(")")[1]).strip()
            # split by space
            # example line :   63: 25/22 22/16             64: 21/15 15/11
            lineElements = line.split(" ")
            die = None
            turn = []
            moves = []
            got_dices = False
            for element in lineElements:
                if ":" in element:
                    if len(moves) != 0 or got_dices:
                        tempturn = Turn(die, moves)
                        self.game_proto.append(tempturn)
                        moves = []
                        die = None
                        got_dices = False
                    element = element.replace(":", "").strip()
                    die = Die(element[0], element[1])
                    got_dices = True
                elif "/" in element:
                    # print(str(element.split("/")[0]))
                    src = int(((element.split("/"))[0]).replace('*', '').strip())
                    trg = int(((element.split("/"))[1]).replace('*', '').strip())
                    moves.append(Move(src, trg))

            if len(moves) != 0 or got_dices:
                self.game_proto.append(Turn(die, moves))
Пример #8
0
    def calculate_moves(self, dices: Die, board) -> [(int, int)]:
        self.items[Items.CURRENT_PLAYER_NAME].set(self.name)
        self.items[Items.CURRENT_PLAYER] = self
        self.items[Items.CURRENT_DIE].set(str(dices.get_roll()))
        self.uiBoard.draw_game_state(board)

        possible_moves = generate_moves(self, dices, board)
        moves = []
        if possible_moves:
            for i in range(len(possible_moves[0])):
                self.items[Items.NEXT_PLAYER_BUTTON].wait_variable(
                    self.items[Items.PLACED_CHECKER])
                moves.append(self.items[Items.LATEST_MOVE])
                print(self.items[Items.LATEST_MOVE])
                self.items[Items.PLACED_CHECKER].set(0)
        print(moves)
        if any(t < 0 for _, t in moves):
            moves = self.map_out_moves(moves, possible_moves)
        return moves
Пример #9
0
def verify_moves(player: Player, moves: [(int, int)], die: Die, board: Board):
    dice = list(die.get_move_options())
    out = board[0][player.color]
    for src, target in moves:
        logging.debug(f"verify move: from {src} to {target}")

        # check out
        if len(out) != 0:
            if src != 0:
                logging.debug(
                    "checker in out and move does not place it into game")
                raise ValueError(
                    "checker in out and move does not place it into game")
            out.pop()
        logging.debug(f"out is okay")

        dif = abs(src - target)
        if src == 0:
            dif = 25 - target
        if dif not in dice and 0 != src and target != 0:
            logging.debug(
                f"distance {dif} of {src},{target} dose not match with any die"
            )
            raise ValueError(
                f"distance {dif} of {src},{target} dose not match with any die"
            )
        dice.remove(dif)
        logging.debug(f"distance is present as die")

        # move in right direction
        if abs(player.home.out_bound - target) > abs(
                player.home.out_bound - src) and 0 < target <= 24 and (
                    player.home.out_bound != 1 and src == 0):
            logging.debug(f"wrong direction")
            raise ValueError("wrong direction")

        local_src = board[src]
        if player.color not in local_src:
            logging.debug(f"src location does not contain checker for player")
            raise ValueError(
                "src location does not contain checker for player")

        logging.debug(f"player moves its checker")

        remove_from_game = target <= 0 or target >= 24
        # player can only move checker to to positions with 1 or less checkers present or one then he already owns
        target_field = None
        if not remove_from_game:
            target_field = board[target]
            if len(target_field.content
                   ) > 1 and player.color not in target_field:
                logging.debug(
                    f"target location {target_field.content} is not available")
                raise ValueError(
                    f"target location {target_field.content} is not available")
            logging.debug(f"target location is available")

        if remove_from_game:
            pos = board.get_checkers_position_of(player)
            in_base = 1 <= min(pos) <= 6 and 1 <= max(pos) <= 6
            if not in_base:
                logging.debug(f"not all checkers in base")
                raise ValueError("not all checkers in base")
            logging.debug(f"can remove checker from game")
        else:
            target_field.place(player.color)
        local_src.remove(player.color)
        logging.debug(f"move is okay")
Пример #10
0
 def test_move_white_checker_from_bar_in(self):
     self.game.board.board = self.game.board.clear_board()
     self.game.board.place_at(0, self.white.color, 1)
     moves = generate_moves(self.white, Die(2, 3), self.game.board)
     expected_moves = [[(0, 22), (22, 20)], [(0, 23), (23, 20)]]
     self.assertEqual(moves, expected_moves)
Пример #11
0
 def test_move_checkers_invalid_checker_fail(self):
     self.game.current_dice = Die(1, 4)
     pprint(self.game.board.board)
     self.assertRaises(ValueError, lambda: game_moves_are_valid(self.white, [(13, 14), (12, 8)], self.game))
Пример #12
0
 def test_move_generator_double_six(self):
     pprint.pprint(generate_moves(self.white, Die(6, 6), self.game.board))
Пример #13
0
 def test_move_checkers_from_valid_to_valid_with_no_items_out_and_not_in_home_success(self):
     self.game.current_dice = Die(3, 4)
     print(self.game.board)
     self.assertIsNone(game_moves_are_valid(self.black, [(13, 9), (13, 10)], self.game))
Пример #14
0
 def test_move_checkers_from_valid_to_invalid_target_location_with_no_items_out_and_not_in_home_fail(self):
     self.game.current_dice = Die(1, 4)
     self.assertRaises(ValueError, lambda: game_moves_are_valid(self.black, [(13, 12), (13, 9)], self.game))
Пример #15
0
 def test_move_generator_double_three(self):
     pprint.pprint(generate_moves(self.white, Die(3, 3), self.game.board))
Пример #16
0
 def test_move_generator_double_two(self):
     pprint.pprint(generate_moves(self.white, Die(2, 2), self.game.board))
Пример #17
0
 def test_move_generator_one_and_two(self):
     pprint.pprint(generate_moves(self.white, Die(2, 3), self.game.board))
Пример #18
0
def __possible_move_options_permutations(die: Die) -> [[int]]:
    move_option = list(die.get_roll())

    if die.is_double():
        return [list(repeat(move_option[0], 4))]
    return [move_option, [move_option[1], move_option[0]]]
Пример #19
0
 def test_move_generator_one_and_two_one_not_in_home(self):
     self.fill_home()
     self.game.board.place_at(7, self.white.color)
     pprint.pprint(self.game.board)
     pprint.pprint(generate_moves(self.white, Die(5, 6), self.game.board))
Пример #20
0
 def test_move_generator_five_six_in_home_one_out(self):
     self.fill_home()
     self.game.board.place_at(0, self.white.color)
     moves = generate_moves(self.white, Die(5, 6), self.game.board)
     pprint.pprint(moves)
     self.assertEqual(len(moves), 3)
Пример #21
0
 def test_move_generator(self):
     self.game.board.board = self.game.board.clear_board()
     self.game.board.place_at(23, self.white.color, 2)
     pprint.pprint(generate_moves(self.white, Die(4, 6), self.game.board))
Пример #22
0
 def test_move_generator_one_and_two_and_one_out_black(self):
     self.game.board.remove_from(19)
     self.game.board.place_at(0, self.black.color)
     pprint.pprint(generate_moves(self.black, Die(1, 2), self.game.board))
Пример #23
0
 def test_move_generator_double_six_black(self):
     pprint.pprint(generate_moves(self.black, Die(6, 6), self.game.board))