def test_get_possible_moves_overwrite_on_other_player(self): board = Board("""\ 2 1 0 0 3 3 2 0 0 0 2 0 0 0 1 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE, use_overwrite=True) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_ONE, EMPTY, EMPTY], [EMPTY, PLAYER_ONE, EMPTY], [EMPTY, EMPTY, PLAYER_ONE]]) self.assertEqual(0, next_game_states[0].player_overwrites[PLAYER_ONE]) def test_get_possible_moves_can_not_move_on_other_player(self): board = Board("""\ 2 0 0 0 3 3 2 0 0 0 2 0 0 0 1 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE, use_overwrite=True) self.assertEqual(0, len(next_game_states))
def test_get_possible_moves_boese01(self): board = Board("""\ 2 0 0 0 8 11 - - - - - - 0 - - - - - - - - - - 2 - - - - - 2 2 2 2 2 2 2 1 0 0 - - - - - - 2 - - - - - - - - - - 2 - - - - - - - - - - 2 - - - - - - - - - - 2 - - - - - - - - - - 2 - - - - 1 2 6 <-> 6 7 4 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE], [HOLE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, EMPTY, EMPTY], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE]])
def test_get_possible_moves_boese07(self): board = Board("""\ 2 1 0 0 8 11 - - - - - - 1 - - - - - - - - - - 2 - - - - - 2 2 2 2 2 2 2 1 1 0 - - - - - - 2 - - - - - - - - - - 2 - - - - - - - - - - 2 - - - - - - - - - - 2 - - - - - - - - - - 2 - - - - 1 2 6 <-> 6 7 4 """) game = GameState(board) next_game_states = game.get_possible_moves_on_position((6, 2), player=PLAYER_ONE, use_overwrite=True) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE], [HOLE, PLAYER_TWO, PLAYER_TWO, PLAYER_TWO, PLAYER_TWO, PLAYER_TWO, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, EMPTY], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_TWO, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_TWO, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_TWO, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_TWO, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_TWO, HOLE, HOLE, HOLE, HOLE]])
def test_get_possible_moves_three_player_choice(self): board = Board("""\ 3 0 0 0 3 3 2 1 3 0 2 0 0 c 0 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(3, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_TWO, PLAYER_ONE, PLAYER_THREE], [EMPTY, PLAYER_ONE, EMPTY], [EMPTY, PLAYER_ONE, EMPTY]]) self.assert_board(next_game_states[1].board, [[PLAYER_ONE, PLAYER_TWO, PLAYER_THREE], [EMPTY, PLAYER_TWO, EMPTY], [EMPTY, PLAYER_TWO, EMPTY]]) self.assert_board(next_game_states[2].board, [[PLAYER_TWO, PLAYER_THREE, PLAYER_ONE], [EMPTY, PLAYER_THREE, EMPTY], [EMPTY, PLAYER_THREE, EMPTY]])
def test_get_possible_moves_boese_lost_trace(self): board = Board("""\ 2 0 0 0 8 9 - - - - - - - - - - - 2 - - - 2 - - - 2 2 2 0 2 2 2 - - - 2 - - - 2 - - - - 2 - - - 2 - - - - 2 - - - 2 - - - - 1 - - - 1 - - - - - - - - - - - 7 2 2 <-> 2 1 0 1 2 6 <-> 6 1 0 """) game = GameState(board) next_game_states = game.get_possible_moves_on_position((4, 2), player=PLAYER_ONE, use_overwrite=True) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE], [HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE], [HOLE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, HOLE], [HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE], [HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE], [HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE], [HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE], [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE]])
def test_parse_transitions(self): board = Board(TestBoardParsing.transition_board) self.assertEqual(board.transitions[((0, 0), Direction.TOP_LEFT)], ((1, 0), Direction.BOTTOM_LEFT)) self.assertEqual(board.transitions[((1, 0), Direction.TOP_RIGHT)], ((0, 0), Direction.BOTTOM_RIGHT)) self.assertEqual(board.transitions[((0, 1), Direction.LEFT)], ((0, 1), Direction.TOP)) self.assertEqual(board.transitions[((0, 1), Direction.BOTTOM)], ((0, 1), Direction.RIGHT))
def __init__(self, map_path, port, time_limit, depth_limit, clients, group_to_player): self.map_path = map_path with open(map_path) as map_file: self.board = Board(map_file.read()) self.server = Server(self.board, time_limit, depth_limit, port, group_to_player) self.clients = clients self.port = port
def test_get_possible_moves_must_capture_minimum_one_enemy(self): board = Board("""\ 2 1 0 0 3 3 1 0 0 0 0 0 0 0 0 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(0, len(next_game_states))
def test_get_possible_moves_can_not_move_on_other_player(self): board = Board("""\ 2 0 0 0 3 3 2 0 0 0 2 0 0 0 1 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE, use_overwrite=True) self.assertEqual(0, len(next_game_states))
def test_get_possible_moves_expansion_only_with_overwrite(self): board = Board("""\ 2 0 0 0 3 3 0 0 0 0 x 0 0 0 0 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(Field.PLAYER_ONE, use_overwrite=True) self.assertEqual(0, len(next_game_states))
def test_get_possible_moves_boese02(self): board = Board("""\ 2 0 0 0 2 10 2 2 2 0 2 2 2 2 2 2 - - - - - 1 2 0 - - 0 0 6 <-> 9 0 2 """) game = GameState(board) next_game_states = game.get_possible_moves_on_position((3, 0), player=PLAYER_ONE) self.assertEqual(0, len(next_game_states))
def test_normal_scoring(self): board = Board("""\ 2 1 0 0 3 3 1 0 0 0 0 0 0 0 0 """) game = GameState(board) scores = game.calculate_scores() self.assertEqual(scores[PLAYER_ONE], 1.0) self.assertEqual(scores[PLAYER_TWO], -1.0)
def test_get_possible_moves_eight_player_choice(self): board = Board("""\ 8 0 0 0 3 3 2 1 3 4 2 5 6 c 7 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(8, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_TWO, PLAYER_ONE, PLAYER_THREE], [PLAYER_FOUR, PLAYER_ONE, PLAYER_FIVE], [PLAYER_SIX, PLAYER_ONE, PLAYER_SEVEN]]) self.assert_board(next_game_states[1].board, [[PLAYER_ONE, PLAYER_TWO, PLAYER_THREE], [PLAYER_FOUR, PLAYER_TWO, PLAYER_FIVE], [PLAYER_SIX, PLAYER_TWO, PLAYER_SEVEN]]) self.assert_board(next_game_states[2].board, [[PLAYER_TWO, PLAYER_THREE, PLAYER_ONE], [PLAYER_FOUR, PLAYER_THREE, PLAYER_FIVE], [PLAYER_SIX, PLAYER_THREE, PLAYER_SEVEN]]) self.assert_board(next_game_states[3].board, [[PLAYER_TWO, PLAYER_FOUR, PLAYER_THREE], [PLAYER_ONE, PLAYER_FOUR, PLAYER_FIVE], [PLAYER_SIX, PLAYER_FOUR, PLAYER_SEVEN]]) self.assert_board(next_game_states[4].board, [[PLAYER_TWO, PLAYER_FIVE, PLAYER_THREE], [PLAYER_FOUR, PLAYER_FIVE, PLAYER_ONE], [PLAYER_SIX, PLAYER_FIVE, PLAYER_SEVEN]]) self.assert_board(next_game_states[5].board, [[PLAYER_TWO, PLAYER_SIX, PLAYER_THREE], [PLAYER_FOUR, PLAYER_SIX, PLAYER_FIVE], [PLAYER_ONE, PLAYER_SIX, PLAYER_SEVEN]]) self.assert_board(next_game_states[6].board, [[PLAYER_TWO, PLAYER_SEVEN, PLAYER_THREE], [PLAYER_FOUR, PLAYER_SEVEN, PLAYER_FIVE], [PLAYER_SIX, PLAYER_SEVEN, PLAYER_ONE]]) self.assert_board(next_game_states[7].board, [[PLAYER_TWO, PLAYER_EIGHT, PLAYER_THREE], [PLAYER_FOUR, PLAYER_EIGHT, PLAYER_FIVE], [PLAYER_SIX, PLAYER_EIGHT, PLAYER_SEVEN]])
def test_get_possible_moves_boese03(self): board = Board("""\ 2 0 0 0 2 10 2 2 2 1 0 2 2 2 2 2 - - - - 0 1 2 0 - - 0 0 6 <-> 9 0 2 """) game = GameState(board) next_game_states = game.get_possible_moves_on_position((4, 0), player=PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE], [HOLE, HOLE, HOLE, HOLE, EMPTY, PLAYER_ONE, PLAYER_TWO, EMPTY, HOLE, HOLE]])
def test_get_possible_moves_five_player_inversion(self): board = Board("""\ 5 0 0 0 3 3 5 4 1 0 2 3 i 0 0 """) game = GameState(board) next_game_states = game.get_possible_moves_on_position((0, 2), player=PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_ONE, PLAYER_FIVE, PLAYER_TWO], [EMPTY, PLAYER_TWO, PLAYER_FOUR], [PLAYER_TWO, EMPTY, EMPTY]])
def test_get_possible_moves_walk_bottom_left(self): board = Board("""\ 2 0 0 0 3 3 0 0 1 0 2 0 0 0 0 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[EMPTY, EMPTY, PLAYER_ONE], [EMPTY, PLAYER_ONE, EMPTY], [PLAYER_ONE, EMPTY, EMPTY]])
def test_get_possible_moves_basic_inversion(self): board = Board("""\ 2 0 0 0 3 3 0 0 1 0 2 0 i 0 0 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[EMPTY, EMPTY, PLAYER_TWO], [EMPTY, PLAYER_TWO, EMPTY], [PLAYER_TWO, EMPTY, EMPTY]])
def test_parse_basic_board(self): board = Board(TestBoardParsing.simple_board) self.assertEqual(board.n_players, 2) self.assertEqual(board.n_overwrite, 3) self.assertEqual(board.n_bombs, 4) self.assertEqual(board.s_bombs, 5) self.assertEqual(board.height, 2) self.assertEqual(board.width, 4) self.assertEqual(board[(0, 0)], HOLE) self.assertEqual(board[(1, 0)], EMPTY) self.assertEqual(board[(2, 0)], PLAYER_ONE) self.assertEqual(board[(3, 0)], PLAYER_TWO) self.assertEqual(board[(0, 1)], EXPANSION) self.assertEqual(board[(1, 1)], CHOICE) self.assertEqual(board[(2, 1)], INVERSION) self.assertEqual(board[(3, 1)], BONUS)
def test_get_possible_moves_eight_player_inversion(self): board = Board("""\ 8 0 0 0 3 3 5 4 1 6 2 3 i 7 8 """) game = GameState(board) next_game_states = game.get_possible_moves_on_position((0, 2), player=PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_SIX, PLAYER_FIVE, PLAYER_TWO], [PLAYER_SEVEN, PLAYER_TWO, PLAYER_FOUR], [PLAYER_TWO, PLAYER_EIGHT, PLAYER_ONE]])
def test_get_possible_moves_holes_stop_move(self): board = Board("""\ 2 0 0 0 3 3 1 0 0 0 2 - 0 0 0 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_ONE, EMPTY, EMPTY], [EMPTY, PLAYER_ONE, HOLE], [EMPTY, EMPTY, PLAYER_ONE]])
def test_get_possible_moves_walk_multipath(self): board = Board("""\ 2 0 0 0 3 3 1 0 0 0 2 2 0 0 0 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_ONE, EMPTY, EMPTY], [EMPTY, PLAYER_ONE, PLAYER_TWO], [EMPTY, EMPTY, PLAYER_ONE]])
class BoardMessage(Message): def __init__(self, board=None): super().__init__() self.board = board def read_from_conn(self, conn): self.read_message_length(conn) board_string = read_string(conn, self.message_length) self.board = Board(board_string) def write_to_conn(self, conn): write_8_bit_int(conn, 2) board_string = self.board.__string__().encode('utf-8') self.message_length = len(board_string) write_32_bit_int(conn, self.message_length) conn.send(board_string)
def test_get_possible_moves_inversion_with_transition(self): board = Board("""\ 2 0 0 0 3 3 2 - 1 2 2 1 i - - 0 0 0 <-> 0 1 6 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_TWO, HOLE, PLAYER_TWO], [PLAYER_TWO, PLAYER_TWO, PLAYER_TWO], [PLAYER_TWO, HOLE, HOLE]])
def test_get_possible_moves_walk_through_transition(self): board = Board("""\ 2 0 0 0 3 3 0 2 0 0 1 0 0 0 0 1 2 4 <-> 1 0 0 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[EMPTY, PLAYER_ONE, EMPTY], [EMPTY, PLAYER_ONE, EMPTY], [EMPTY, PLAYER_ONE, EMPTY]])
def test_get_possible_moves_walk_through_loop_transition(self): board = Board("""\ 2 0 0 0 1 9 1 - 2 - 0 - 2 - 1 0 0 6 <-> 8 0 2 2 0 6 <-> 0 0 2 4 0 6 <-> 2 0 2 6 0 6 <-> 4 0 2 8 0 6 <-> 6 0 2 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_ONE, HOLE, PLAYER_ONE, HOLE, PLAYER_ONE, HOLE, PLAYER_ONE, HOLE, PLAYER_ONE]])
def test_get_possible_moves_capture_expansion_rows(self): board = Board("""\ 2 0 0 0 3 3 0 0 0 0 x 0 0 0 1 """) game = GameState(board) next_game_states = game.get_possible_moves_for_player(PLAYER_ONE, use_overwrite=True) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[PLAYER_ONE, EMPTY, EMPTY], [EMPTY, PLAYER_ONE, EMPTY], [EMPTY, EMPTY, PLAYER_ONE]]) self.assertEqual(0, next_game_states[0].player_overwrites[PLAYER_ONE])
def test_typical_interaction(self): board = Board("""\ 2 0 0 0 2 2 0 0 0 0 """) server = network.BasicServer(board) client = network.BasicClient(14) server.start() def connect_client(): client.start() t = threading.Thread(target=connect_client) t.start() self.assertEqual(14, server.accept_client()) self.assertTrue(server.clients_by_group[14]) server.set_player_for_group(14, Field.PLAYER_ONE) t.join() self.assertEqual(Field.PLAYER_ONE, client.player) server.send_player_message(Field.PLAYER_ONE, network.MoveRequestMessage(1000, 0)) rec = client.read_message() self.assertEqual(1000, rec.time_limit) self.assertEqual(0, rec.depth_limit) client.send_message(network.MoveResponseMessage((1, 2), None)) rec = server.read_player_message(Field.PLAYER_ONE, network.MoveResponseMessage) self.assertEqual((1, 2), rec.pos) self.assertEqual(None, rec.choice) client.stop() server.stop()
def run_server_example(): logging.basicConfig(level=logging.INFO, format='%(message)s') board = Board("""\ 2 0 2 0 6 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 """) # Play actual game server = Server(board, 0, 1) server.start() server.join()
def test_board_message(self): board = Board("""\ 2 1 2 3 3 3 0 0 0 1 0 2 0 0 0 0 0 0 <-> 2 0 2 """) board_message = network.BoardMessage(board) board_message.write_to_conn(self.server_conn) answer = network.read_message_from_conn(self.client) self.assertEqual(2, answer.board.n_players) self.assertEqual(1, answer.board.n_overwrite) self.assertEqual(Field.PLAYER_ONE, answer.board[(0, 1)]) self.assertEqual(((2, 0), Direction.LEFT), answer.board.transitions[((0, 0), Direction.TOP)])
def test_get_possible_moves_bomb_two(self): board = Board("""\ 2 0 2 2 3 3 0 0 0 - - 0 0 0 0 """) game = GameState(board) game.bomb_phase = True next_game_states = game.get_possible_bomb_move_on_position((0, 0), player=PLAYER_ONE) self.assertEqual(1, len(next_game_states)) self.assert_board(next_game_states[0].board, [[HOLE, HOLE, HOLE], [HOLE, HOLE, HOLE], [EMPTY, EMPTY, EMPTY]]) self.assertEqual(next_game_states[0].player_bombs[PLAYER_ONE], 1) self.assertEqual(next_game_states[0].player_bombs[PLAYER_TWO], 2)