Ejemplo n.º 1
0
 def to_uci_usi(self):
     if self.starting_fen is None:
         board_temp = Board(self.board.start_blue, self.board.start_red)
         fen = board_temp.to_fen(Color.BLUE, 0)
     else:
         fen = self.starting_fen
     res_l = ["position", "fen", fen, "moves"]
     for action in self.actions:
         if action is None:
             res_l.append("a1a1")
         else:
             res_l.append(action.to_uci_usi())
     return " ".join(res_l)
Ejemplo n.º 2
0
class TestBoardGwee(BoardTest):

    def setUp(self) -> None:
        self.board = Board(start_blue="gwee", start_red="gwee")

    def test_action_elephant(self):
        self.assertEqual(len(self.board.get(0, 1).get_actions()), 1)
        self.assertEqual(len(self.board.get(0, 6).get_actions()), 0)
        self.assertEqual(len(self.board.get(9, 2).get_actions()), 0)
        self.assertEqual(len(self.board.get(9, 7).get_actions()), 1)
        self.board.set(0, 1, None)
        self.board.set(3, 3, Elephant(3, 3, Color.BLUE, self.board))
        self.board.invalidate_action_cache()
        self.assertEqual(len(self.board.get(3, 3).get_actions()), 3)

    def test_action_horse(self):
        self.assertEqual(len(self.board.get(0, 2).get_actions()), 1)
        self.assertEqual(len(self.board.get(0, 7).get_actions()), 2)
        self.assertEqual(len(self.board.get(9, 6).get_actions()), 1)
        self.assertEqual(len(self.board.get(9, 1).get_actions()), 2)

    def test_get_all_actions(self):
        self.assertEqual(len(self.board.get_actions(Color.BLUE)), 31)

    def test_read(self):
        board = Board.from_string("""
            .HEG.GE.R
            .....K...
            .......RH
            S.S...C.S
            .......S.
            .s..sr.C.
            s......ss
            ...kce.c.
            .........
            .h..ggher""")
        self.assertEqual(len(board.get_actions(Color.BLUE)), 1)
        self.assertFalse(board.is_finished(Color.BLUE))

    def test_read2(self):
        board = Board.from_string("""
            ...K.....
            cH.......
            .H..G....
            ......s..
            ...S..s..
            ....s....
            .........
            .hRk.gh..
            .C..gE...
            .........""")
        self.assertEqual(len(board.get_actions(Color.RED)), 1)
        self.assertFalse(board.is_finished(Color.RED))

    def test_to_fen(self):
        fen = self.board.to_fen(Color.BLUE, 105)
        self.assertIn("/", fen)
        self.assertIn("54", fen)

    def test_from_fen(self):
        board = Board.from_fen("rnba1abnr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RNBA1ABNR w - - 0 1")
        self.assertEqual(board.get_score(Color.BLUE), 72)

    def test_game_from_fen(self):
        game = Game.from_fen(RandomPlayer(Color.BLUE),
                             RandomPlayer(Color.RED),
                             "rnba1abnr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RNBA1ABNR w - - 0 1")
        self.assertEqual(game.current_player, Color.BLUE)
        self.assertEqual(game.round, 0)

    def test_uci_usi(self):
        game = Game.from_uci_usi(
            RandomPlayer(Color.BLUE),
            RandomPlayer(Color.RED),
            "position fen r1bakab1r/9/2ncc4/1pppn2R1/9/9/BPP1P1P1P/2N1CN1C1/4K4/3A1A2R w - - 0 1 "
            "moves g4h4 f10e9 i1g1 d10d9 g1g9 d8d10 h3h5 a10a6 e3i3 e7f5 h5e5 a6e6 i3i10 d10f10 "
            "h7f7 f5h4 e5e3 e6i6 g9g10 h4f3 g10f10")
        self.assertIsNotNone(game)

    def test_uci_usi4(self):
        game = Game.from_uci_usi(
            RandomPlayer(Color.BLUE),
            RandomPlayer(Color.RED),
            "position fen rbna1abnr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RNBA1ABNR w - - 0 1 "
            "moves b1c3 a7b7 b3e3 a10a4 c3a4 f10e10 e4d4 e7f7 g1e4 e9f9 h1g3 c7c6 h3f3 f7e7 e4g7 b8b2 a4b2 i10i8 a0a9 "
            "f9f8 g4f4")
        action = game.get_current_actions()[0]
        game.apply_action(action, invalidate_cache=False)
        game.reverse_action()
        print(game.get_current_actions())

    def test_uci_usi5(self):
        game = Game.from_uci_usi(
            RandomMCTSPlayer(Color.BLUE, n_simulations=100),
            RandomMCTSPlayer(Color.RED, n_simulations=100),
            "position fen rbna1anbr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RBNA1ABNR w - - 0 1 moves a4b4 i10i9 "
            "h1g3 e9d8 e4e5 d10d9 g4f4 e7f7 i1i2 d8e8 a1a4 i9h9 e2d2 e8d8 h3f3 h9e9 i2i1 a10a8 a4a1 e9d10 g3i2 i7i6 "
            "b4b5 d10e9 f4g4 e9i9 d1e1 f7e7 c4d4 i9f9 d2d1 a8a10 e5f5 e7f7 c1d3 f9f8 e1e2 b8e8 i2g3 i6i5 d4e4 e8c8 "
            "d3c1 d8e9 e2f2 f7e7 g4f4 c10b8 f5e5 f8d8 e5d5 c8c1 i4i5 h10f7 b5b6 d8f8 b6b7 e9d8 g3i4 g7g6 f1e1 c1c10 "
            "a1a2 c7b7 i5h5 f8e8 d5e5 h8h1 i1i3 g10f8 f2f1 c10i10 a2a7 g6h6 e5e6 e8e9 d1e2 f8h7 a7b7 e7d7 b7b8 a1a1 "
            "f3f7 a1a1 h5i5 a1a1 b8b5 a10a4 b3b7 e9d10 e6f6 f10e10 f6g6 d7d6 f4f5 a4c4 g6g7 h6i6 b5a5 h1h9 i3c3 i6i5 "
            "f5e5 i10i4 c3b3 h9h1 e5d5 c4c2 e2d1 h7g9 f7i7 c2h2 b3d3 h2c2 a5a9 c2c8 d3h3 i4i6 h3h10 g9i10 a9c9 d6d5 "
            "i7e7 d8e9 d1e2 i10h8 c9d9")
        action = game.get_current_actions()[0]
        game.apply_action(action, invalidate_cache=False)
        game.reverse_action()

    def test_uci_usi3(self):
        uci_usi = "position fen rbna1anbr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RBNA1ABNR w - - 0 1 moves i4h4 " \
                  "g10f8 h1g3 b8g8 a4b4 g7f7 g4f4 g8g2 c1d3 e9e9 b3e3 h8e8 h4g4 g2c2 b1d4 b10d7 h3f3 i10i8 i1h1 d7f4 " \
                  "g4f4 i8g8 g3i4 i7h7 h1h7 g8g2 e2e1 f8h7 d1e2 c10d8 f3d1 d10e10 f1f2 c7b7 a1a5 c2f2 e2f2 e7e6 f2e2 " \
                  "f7f6 a5h5 g2g7 h5h6 a10d10 d1f1 e9f9 f4f5 f9e9 f5f6 e6d6 f6g6 g7d7 e4e5 e9d9 i4g5 e8b8 g6f6 d7g7 " \
                  "g5h7 g7g1 h7i9 g1i1 i9g8 b8g8 h6h10 f10e9 e2f2 g8b8 e1e2 b8e8 d3e1 i1i3 f2f3 b7c7 f1d1 c7d7 d4g6 " \
                  "i3i7 f6e6 d8b7 e6d6 i7i2 f3f2 b7d6 e5e6 i2i5 e6d6 d10b10 d6d7 i5d5 g6d4 d5d7 d1d7 b10a10 d7d1 e9d8 " \
                  "h10h9 d9d10 d1d8 e10f10 h9h10 d10e10 d8d1 a7b7 d1f1 a10a2 e2d3 a2f2 f1d1 b7c7 h10h8 e10e9 d4g6 " \
                  "f2f8 h8h9 e9e10 h9c9 f10e9 c9c10 e9d10 c10c7 e10f10 c7e7 d10e9 e7i7 f10f9 i7i9 f9f10 e1g2 e8e10 " \
                  "g2h4 e9f9 h4i6 f8f4 i6h8 f4f1 d3d2 f9f8 g6d4 f1f2 d2d3 f2f3 i9i1 f8e9 i1e1 f3f4 h8g6 f4f6 d3d3 " \
                  "e10g10 d3d3 f6f2 e1e2 f2e2 d3e2 g10e10 e2e2 e10e4 e2e2 f10e10 g6e7 e4e8 e7d5 e10f10 d5c7 e8e10 " \
                  "d1d6 e10e7 d6d3 e7e10 d3f1 e10e5 e2f2 f10e10 d4g2 e9d9 g2e5 d9e9 e5b7 e9f8 c7e8 f8e9 e8g9 e9f9 " \
                  "f1f9 e10f10 f9f1 f10f10"
        game = Game.from_uci_usi(
            RandomPlayer(Color.BLUE),
            RandomPlayer(Color.RED),
            uci_usi)
        self.assertIsNotNone(game)

    def test_strange_move_chariot(self):
        board = Board.from_fen("2b1akb1B/2r6/4C2c1/5p3/1p1P1Pp2/9/1PnN5/5R3/2B1K4/5AN2 w - - 1 67")
        actions = board.get(2, 5).get_actions()
        self.assertNotIn(Action(2, 5, 1, 4), actions)