class TestEngine(unittest.TestCase):
    def setUp(self):
        self.board = Board()

    def test_isOnBoard(self):
        self.assertEquals(self.board.on_board([1, 1]), True)

    def test_isNotOnBoard(self):
        self.assertEquals(self.board.on_board([1, 9]), False)
Exemple #2
0
def empty_board():
    sb = Board()
    sb.pieces = [
        [None, None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None, None],
    ]

    return sb
Exemple #3
0
 def test_init_creates_expected_cardstacks(self, mock_deck):
     mock_deck.__len__.return_value = 40
     mock_deck.card_colors = ('red', 'blue')
     mock_deck.card_numbers = (1, 2, 2, 3)
     board = Board(mock_deck)
     # Check that there's a stack for each color in the deck
     for color in mock_deck.card_colors:
         assert isinstance(board.card_stacks[color], CardStack)
Exemple #4
0
 def test_init(self):
     board = Board(None)
     self.assertEqual(len(board.board), Common.board_size**2)
     self.assertIsInstance(board.board[0], Rook)
     self.assertIsInstance(board.board[7 * Common.board_size + 6], Knight)
     self.assertIsInstance(board.board[6 * Common.board_size + 7], Pawn)
     self.assertEqual(board.board[6 * Common.board_size + 7].color,
                      Common.black)
Exemple #5
0
    def request_move(self, board, legal, player_num):
        # 現在の盤面から決まった回数プレイアウトして、一番良い手を返す
        root_board = Board(board)
        # game_tree[board_num][pos] = board_numの盤面で手番プレイヤーがposに打ったときに遷移するboardの番号
        game_tree = [[-1] * 81 for _ in range(60000)]
        # win_rate[board_num] = (そのboardに遷移する手を打ったときの勝率, シミュレーションでそのboardにたどり着いた回数)
        win_rate = [(0, 0)] * 60000
        # visited_num[board_num] = そのboardを訪れた回数
        visited_num = [0] * 60000
        board_count = 1
        for _ in range(self.playout_num):
            # プレイアウトしてrecordを作る
            now_board = copy.deepcopy(root_board)
            record = [(-1, -1, player_num)]
            now_player_num = player_num
            now_board_num, now_state = 0, 0
            random_selected = False
            while now_state == 0:
                now_legal = legal if now_board_num == 0 else now_board.legal_moves(
                    record[-1][1])
                move = -1
                if now_board_num != 0 and (
                        random_selected
                        or visited_num[now_board_num] < self.THRESHOLD):
                    move = self.random_move(now_legal)
                    random_selected |= True
                else:
                    if visited_num[now_board_num] == (0 if now_board_num == 0
                                                      else self.THRESHOLD):
                        # 展開する
                        for pos in range(81):
                            game_tree[now_board_num][pos] = board_count
                            board_count += 1
                    move = self.ucb_move(now_board_num, now_legal, game_tree,
                                         win_rate)
                now_board.mark(move, now_player_num)
                if not random_selected:
                    record.append((now_board_num, move, now_player_num))
                    now_board_num = game_tree[now_board_num][move]
                now_player_num = now_player_num ^ 3
                now_state = now_board.check_state()

            # win_rateを更新する
            visited_num[0] += 1
            for board_num, pos, player in record:
                if board_num < 0:
                    continue
                next_board_num = game_tree[board_num][pos]
                visited_num[next_board_num] += 1
                win, num = win_rate[next_board_num]
                win_rate[next_board_num] = (
                    win + 1, num + 1) if now_state == player else (win,
                                                                   num + 1)
        return self.best_move(0, legal, game_tree, win_rate)
Exemple #6
0
 def test_init_creates_discard_stats(self, mock_deck):
     mock_deck.__len__.return_value = 40
     mock_deck.card_colors = ('red', 'blue')
     mock_deck.card_numbers = (1, 2, 2, 3)
     board = Board(mock_deck)
     # Assert there's a discard pile with a length:
     assert len(board.discard) == 0
     # Assert there's a discard_stats list with one counter per card number
     for color in mock_deck.card_colors:
         for number in mock_deck.card_numbers:
             assert len(board.discard_stats[color]) == len(set(mock_deck.card_numbers))
