Exemple #1
0
    def test_oppposing_player_color(self):
        """Tests opposing_player_color returns correct color."""

        game = XiangqiGame()

        self.assertEqual("black", game.opposing_player_color("red"))
        self.assertEqual("red", game.opposing_player_color("black"))
 def test_is_valid_move_elephant(self):
     game = XiangqiGame()
     result_1 = game.is_valid_move_elephant("c1", "a3")
     result_2 = game.is_valid_move_elephant("c1", "e3")
     result_3 = game.is_valid_move_elephant("e1", "e2")
     result_4 = game.is_valid_move_elephant("c10", "a8")
     result_5 = game.is_valid_move_elephant("g10", "e8")
     final_result = (result_1, result_2, result_3, result_4, result_5)
     self.assertEqual(final_result, (True, True, True, True, True))
Exemple #3
0
class AdvisorTest(unittest.TestCase):
    """Test routines for Advisor GamePiece mechanics."""

    def setUp(self) -> None:
        self.game = XiangqiGame()
        self.game.new_game()

    def test_verify_red_advisor_can_move_diagonally_within_palace(self):
        self.assertTrue(self.game.make_move('d1', 'e2'))

    def test_verify_red_advisor_cannot_move_horizontally(self):
        self.assertFalse(self.game.make_move('d1', 'e1'))

    def test_verify_red_advisor_cannot_leave_palace(self):
        self.assertFalse(self.game.make_move('d1', 'c2'))

    def test_verify_black_advisor_can_move_diagonally_within_palace(self):
        self.assertTrue(self.game.make_move('d1', 'e2'))
        self.assertTrue(self.game.make_move('d10', 'e9'))

    def test_verify_black_advisor_cannot_move_horizontally(self):
        self.assertFalse(self.game.make_move('d10', 'e10'))

    def test_verify_black_advisor_cannot_leave_palace(self):
        self.assertFalse(self.game.make_move('d10', 'c9'))
 def test_is_valid_move_cannon(self):
     game = XiangqiGame()
     result_1 = game.is_valid_move_cannon("b3", "b7")
     result_2 = game.is_valid_move_cannon("h3", "e3")
     result_3 = game.is_valid_move_cannon("b8", "d8")
     result_4 = game.is_valid_move_cannon("h8", "h5")
     result_5 = game.is_valid_move_cannon("h3", "a3")
     result_6 = game.is_valid_move_cannon("b8", "b3")
     final_result = (result_1, result_2, result_3, result_4, result_5,
                     result_6)
     self.assertEqual(final_result, (True, True, True, True, False, False))
Exemple #5
0
 def test_readme_code(self):
     game = XiangqiGame()
     move_result = game.make_move('c1', 'e3')
     self.assertTrue(move_result)
     black_in_check = game.is_in_check('black')
     self.assertFalse(black_in_check)
     game.make_move('e7', 'e6')
     state = game.get_game_state()
     self.assertEqual(state, 'UNFINISHED')
Exemple #6
0
    def test_chariot_moves(self):
        """Tests all types of Chariot moves that are valid."""

        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))

        board.get_point_with_pos("a1").set_piece(Chariot("red"))
        board.get_point_with_pos("a10").set_piece(Chariot("black"))

        # test can only capture opposing color and cannot jump
        board.get_point_with_pos("a3").set_piece(Soldier("red"))
        board.get_point_with_pos("a8").set_piece(Soldier("red"))

        red_chariot_moves = game.get_valid_end_pos_array("a1", "R")
        black_chariot_moves = game.get_valid_end_pos_array("a10", "R")
        self.assertEqual(["b1", "c1", "d1", "a2"], red_chariot_moves)
        self.assertEqual(["b10", "c10", "a9", "a8"], black_chariot_moves)
Exemple #7
0
class ChariotTest(unittest.TestCase):
    """Test routines for Chariot GamePiece mechanics."""

    def setUp(self) -> None:
        self.game = XiangqiGame()
        self.game.new_game()

    def test_chariot_can_move_in_all_directions(self):
        self.assertTrue(self.game.make_move('a1', 'a3'))
        self.assertTrue(self.game.make_move('a10', 'a9'))
        self.assertTrue(self.game.make_move('i1', 'i2'))
        self.assertTrue(self.game.make_move('i10', 'i8'))
        self.assertTrue(self.game.make_move('b3', 'b10'))
        self.assertTrue(self.game.make_move('a9', 'h9'))
        self.assertTrue(self.game.make_move('a3', 'a2'))

    def test_chariot_can_capture_piece(self):
        self.assertTrue(self.game.make_move('a1', 'a3'))
        self.assertTrue(self.game.make_move('a7', 'a6'))
        self.assertTrue(self.game.make_move('a4', 'a5'))
        self.assertTrue(self.game.make_move('a6', 'a5'))
        self.assertTrue(self.game.make_move('a3', 'a5'))
 def test_is_valid_move_chariot(self):
     game = XiangqiGame()
     result_1 = game.is_valid_move_chariot("a1", "a2")
     result_2 = game.is_valid_move_chariot("a1", "a3")
     result_3 = game.is_valid_move_chariot("i1", "i2")
     result_4 = game.is_valid_move_chariot("a10", "a8")
     result_5 = game.is_valid_move_chariot("a10", "a6")
     result_6 = game.is_valid_move_chariot("i1", "i5")
     final_result = (result_1, result_2, result_3, result_4, result_5,
                     result_6)
     self.assertEqual(final_result, (True, True, True, True, False, False))