Exemple #7
0
    def __init__(self, host: str, port: int) -> None:
        self.running = True
        self.players = []

        self.connection_information = (host, port)
        self.socket_selector = selectors.DefaultSelector()

        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server_socket.setblocking(False)
        self.server_socket.bind(self.connection_information)
        self.server_socket.listen(2)

        self.socket_selector.register(
            self.server_socket, selectors.EVENT_READ, data=self.accept_handler
        )

        logger.info(f"Serving on {self.connection_information}")

        self.board = Board()

        logger.info(f"Game state initiated")
Exemple #8
0
 def __init__(self, players, deck_seed=False):
     num_players = len(players)
     if not 2 <= num_players <= 5:
         raise Exception("There must be between 2 and 5 players")
     for player in players:
         if not isinstance(player, Player):
             raise Exception("All players must inherit from the Player class.")
     self.colors = ('r', 'y', 'g', 'w', 'b')  # TODO: rainbow/mixed/wilds
     self.numbers = (1, 1, 1, 2, 2, 3, 3, 4, 4, 5)
     self.players = players
     self.deck = Deck(colors=self.colors, numbers=self.numbers, seed=deck_seed)
     self.player_hands = [[] for _ in range(len(self.players))]
     self.master_game_state = GameState(Board(self.deck), self.player_hands)
     self.game_almost_over = None
Exemple #9
0
class GameServer:
    def __init__(self, host: str, port: int) -> None:
        self.running = True
        self.players = []

        self.connection_information = (host, port)
        self.socket_selector = selectors.DefaultSelector()

        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server_socket.setblocking(False)
        self.server_socket.bind(self.connection_information)
        self.server_socket.listen(2)

        self.socket_selector.register(
            self.server_socket, selectors.EVENT_READ, data=self.accept_handler
        )

        logger.info(f"Serving on {self.connection_information}")

        self.board = Board()

        logger.info(f"Game state initiated")

    def get_number_of_players(self):
        return len(self.players)

    def accept_handler(self, file_obj_socket):
        client_socket, address = file_obj_socket.accept()
        client_socket.setblocking(False)

        peername = client_socket.getpeername()

        if len(self.players) < 2:
            client_socket.sendall(b"Success")
            self.players.append({
                "id": Players.P1 if len(self.players) == 0 else Players.P2,
                "file_obj": file_obj_socket,
                "socket": client_socket
            })

            logger.info(f"Connection from {peername}")

            self.socket_selector.register(
                client_socket, selectors.EVENT_READ, data=self.receive_handler
            )
        else:
            logger.info(f"Rejecting connection from {peername}")
            client_socket.sendall(b"Full server")
            client_socket.close()

    def receive_handler(self, file_obj_socket):
        data = file_obj_socket.recv(9)
        if data:
            self.data_received(file_obj_socket, data)
        else:
            logger.info(f"Disconnect from {file_obj_socket.getpeername()}")
            self.socket_selector.unregister(file_obj_socket)
            for player in self.players:
                if player["file_obj"] is file_obj_socket:
                    del player

            file_obj_socket.close()

    def data_received(self, file_obj_socket, data):
        message: str = data.decode("utf-8")
        logger.info(f"Received from {file_obj_socket.getpeername()}: {message}")

        command = message[:3]

        if command == "mov":
            from_coord = message[4:6]
            to_coord = message[7:9]

            result = self.board.move(*cn(from_coord, to_coord))

            if result is False:
                # move piece to origin position in client
                reverse_message: bytes = str.encode(f"mov:{to_coord},{from_coord}")
                file_obj_socket.sendall(reverse_message)
            else:
                for player in self.players:
                    if player["socket"] is not file_obj_socket:
                        player["socket"].sendall(data)

                last_piece_removed = self.board.get_last_piece_removed()
                if last_piece_removed is not None:
                    char_coord: str = nc(last_piece_removed)
                    rm_message: bytes = str.encode(f"rmv:{char_coord},__")
                    for player in self.players:
                        player["socket"].sendall(rm_message)

                last_piece_king = self.board.get_last_piece_king()
                if last_piece_king is not None:
                    char_coord: str = nc(last_piece_king)
                    king_message: bytes = str.encode(f"kin:{char_coord},__")
                    for player in self.players:
                        player["socket"].sendall(king_message)

        elif command == "ptn":
            winner = self.board.game_winner()
            if winner is not None:
                for player in self.players:
                    if player["id"] is winner:
                        player["socket"].sendall(b"win:__,__")
                    else:
                        player["socket"].sendall(b"los:__,__")
            else:
                self.board.next_turn()
                file_obj_socket.sendall(b"ntn:__,__")
                for player in self.players:
                    if player["socket"] is not file_obj_socket:
                        player["socket"].sendall(b"ytn:__,__")
        else:
            logger.error(f"Unknown command {message}")

    def main_loop(self):
        try:
            while self.running:
                events = self.socket_selector.select()
                for key, _ in events:
                    callback = key.data
                    callback(key.fileobj)
        except KeyboardInterrupt:
            logger.info("Closing server")

        self.server_socket.close()
        self.socket_selector.close()

    def close_server(self):
        self.running = False