Exemple #9
0
    def test_horse_moves(self):
        """Tests all types of Horse moves that are valid."""

        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))

        board.get_point_with_pos("b1").set_piece(Horse("red"))
        board.get_point_with_pos("b10").set_piece(Horse("black"))

        # test can only capture opposing color pieces
        board.get_point_with_pos("c3").set_piece(Soldier("red"))
        board.get_point_with_pos("c8").set_piece(Soldier("red"))

        red_horse_moves = game.get_valid_end_pos_array("b1", "H")
        black_horse_moves = game.get_valid_end_pos_array("b10", "H")
        self.assertEqual(["d2", "a3"], red_horse_moves)
        self.assertEqual(["a8", "c8", "d9"], black_horse_moves)

        # test can be blocked
        board.get_point_with_pos("c1").set_piece(Soldier("red"))
        board.get_point_with_pos("c10").set_piece(Soldier("red"))

        red_horse_moves = game.get_valid_end_pos_array("b1", "H")
        black_horse_moves = game.get_valid_end_pos_array("b10", "H")
        self.assertEqual(["a3"], red_horse_moves)
        self.assertEqual(["a8", "c8"], black_horse_moves)

        # test can have no moves
        board.get_point_with_pos("b2").set_piece(Soldier("red"))
        board.get_point_with_pos("b9").set_piece(Soldier("red"))

        red_horse_moves = game.get_valid_end_pos_array("b1", "H")
        black_horse_moves = game.get_valid_end_pos_array("b10", "H")
        self.assertEqual([], red_horse_moves)
        self.assertEqual([], black_horse_moves)
 def test_is_valid_move_horse(self):
     game = XiangqiGame()
     result_1 = game.is_valid_move_horse("b1", "a3")
     result_2 = game.is_valid_move_horse("b1", "c3")
     result_3 = game.is_valid_move_horse("b10", "c8")
     result_4 = game.is_valid_move_horse("h10", "g8")
     result_5 = game.is_valid_move_horse("h10", "i8")
     result_6 = game.is_valid_move_horse("h1", "g3")
     final_result = (result_1, result_2, result_3, result_4, result_5,
                     result_6)
     self.assertEqual(final_result, (True, True, True, True, True, True))
 def test_is_valid_move_general(self):
     game = XiangqiGame()
     result_1 = game.is_valid_move_general("e1", "e2")
     result_2 = game.is_valid_move_general("e1", "d1")
     result_3 = game.is_valid_move_general("e1", "f1")
     result_4 = game.is_valid_move_general("e10", "e9")
     result_5 = game.is_valid_move_general("e10", "d10")
     result_6 = game.is_valid_move_general("e10", "f10")
     final_result = (result_1, result_2, result_3, result_4, result_5,
                     result_6)
     self.assertEqual(final_result, (True, True, True, True, True, True))
 def test_red_wins_1(self):
     game = XiangqiGame()
     result_1 = game.make_move('h3', 'e3')
     result_2 = game.make_move('h8', 'g8')
     result_3 = game.make_move('h1', 'i3')
     result_4 = game.make_move('i10', 'i9')
     result_5 = game.make_move('i1', 'h1')
     result_6 = game.make_move('h10', 'i8')
     result_7 = game.make_move('e3', 'e7')
     result_8 = game.make_move('i8', 'g9')
     result_9 = game.make_move('b3', 'e3')
     result_10 = game.make_move('g8', 'h8')
     result_11 = game.make_move('h1', 'h8')
     result_12 = game.make_move('b8', 'b6')
     result_13 = game.make_move('h8', 'e8')
     result_14 = game.is_in_check("black")
     result_15 = game.make_move('f10', 'e9')
     result_16 = game.make_move('e8', 'i8')
     result_17 = game.make_move('c10', 'e8')
     result_18 = game.make_move('i8', 'i9')
     result_19 = game.make_move('b6', 'b2')
     result_20 = game.make_move('i9', 'g9')
     result_21 = game.make_move('c7', 'c6')
     result_22 = game.make_move('g9', 'g10')
     result_23 = game.get_game_state()
     final_result = (result_1, result_2, result_3, result_4, result_5,
                     result_6, result_7, result_8, result_9, result_10,
                     result_11, result_12, result_13, result_14, result_15,
                     result_16, result_17, result_18, result_19, result_20,
                     result_21, result_22, result_23)
     self.assertEqual(final_result,
                      (True, True, True, True, True, True, True, True, True,
                       True, True, True, True, True, True, True, True, True,
                       True, True, True, True, "RED_WON"))
Exemple #13
0
    def test_cannon_moves(self):
        """Tests all types of Cannon moves that are valid."""

        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))

        board.get_point_with_pos("b4").set_piece(Cannon("red"))
        board.get_point_with_pos("b7").set_piece(Cannon("black"))

        # test cannot capture own piece and must jump to capture
        board.get_point_with_pos("b2").set_piece(Soldier("black"))
        board.get_point_with_pos("b9").set_piece(Soldier("red"))

        red_cannon_moves = game.get_valid_end_pos_array("b4", "C")
        black_cannon_moves = game.get_valid_end_pos_array("b7", "C")
        self.assertEqual(['c4', 'd4', 'e4', 'f4', 'g4', 'h4', 'i4', 'a4', 'b5', 'b6', 'b3'], red_cannon_moves)

        self.assertEqual(['c7', 'd7', 'e7', 'f7', 'g7', 'h7', 'i7', 'a7', 'b8', 'b6', 'b5'], black_cannon_moves)

        # test can capture opposing piece if jumping one piece of different color
        board.get_point_with_pos("b2").set_piece(Soldier("red"))
        board.get_point_with_pos("b9").set_piece(Soldier("black"))

        red_cannon_moves = game.get_valid_end_pos_array("b4", "C")
        black_cannon_moves = game.get_valid_end_pos_array("b7", "C")
        self.assertEqual(['c4', 'd4', 'e4', 'f4', 'g4', 'h4', 'i4', 'a4', 'b5', 'b6', 'b9', 'b3'], red_cannon_moves)

        self.assertEqual(['c7', 'd7', 'e7', 'f7', 'g7', 'h7', 'i7', 'a7', 'b8', 'b6', 'b5', 'b2'], black_cannon_moves)

        # test cannot capture opposing piece if jumping more than two pieces
        board.get_point_with_pos("b2").set_piece(Soldier("red"))
        board.get_point_with_pos("b9").set_piece(Soldier("black"))
        board.get_point_with_pos("b3").set_piece(Soldier("black"))
        board.get_point_with_pos("b8").set_piece(Soldier("red"))

        red_cannon_moves = game.get_valid_end_pos_array("b4", "C")
        black_cannon_moves = game.get_valid_end_pos_array("b7", "C")
        self.assertEqual(['c4', 'd4', 'e4', 'f4', 'g4', 'h4', 'i4', 'a4', 'b5', 'b6'], red_cannon_moves)

        self.assertEqual(['c7', 'd7', 'e7', 'f7', 'g7', 'h7', 'i7', 'a7', 'b6', 'b5'], black_cannon_moves)

        # test can capture opposing piece if jumping one piece of same color
        board.get_point_with_pos("b3").set_piece(None)
        board.get_point_with_pos("b8").set_piece(None)

        board.get_point_with_pos("b4").set_piece(Cannon("black"))

        black_cannon_moves = game.get_valid_end_pos_array("b7", "C")
        self.assertEqual(['c7', 'd7', 'e7', 'f7', 'g7', 'h7', 'i7', 'a7', 'b8', 'b6', 'b5', 'b2'], black_cannon_moves)

        board.get_point_with_pos("b4").set_piece(Cannon("red"))
        board.get_point_with_pos("b7").set_piece(Cannon("red"))

        red_cannon_moves = game.get_valid_end_pos_array("b4", "C")
        self.assertEqual(['c4', 'd4', 'e4', 'f4', 'g4', 'h4', 'i4', 'a4', 'b5', 'b6', 'b9', 'b3'], red_cannon_moves)

        board.get_point_with_pos("b7").set_piece(Cannon("black"))
Exemple #14
0
    def test_soldier_moves(self):
        """Tests all types of Soldier moves that are valid."""

        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))

        board.get_point_with_pos("a4").set_piece(Soldier("red"))
        board.get_point_with_pos("i7").set_piece(Soldier("black"))

        # test can only move forward on own side
        red_soldier_moves = game.get_valid_end_pos_array("a4", "S")
        black_soldier_moves = game.get_valid_end_pos_array("i7", "S")
        self.assertEqual(["a5"], red_soldier_moves)
        self.assertEqual(["i6"], black_soldier_moves)

        game.make_move("a4", "a5")
        game.make_move("i7", "i6")

        red_soldier_moves = game.get_valid_end_pos_array("a5", "S")
        black_soldier_moves = game.get_valid_end_pos_array("i6", "S")
        self.assertEqual(["a6"], red_soldier_moves)
        self.assertEqual(["i5"], black_soldier_moves)

        # test can move forward and sideways on opposing side
        game.make_move("a5", "a6")
        game.make_move("i6", "i5")

        red_soldier_moves = game.get_valid_end_pos_array("a6", "S")
        black_soldier_moves = game.get_valid_end_pos_array("i5", "S")
        self.assertEqual(["b6", "a7"], red_soldier_moves)
        self.assertEqual(["h5", "i4"], black_soldier_moves)

        game.make_move("a6", "b6")
        game.make_move("i5", "h5")

        red_soldier_moves = game.get_valid_end_pos_array("b6", "S")
        black_soldier_moves = game.get_valid_end_pos_array("h5", "S")
        self.assertEqual(["c6", "a6", "b7"], red_soldier_moves)
        self.assertEqual(["i5", "g5", "h4"], black_soldier_moves)

        # test can only capture opposing color
        board.get_point_with_pos("c6").set_piece(Soldier("red"))
        board.get_point_with_pos("a6").set_piece(Soldier("black"))

        board.get_point_with_pos("i5").set_piece(Soldier("red"))
        board.get_point_with_pos("g5").set_piece(Soldier("black"))

        red_soldier_moves = game.get_valid_end_pos_array("b6", "S")
        black_soldier_moves = game.get_valid_end_pos_array("h5", "S")
        self.assertEqual(["a6", "b7"], red_soldier_moves)
        self.assertEqual(["i5", "h4"], black_soldier_moves)