Exemple #10
0
 def __init__(self):
     self._history = []
     self._initial_board = Board(self)
     self._board = self._initial_board
Exemple #11
0
    Otherwise return None
    """
    for xy, color in board._b.items():
        if color == EMPTY_COLOR:
            continue
        str_x, str_y = xy.split(',')
        x = int(str_x)
        y = int(str_y)

        for x_step, y_step in [(1, 0), (0, 1), (1, 1), (1, -1)]:
            if get_others(board._b, x, y, x_step, y_step) == {color}:
                return color
    return None


if __name__ == '__main__':
    board = Board(width=COLS, height=ROWS)
    game_loop_v3(board)


"""
print_board(board)
put_stone(board, 0, 'blue')
print_board(board)
put_stone(board, 2, 'blue')
print_board(board)
put_stone(board, 0, 'blue')
print_board(board)
"""

Exemple #12
0
def initial_board():
    return Board()
Exemple #13
0
 def test_empty_board(self):
     board = Board.create_empty_board(None)
     self.assertEqual(len(board.board), Common.board_size ** 2)
     for field in board.board:
         self.assertIsNone(field)
 def setUp(self):
     self.board = Board()
Exemple #15
0
 def test_empty_board(self):
     board = Board.create_empty_board(None)
     self.assertEqual(len(board.board), Common.board_size**2)
     for field in board.board:
         self.assertIsNone(field)
class GameController:

    MAX_MONEY = 1000

    def __init__(self) -> None:
        super().__init__()
        # self.player = Player("Daniele", 1000)
        # self.player2 = Player("peppe", 1000)
        self.props = (
            Prop("Vicolo Corto",
                 60,
                 30,
                 50,
                 2,
                 0,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Vicolo Stretto",
                 60,
                 30,
                 50,
                 2,
                 0,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Station("Stazione Sud", 60, 200),
            Prop("Bastioni Gran Sasso",
                 60,
                 30,
                 50,
                 3,
                 1,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Viale Vesuvio",
                 60,
                 30,
                 50,
                 3,
                 1,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Viale monte rosa",
                 60,
                 30,
                 50,
                 3,
                 1,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Via Accademia",
                 60,
                 30,
                 50,
                 3,
                 2,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Corso Ateneo",
                 60,
                 30,
                 50,
                 3,
                 2,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Piazza Università",
                 60,
                 30,
                 50,
                 3,
                 2,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Via Verdi",
                 60,
                 30,
                 50,
                 3,
                 3,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Corso Raffaello",
                 60,
                 30,
                 50,
                 3,
                 3,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Piazza Dante",
                 60,
                 30,
                 50,
                 3,
                 3,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Via Marco Polo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Corso Magellano",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
            Prop("Largo Colombo",
                 60,
                 30,
                 50,
                 3,
                 4,
                 none=5,
                 complete=8,
                 one=10,
                 two=20,
                 three=30,
                 four=50,
                 hotel=60),
        )

        # self.players = [self.player, self.player2]
        # print(self.player)
        # print(self.player2)
        self.board = Board(self.props)

    def initialize(self):
        pass

    def start_game(self):
        pass

    def add_player(self, name, color):
        p = Player(name, GameController.MAX_MONEY, color)
        self.board.add_player(p)