Exemple #15
0
class HorseTest(unittest.TestCase):
    def setUp(self) -> None:
        self.game = XiangqiGame()
        self.game.new_game()

    def test_both_teams_horses_can_move_legally(self):
        self.assertTrue(self.game.make_move('h1', 'g3'))
        self.assertTrue(self.game.make_move('b10', 'c8'))
        self.assertTrue(self.game.make_move('b1', 'a3'))
        self.assertTrue(self.game.make_move('h10', 'g8'))

    def test_red_horse_cant_move_when_path_is_obstructed_by_own_piece(self):
        self.assertTrue(self.game.make_move('h1', 'g3'))
        self.assertTrue(self.game.make_move('g7', 'g6'))
        self.assertFalse(self.game.make_move('g3', 'f5'))

    def test_black_horse_cant_move_when_path_is_obstructed_by_own_piece(self):
        self.assertTrue(self.game.make_move('c4', 'c5'))
        self.assertTrue(self.game.make_move('h10', 'g8'))
        self.assertTrue(self.game.make_move('e4', 'e5'))
        self.assertFalse(self.game.make_move('g8', 'f6'))
Exemple #16
0
    def test_elephant_moves(self):
        """Tests all types of Elephant moves that are valid."""

        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))

        board.get_point_with_pos("c1").set_piece(Elephant("red"))
        board.get_point_with_pos("g1").set_piece(Elephant("red"))
        board.get_point_with_pos("c10").set_piece(Elephant("black"))
        board.get_point_with_pos("g10").set_piece(Elephant("black"))

        # test can only capture opposing color pieces
        red_elephant_moves = game.get_valid_end_pos_array("c1", "E")
        black_elephant_moves = game.get_valid_end_pos_array("c10", "E")
        self.assertEqual(["e3", "a3"], red_elephant_moves)
        self.assertEqual(["e8", "a8"], black_elephant_moves)

        red_elephant_moves = game.get_valid_end_pos_array("g1", "E")
        black_elephant_moves = game.get_valid_end_pos_array("g10", "E")
        self.assertEqual(["i3", "e3"], red_elephant_moves)
        self.assertEqual(["i8", "e8"], black_elephant_moves)

        game.make_move("c1", "e3")
        game.make_move("c10", "e8")

        board.get_point_with_pos("c1").set_piece(Soldier("black"))
        board.get_point_with_pos("c10").set_piece(Soldier("red"))

        red_elephant_moves = game.get_valid_end_pos_array("e3", "E")
        black_elephant_moves = game.get_valid_end_pos_array("e8", "E")
        self.assertEqual(["g5", "c5", "c1"], red_elephant_moves)
        self.assertEqual(["g6", "c10", "c6"], black_elephant_moves)

        # test cannot jump over pieces and test can have no moves
        board.get_point_with_pos("h2").set_piece(Soldier("red"))
        board.get_point_with_pos("h9").set_piece(Soldier("red"))

        red_elephant_moves = game.get_valid_end_pos_array("g1", "E")
        black_elephant_moves = game.get_valid_end_pos_array("g10", "E")
        self.assertEqual([], red_elephant_moves)
        self.assertEqual([], black_elephant_moves)

        # test cannot cross river
        board.get_point_with_pos("c1").set_piece(None)
        board.get_point_with_pos("c10").set_piece(None)

        game.make_move("e3", "c5")
        game.make_move("e8", "c6")

        red_elephant_moves = game.get_valid_end_pos_array("c5", "E")
        black_elephant_moves = game.get_valid_end_pos_array("c6", "E")
        self.assertEqual(["e3", "a3"], red_elephant_moves)
        self.assertEqual(["e8", "a8"], black_elephant_moves)
Exemple #17
0
class ElephantTest(unittest.TestCase):
    """Test routines for Elephant GamePiece mechanics."""

    def setUp(self) -> None:
        self.game = XiangqiGame()
        self.game.new_game()

    def test_verify_red_elephant_cannot_jump_piece(self):
        self.game.make_move('e1', 'e2')
        self.game.make_move('e10', 'e9')
        self.game.make_move('e2', 'f2')
        self.game.make_move('e9', 'e8')
        self.assertFalse(self.game.make_move('g1', 'e3'))

    def test_black_elephant_cannot_jump_piece(self):
        self.assertTrue(self.game.make_move('e4', 'e5'))
        self.assertTrue(self.game.make_move('e7', 'e6'))
        self.assertTrue(self.game.make_move('e5', 'e6'))
        self.assertTrue(self.game.make_move('a7', 'a6'))
        self.assertTrue(self.game.make_move('c1', 'e3'))
        self.assertTrue(self.game.make_move('b10', 'a8'))
        self.assertTrue(self.game.make_move('e6', 'f6'))
        self.assertTrue(self.game.make_move('a6', 'a5'))
        self.assertTrue(self.game.make_move('f6', 'f7'))
        self.assertTrue(self.game.make_move('i7', 'i6'))
        self.assertTrue(self.game.make_move('f7', 'f8'))
        self.assertTrue(self.game.make_move('g7', 'g6'))
        self.assertTrue(self.game.make_move('f8', 'f9'))
        self.assertFalse(self.game.make_move('g10', 'e8'))

    def test_black_elephant_cannot_cross_river(self):
        self.game.make_move('g10', 'e8')
        self.game.make_move('e8', 'g6')
        self.game.make_move('i4', 'i5')
        self.assertFalse(self.game.make_move('g6', 'i4'))
Exemple #18
0
class XiangqiGameTester(unittest.TestCase):
    def setUp(self):
        self.game = XiangqiGame()

    def test_move_exposes_general(self):
        red_soldier = self.game._board[4]['e']
        self.game._board[4]['e'] = None
        self.game._board[7]['e'] = red_soldier
        red_soldier.set_coordinates('e', 7)

        self.assertTrue(
            self.game.move_exposes_general(
                { "row": 7, "column": 'e' },
                { "row": 7, "column": 'd'}
            )
        )

    def test_move_puts_current_player_in_check(self):
        red_soldier = self.game._board[4]['e']
        self.game._board[4]['e'] = None
        self.game._board[7]['e'] = red_soldier
        red_soldier.set_coordinates('e', 7)

        black_rook = self.game._board[10]['a']
        self.game._board[10]['a'] = None
        self.game._board[8]['e'] = black_rook
        black_rook.set_coordinates('e', 8)

        self.assertFalse(
            self.game.is_in_check('red')
        )

        self.assertTrue(
            self.game.move_puts_current_player_in_check(
                { "row": 7, "column": 'e' },
                { "row": 7, "column": 'd'}
            )
        )

        self.assertFalse(
            self.game.is_in_check('red')
        )

    def test_basic_integration(self):
        self.assertTrue(
            self.game.make_move('h3', 'h10')
        )

        self.assertFalse(
            self.game.make_move('f10', 'e9')
        )

    def test_black_check_1(self):
        self.game._board = copy.deepcopy( XiangqiGame.BLANK_BOARD )
        red_general = General('red', 'e', 1)
        red_advisor = Advisor('red', 'd', 1)

        black_chariot = Chariot('black', 'd', 8)
        black_general = General('black', 'd', 10)

        self.game._board[1]['e'] = red_general
        self.game._board[1]['d'] = red_advisor
        self.game._board[8]['d'] = black_chariot
        self.game._board[10]['d'] = black_general

        self.game._red_pieces = {
            'G': red_general,
            'A1': red_advisor
        }

        self.game._black_pieces = {
            'G': black_general,
            'R1': black_chariot
        }

        self.game._current_turn = "black"

        self.assertFalse(
            self.game.is_in_check('red')
        )

        self.assertTrue(
            self.game.make_move('d8', 'd1')
        )

        self.assertTrue(
            self.game.is_in_check('red')
        )

        self.assertEqual(
            self.game.get_game_state(),
            "UNFINISHED"
        )

    def test_black_check_2(self):
        self.game._board = copy.deepcopy( XiangqiGame.BLANK_BOARD )
        red_general = General('red', 'e', 2)

        black_soldier = Soldier('black', 'e', 4)
        black_general = General('black', 'e', 10)

        self.game._board[2]['e'] = red_general
        self.game._board[4]['e'] = black_soldier
        self.game._board[10]['e'] = black_general

        self.game._red_pieces = {
            'G': red_general
        }

        self.game._black_pieces = {
            'G': black_general,
            'S1': black_soldier
        }

        self.game._current_turn = "black"

        self.assertFalse(
            self.game.is_in_check('red')
        )

        self.game.make_move('e4', 'e3')

        self.assertTrue(
            self.game.is_in_check('red')
        )

        self.assertEqual(
            self.game.get_game_state(),
            "UNFINISHED"
        )

    def test_black_win_stalemate_1(self):
        self.game._board = copy.deepcopy( XiangqiGame.BLANK_BOARD )
        red_general   = General('red', 'e', 1)
        red_advisor_1 = Advisor('red', 'f', 1)
        red_advisor_2 = Advisor('red', 'e', 2)

        black_soldier = Soldier('black', 'c', 2)
        black_general = General('black', 'e', 10)

        self.game._board[1]['e'] = red_general
        self.game._board[1]['f'] = red_advisor_1
        self.game._board[2]['e'] = red_advisor_2
        self.game._board[2]['c'] = black_soldier
        self.game._board[10]['e'] = black_general

        self.game._red_pieces = {
            'G': red_general,
            'A1': red_advisor_1,
            'A2': red_advisor_2
        }

        self.game._black_pieces = {
            'G': black_general,
            'S1': black_soldier
        }

        self.game._current_turn = "black"

        self.assertFalse(
            self.game.is_in_check('red')
        )

        self.game.make_move('c2', 'd2')

        self.assertFalse(
            self.game.is_in_check('red')
        )

        self.assertEqual(
            self.game.get_game_state(),
            "BLACK_WON"
        )

    def test_black_win_checkmate_1(self):
        self.game._board = copy.deepcopy( XiangqiGame.BLANK_BOARD )
        red_general   = General('red', 'd', 1)

        black_chariot = Chariot('black', 'c', 6)
        black_general = General('black', 'e', 8)

        self.game._board[1]['d'] = red_general
        self.game._board[6]['c'] = black_chariot
        self.game._board[8]['e'] = black_general

        self.game._red_pieces = {
            'G': red_general
        }

        self.game._black_pieces = {
            'G': black_general,
            'R1': black_chariot
        }

        self.game._current_turn = "black"

        self.assertFalse(
            self.game.is_in_check('red')
        )

        self.game.make_move('c6', 'd6')

        self.assertTrue(
            self.game.is_in_check('red')
        )

        self.assertEqual(
            self.game.get_game_state(),
            "BLACK_WON"
        )

    def test_red_win_checkmate_1(self):
        self.game._board = copy.deepcopy( XiangqiGame.BLANK_BOARD )
        red_general = General('red', 'e', 1)
        red_soldier = Soldier('red', 'd', 6)

        black_horse   = Horse('black', 'd', 9)
        black_general = General('black', 'd', 8)

        self.game._board[1]['e'] = red_general
        self.game._board[6]['d'] = red_soldier
        self.game._board[9]['d'] = black_horse
        self.game._board[8]['d'] = black_general

        self.game._red_pieces = {
            'G': red_general,
            'S1': red_soldier
        }

        self.game._black_pieces = {
            'G': black_general,
            'H1': black_horse
        }

        self.game._current_turn = "red"

        self.assertFalse(
            self.game.is_in_check('black')
        )

        self.game.make_move('d6', 'd7')

        self.assertTrue(
            self.game.is_in_check('black')
        )

        self.assertEqual(
            self.game.get_game_state(),
            "RED_WON"
        )

    def test_red_win_checkmate_2(self):
        self.game._board = copy.deepcopy( XiangqiGame.BLANK_BOARD )
        red_general = General('red', 'e', 1)
        red_advisor = Advisor('red', 'e', 2)
        red_cannon  = Cannon('red', 'd', 2)

        black_general = General('black', 'd', 9)

        self.game._board[1]['e'] = red_general
        self.game._board[2]['e'] = red_advisor
        self.game._board[2]['d'] = red_cannon
        self.game._board[9]['d'] = black_general

        self.game._red_pieces = {
            'G': red_general,
            'A1': red_advisor,
            'C1': red_cannon
        }

        self.game._black_pieces = {
            'G': black_general
        }

        self.game._current_turn = "red"

        self.assertFalse(
            self.game.is_in_check('black')
        )

        self.game.make_move('e2', 'd3')

        self.assertTrue(
            self.game.is_in_check('black')
        )

        self.assertEqual(
            self.game.get_game_state(),
            "RED_WON"
        )

    def test_convert_coord(self):
        self.assertEqual(
            self.game.convert_coord('a10'),
            { "row": 10, "column": 'a' }
        )
Exemple #19
0
    def test_move_must_prevent_checkmate_if_in_check(self):
        """Tests that if player is in check, only moves that prevent checck are allowed."""

        # test red must prevent checkmate
        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("f1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))
        board.get_point_with_pos("e10").set_piece(Chariot("black"))
        board.get_point_with_pos("i10").set_piece(Chariot("black"))
        board.get_point_with_pos("a1").set_piece(Chariot("red"))

        game.make_move("a1", "a2")
        game.make_move("i10", "f10")

        self.assertEqual(False, game.make_move("a2", "b2"))
        self.assertEqual(False, game.make_move("f1", "e1"))
        self.assertEqual(True, game.make_move("a2", "f2"))

        # test black must prevent checkmate
        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("f1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))
        board.get_point_with_pos("a9").set_piece(Chariot("black"))
        board.get_point_with_pos("e4").set_piece(Chariot("red"))
        board.get_point_with_pos("a2").set_piece(Chariot("red"))

        game.make_move("a2", "d2")

        self.assertEqual(False, game.make_move("d10", "d9"))
        self.assertEqual(False, game.make_move("a9", "b9"))
        self.assertEqual(True, game.make_move("a9", "d9"))
Exemple #20
0
    def test_stalemate(self):
        """Tests stalemate correctly determines if red and black are put into stalemate."""

        # test red in stalemate
        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))
        board.get_point_with_pos("f10").set_piece(Chariot("black"))

        self.assertEqual(False, game.stalemate())

        board.get_point_with_pos("a2").set_piece(Chariot("black"))

        self.assertEqual(True, game.stalemate())

        # test black in stalemate
        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))

        game.make_move("e1", "f1")

        self.assertEqual(False, game.stalemate())

        board.get_point_with_pos("e9").set_piece(Chariot("red"))
        board.get_point_with_pos("c9").set_piece(Chariot("red"))

        self.assertEqual(True, game.stalemate())
Exemple #21
0
    def test_is_in_check(self):
        """Tests is_in_check correctly returns if the current player is in check for both red and black."""

        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))

        # test where red and black are not in check
        self.assertEqual(False, game.is_in_check("red"))
        self.assertEqual(False, game.is_in_check("black"))

        board.get_point_with_pos("e3").set_piece(Chariot("red"))
        board.get_point_with_pos("d8").set_piece(Chariot("black"))

        self.assertEqual(False, game.is_in_check("red"))
        self.assertEqual(False, game.is_in_check("black"))

        # test where black is in check
        board.get_point_with_pos("d8").set_piece(Chariot("red"))

        self.assertEqual(False, game.is_in_check("red"))
        self.assertEqual(True, game.is_in_check("black"))

        # test where red is in check
        board.get_point_with_pos("d8").set_piece(None)
        board.get_point_with_pos("e3").set_piece(Chariot("black"))

        self.assertEqual(True, game.is_in_check("red"))
        self.assertEqual(False, game.is_in_check("black"))
Exemple #22
0
 def setUp(self) -> None:
     self.game = XiangqiGame()
     self.game.new_game()
Exemple #23
0
    def test_checkmate_preventable(self):
        """
        Tests that checkmate_preventable returns True if a move is possible that removes check from the player in check.
        """

        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))

        # test red can prevent checkmate
        self.assertEqual(True, game.checkmate_preventable())

        game.make_move("e1", "e2")

        # test black can prevent checkmate
        self.assertEqual(True, game.checkmate_preventable())

        game.make_move("d10", "d9")

        # test black cant prevent checkmate
        self.assertEqual(True, game.checkmate_preventable())

        board.get_point_with_pos("d1").set_piece(Chariot("red"))
        game.make_move("d1", "d2")

        self.assertEqual(False, game.checkmate_preventable())

        # test red cant prevent checkmate
        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))
        board.get_point_with_pos("e10").set_piece(Chariot("black"))
        board.get_point_with_pos("f10").set_piece(Chariot("black"))

        self.assertEqual(False, game.checkmate_preventable())
    def test_black_wins_1(self):
        game = XiangqiGame()
        result_1 = game.make_move('h3', 'f3')
        result_2 = game.make_move('b8', 'e8')
        result_3 = game.make_move('f3', 'f6')
        result_4 = game.make_move('h10', 'g8')
        result_5 = game.make_move('h1', 'g3')
        result_6 = game.make_move('i10', 'h10')
        result_7 = game.make_move('c4', 'c5')
        result_8 = game.make_move('a10', 'a9')
        result_9 = game.make_move('c5', 'c6')
        result_10 = game.make_move('a9', 'f9')
        result_11 = game.make_move('c6', 'c7')
        result_12 = game.make_move('f9', 'f6')
        result_13 = game.make_move('c7', 'd7')
        result_14 = game.make_move('f6', 'd6')
        result_15 = game.make_move('d7', 'e7')
        result_16 = game.make_move('g8', 'e7')
        result_17 = game.make_move('i1', 'h1')
        result_18 = game.make_move('d6', 'd3')
        result_19 = game.make_move('c1', 'e3')
        result_20 = game.make_move('d3', 'b3')
        result_21 = game.make_move('a1', 'a3')
        result_22 = game.make_move('b3', 'b1')
        result_23 = game.make_move('a3', 'd3')
        result_24 = game.make_move('h8', 'h2')
        result_25 = game.make_move('h1', 'h2')
        result_26 = game.make_move('h10', 'h2')
        result_27 = game.make_move('g3', 'e2')
        result_28 = game.make_move('b10', 'c8')
        result_29 = game.make_move('d3', 'c3')
        result_30 = game.make_move('e7', 'd5')
        result_31 = game.make_move('c3', 'c8')
        result_32 = game.make_move('d5', 'f4')
        result_33 = game.make_move('c8', 'e8')
        result_34 = game.is_in_check("black")
        result_35 = game.make_move('g10', 'e8')
        result_36 = game.make_move('e4', 'e5')
        result_37 = game.make_move('f4', 'd3')
        result_38 = game.get_game_state()

        final_result = (result_1, result_2, result_3, result_4, result_5,
                        result_6, result_7, result_8, result_9, result_10,
                        result_11, result_12, result_13, result_14, result_15,
                        result_16, result_17, result_18, result_19, result_20,
                        result_21, result_22, result_23, result_24, result_25,
                        result_26, result_27, result_28, result_29, result_30,
                        result_31, result_32, result_33, result_34, result_35,
                        result_36, result_37, result_38)
        self.assertEqual(
            final_result,
            (True, True, True, True, True, True, True, True, True, True, True,
             True, True, True, True, True, True, True, True, True, True, True,
             True, True, True, True, True, True, True, True, True, True, True,
             True, True, True, True, "BLACK_WON"))
Exemple #25
0
    def test_update_game_state_with_checkmate(self):
        """tests when checkmate happens game_state is correctly updated."""

        # test red won with checkmate
        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))
        board.get_point_with_pos("a1").set_piece(Chariot("red"))

        self.assertEqual("UNFINISHED", game.get_game_state())

        game.make_move("a1", "d1")
        self.assertEqual("RED_WON", game.get_game_state())

        # test black won with checkmate
        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d10").set_piece(General("black"))

        game.make_move("e1", "f1")

        board.get_point_with_pos("e10").set_piece(Chariot("black"))
        board.get_point_with_pos("g10").set_piece(Chariot("black"))

        self.assertEqual("UNFINISHED", game.get_game_state())

        game.make_move("g10", "f10")
        self.assertEqual("BLACK_WON", game.get_game_state())
    def test_red_wins_2(self):
        game = XiangqiGame()
        result_1 = game.make_move('b3', 'e3')
        result_2 = game.make_move('h8', 'h9')
        result_3 = game.make_move('h1', 'g3')
        result_4 = game.make_move('g9', 'i7')
        result_5 = game.get_game_state()
        result_6 = game.make_move('h9', 'h5')
        result_7 = game.make_move('b1', 'c3')
        result_8 = game.make_move('h10', 'g8')
        result_9 = game.make_move('a1', 'b1')
        result_10 = game.get_game_state()
        result_11 = game.make_move('g7', 'f5')
        result_12 = game.make_move('h5', 'f5')
        result_13 = game.make_move('b1', 'b8')
        result_14 = game.make_move('e7', 'e6')
        result_15 = game.make_move('e3', 'e6')
        result_16 = game.make_move('d9', 'e8')
        result_17 = game.make_move('f5', 'f6')
        result_18 = game.make_move('h3', 'h5')
        result_19 = game.get_game_state()
        result_20 = game.make_move('g7', 'g6')
        result_21 = game.make_move('h5', 'e5')
        result_22 = game.is_in_check("red")
        result_23 = game.is_in_check("black")
        result_24 = game.get_game_state()

        final_result = (result_1, result_2, result_3, result_4, result_5,
                        result_6, result_7, result_8, result_9, result_10,
                        result_11, result_12, result_13, result_14, result_15,
                        result_16, result_17, result_18, result_19, result_20,
                        result_21, result_22, result_23, result_24)
        self.assertEqual(
            final_result,
            (True, True, True, False, "UNFINISHED", True, True, True, True,
             "UNFINISHED", False, True, True, True, True, False, True, True,
             "UNFINISHED", True, True, False, True, "RED_WON"))
Exemple #27
0
    def test_advisor_moves(self):
        """Tests all types of Advisor moves that are valid."""

        game = XiangqiGame()
        board = game.get_board()
        board.clear_board()

        board.get_point_with_pos("e1").set_piece(General("red"))
        board.get_point_with_pos("d9").set_piece(General("black"))

        board.get_point_with_pos("d1").set_piece(Advisor("red"))
        board.get_point_with_pos("f1").set_piece(Advisor("red"))
        board.get_point_with_pos("d10").set_piece(Advisor("black"))
        board.get_point_with_pos("f10").set_piece(Advisor("black"))

        # cannot leave castle
        red_advisor_moves = game.get_valid_end_pos_array("d1", "A")
        black_advisor_moves = game.get_valid_end_pos_array("d10", "A")
        self.assertEqual(["e2"], red_advisor_moves)
        self.assertEqual(["e9"], black_advisor_moves)

        red_advisor_moves = game.get_valid_end_pos_array("d1", "A")
        black_advisor_moves = game.get_valid_end_pos_array("d10", "A")
        self.assertEqual(["e2"], red_advisor_moves)
        self.assertEqual(["e9"], black_advisor_moves)

        game.make_move("d1", "e2")
        game.make_move("d10", "e9")

        # test can only capture opposing color pieces
        board.get_point_with_pos("d1").set_piece(Soldier("black"))
        board.get_point_with_pos("d10").set_piece(Soldier("red"))

        red_advisor_moves = game.get_valid_end_pos_array("e2", "A")
        black_advisor_moves = game.get_valid_end_pos_array("e9", "A")
        self.assertEqual(["f3", "d3", "d1"], red_advisor_moves)
        self.assertEqual(["d10", "d8", "f8"], black_advisor_moves)

        # test can have no moves
        red_advisor_moves = game.get_valid_end_pos_array("f1", "A")
        black_advisor_moves = game.get_valid_end_pos_array("f10", "A")
        self.assertEqual([], red_advisor_moves)
        self.assertEqual([], black_advisor_moves)
    def test_black_wins_2(self):
        game = XiangqiGame()
        result_1 = game.make_move('b3', 'b10')
        result_2 = game.make_move('a10', 'b10')
        result_3 = game.make_move('h3', 'h10')
        result_4 = game.make_move('i10', 'h10')
        result_5 = game.make_move('a1', 'a3')
        result_6 = game.make_move('b8', 'b6')
        result_7 = game.make_move('a3', 'f3')
        result_8 = game.make_move('b6', 'i6')
        result_9 = game.get_game_state()
        result_10 = game.is_in_check("red")
        result_11 = game.is_in_check("black")
        result_12 = game.make_move('f3', 'i3')
        result_13 = game.make_move('i6', 'i3')
        result_14 = game.make_move('h1', 'i3')
        result_15 = game.make_move('b10', 'b1')
        result_16 = game.make_move('d1', 'e2')
        result_17 = game.make_move('b1', 'c1')
        result_18 = game.make_move('e2', 'd1')
        result_19 = game.make_move('c1', 'c4')
        result_20 = game.make_move('a9', 'b8')
        result_21 = game.make_move('g1', 'e3')
        result_22 = game.make_move('c4', 'e4')
        result_23 = game.get_game_state()
        result_24 = game.make_move('i1', 'i2')
        result_25 = game.make_move('e4', 'e3')
        result_26 = game.is_in_check("red")
        result_27 = game.is_in_check("black")
        result_28 = game.make_move('d1', 'e2')
        result_29 = game.make_move('h8', 'h1')
        result_30 = game.get_game_state()

        final_result = (result_1, result_2, result_3, result_4, result_5,
                        result_6, result_7, result_8, result_9, result_10,
                        result_11, result_12, result_13, result_14, result_15,
                        result_16, result_17, result_18, result_19, result_20,
                        result_21, result_22, result_23, result_24, result_25,
                        result_26, result_27, result_28, result_29, result_30)
        self.assertEqual(
            final_result,
            (True, True, True, True, True, True, True, True, "UNFINISHED",
             False, False, True, True, True, True, True, True, True, True,
             False, True, True, "UNFINISHED", True, True, True, False, True,
             True, "BLACK_WON"))
Exemple #29
0
 def setUp(self):
     self.game = XiangqiGame()
Exemple #30
0
from XiangqiGame import XiangqiGame
from XiangqiGame_single_module import XiangqiGame
from Board import Board
from Player import Player
from GeneralPiece import GeneralPiece
from ElephantPiece import ElephantPiece
from ChariotPiece import ChariotPiece
from SoldierPiece import SoldierPiece
from AdvisorPiece import AdvisorPiece
from HorsePiece import HorsePiece
from CannonPiece import CannonPiece

if __name__ == "__main__":
    game = XiangqiGame()
    board = game._board
    red_pieces = game.get_player("red")._pieces
    black_pieces = game.get_player("black")._pieces

    # initialize empty board
    board._board_state = [  # board state represented as a 2D array
        [None for file in board._files]
        for rank in board._ranks  # initialize all positions to None
    ]

    board._piece_state = {
    }  # dictionary of piece: pos pairs. When pieces are captured, value is set to None

    for piece in black_pieces:
        piece.set_pos(None)

    for piece in red_pieces: