def test_15(self):
     """A test to check if chariot can attack correctly and move correctly"""
     g1 = Game.XiangqiGame()
     g1.make_move("a1", "a2")  # red
     g1.make_move("a10", "a9")  # black
     g1.make_move("e1", "e2")  # red
     g1.make_move("a9", "f9")  # black
     g1.make_move("a2", "d2")
     g1.make_move("a7", "a6")
     g1.make_move("d2", "d9")
     g1.make_move("b8", "b9")
     with self.subTest():
         self.assertEqual([
             'd8', 'd7', 'd6', 'd5', 'd4', 'd3', 'd2', 'c9', 'e9', 'b9',
             'f9', 'd10'
         ],
                          g1.get_game_board().get_game_piece_by_location(
                              1, 3).get_legal_moves())
     with self.subTest():
         self.assertEqual([
             'f8', 'f7', 'f6', 'f5', 'f4', 'f3', 'f2', 'e9', 'g9', 'h9',
             'i9', 'd9', 'f1'
         ],
                          g1.get_game_board().get_game_piece_by_location(
                              1, 5).get_legal_moves())
 def test_3(self):
     """A test to confirm a piece was moved to the new location."""
     g1 = Game.XiangqiGame()
     piece = g1.get_game_board().get_game_piece_by_location(9, 4)
     g1.make_move("e1", "e2")
     self.assertEqual(g1.get_game_board().get_game_piece_by_location(8, 4),
                      piece)
 def test_cannon_jump_false(self):
     game = XiangqiGame()
     game.make_move("e4", "e5")
     self.assertEqual(game.make_move("g10", "g8"), False)
     game.make_move("e7", "e6")
     game.make_move("a4", "a5")
     self.assertEqual(game.make_move("h8", "c8"), True)
 def test_general_can_see(self):
     game = XiangqiGame()
     game.make_move("e4", "e5")
     game.make_move("e7", "e6")
     game.make_move("e5", "e6")
     game.make_move("i7", "i6")
     self.assertEqual(game.make_move("e6", "g6"), False)
Example #5
0
 def test_1(self):
     """
     Test for initial game state
     """
     a_game = game.XiangqiGame()
     a_game_state = a_game.get_game_state()
     self.assertIs('UNFINISHED', a_game_state)
Example #6
0
def main():
    """the main gameplay loop"""
    game = XiangqiGame()  # initializes game
    while True:
        clear_screen()  # start by clearing screen
        print("Type 'q' at any time to forfeit")
        print()
        game.show_board()
        print()
        print('Game state:', game.get_game_state())
        print('Turn:', game.get_turn(), end='')
        if game.is_in_check(game.get_turn().lower()):
            print(' (in check)')
        print()
        print()

        move_from = input('Move from: ')
        if move_from == 'q':
            if game.get_turn() == 'RED':
                print('BLACK_WON')
            if game.get_turn() == 'BLACK':
                print('RED_WON')
            time.sleep(1)
            break

        move_to = input('Move to: ')
        if move_to == 'q':
            break

        if not game.make_move(move_from, move_to):
            print("Invalid move. Try again.")
            time.sleep(1)
        else:
            game.make_move(move_from, move_to)
 def test_8(self):
     """A test to ensure the Soldier can't move outside of its moveset"""
     g1 = Game.XiangqiGame()
     with self.subTest():
         self.assertEqual(g1.make_move("e4", "e3"), False)  # red moves
     with self.subTest():
         self.assertEqual(g1.make_move("e4", "f4"), False)  # red moves
 def test_9(self):
     """A test to ensure the General can't move beyond 1 space"""
     g1 = Game.XiangqiGame()
     with self.subTest():
         self.assertEqual(g1.make_move("e1", "e3"), False)  # red moves
     with self.subTest():
         self.assertEqual(g1.make_move("e4", "d2"), False)  # red moves
 def test_16(self):
     """A test to check if the cannon can attack correctly and move correctly"""
     g1 = Game.XiangqiGame()
     g1.make_move("h3", "f3")  # red moves
     g1.make_move("h8", "h7")  # black moves
     g1.make_move("f3", "f7")  # red moves
     g1.make_move("b8", "f8")  # black moves
     g1.make_move("b3", "f3")  # red moves
     with self.subTest():
         self.assertEqual(['h9', 'h8', 'h6', 'h5', 'h4', 'h3', 'h2', 'f7'],
                          g1.get_game_board().get_game_piece_by_location(
                              3, 7).get_legal_moves())
     with self.subTest():
         self.assertEqual([
             'f9', 'a8', 'b8', 'c8', 'd8', 'e8', 'g8', 'h8', 'i8', 'f6',
             'f5', 'f4', 'f3'
         ],
                          g1.get_game_board().get_game_piece_by_location(
                              2, 5).get_legal_moves())
     with self.subTest():
         self.assertEqual(
             ['f6', 'f5', 'f4', 'd7', 'c7', 'h7', 'f9', 'f10', 'f2'],
             g1.get_game_board().get_game_piece_by_location(
                 3, 5).get_legal_moves())
     with self.subTest():
         self.assertEqual([
             'f6', 'f5', 'f4', 'f2', 'a3', 'b3', 'c3', 'd3', 'e3', 'g3',
             'h3', 'i3', 'f8'
         ],
                          g1.get_game_board().get_game_piece_by_location(
                              7, 5).get_legal_moves())
 def test_10(self):
     """A test to see if the elephant is blocked correctly, upper right"""
     g1 = Game.XiangqiGame()
     g1.make_move("c1", "e3")  # red
     g1.make_move("h8", "f8")  # black
     g1.make_move("e1", "e2")  # red
     g1.make_move("f8", "f4")  # black
     with self.subTest():
         self.assertNotIn(
             "g5",
             g1.get_game_board().get_game_piece_by_location(
                 7, 4).get_legal_moves())
     with self.subTest():
         self.assertNotIn(
             "g1",
             g1.get_game_board().get_game_piece_by_location(
                 7, 4).get_legal_moves())
     with self.subTest():
         self.assertIn(
             "c5",
             g1.get_game_board().get_game_piece_by_location(
                 7, 4).get_legal_moves())
     with self.subTest():
         self.assertIn(
             "c1",
             g1.get_game_board().get_game_piece_by_location(
                 7, 4).get_legal_moves())
 def test_general_leave_castle(self):
     game = XiangqiGame()
     game.make_move("e1", "e2")
     game.make_move("e7", "e6")
     game.make_move("e2", "d2")
     game.make_move("e6", "e5")
     self.assertEqual(game.make_move("d2", "c2"), False)
     self.assertEqual(game.make_move("d2", "d3"), True)
 def test_soldier(self):
     game = XiangqiGame()
     self.assertEqual(game.make_move("e4", "e5"), True)
     game.make_move("e4", "e5")
     self.assertEqual(game.make_move("e5", "f5"), False)
     game.make_move("a7", "a6")
     game.make_move("e5", "e6")
     game.make_move("a6", "a5")
     self.assertEqual(game.make_move("e6", "f6"), True)
 def test_4(self):
     """A test to ensure you can't land on your own piece"""
     g1 = Game.XiangqiGame()
     g1.make_move("e1", "d1")
     with self.subTest():
         self.assertEqual(g1.make_move("e1", "d1"), False)
     with self.subTest():
         self.assertEqual(
             g1.get_game_board().get_game_piece_color_by_location(9, 4),
             "Red")
 def test_illegal_moves(self):
     """tests to make sure illegal moves return False"""
     game = XiangqiGame.XiangqiGame()
     self.assertFalse(game.make_move('e10', 'e9'))  # wrong turn
     self.assertFalse(game.make_move('a1', 'a4'))
     self.assertFalse(game.make_move('b1', 'd2'))
     self.assertFalse(game.make_move('d1', 'd2'))
     self.assertFalse(game.make_move('e1', 'd2'))
     self.assertFalse(game.make_move('a4', 'b4'))
     self.assertFalse(game.make_move('b3', 'b8'))
Example #15
0
 def test_3(self):
     """
     Test adding pieces to the board
     """
     a_game = game.XiangqiGame()
     a_placement = a_game.set_active(game.General('red'), 'e1')
     self.assertTrue(a_placement, 'Piece did not pass placement')
     a_next_placement = a_game.set_active(game.Advisor('black'), 'e11')
     self.assertFalse(a_next_placement,
                      'Placement allowed illegal placement of piece')
    def test_readme_example(self):
        """tests the code given in the readme"""
        game = XiangqiGame.XiangqiGame()
        move_result = game.make_move('c1', 'e3')
        black_in_check = game.is_in_check('black')
        game.make_move('e7', 'e6')
        state = game.get_game_state()

        self.assertEqual(True, move_result)
        self.assertEqual(False, black_in_check)
        self.assertEqual('UNFINISHED', state)
 def test_7(self):
     """A test to ensure the Elephant can't move outside of its moveset"""
     g1 = Game.XiangqiGame()
     g1.make_move("c1", "e3")  # red moves
     g1.make_move("c10", "e8")  # black moves
     g1.make_move("e3", "c5")  # red moves
     g1.make_move("e8", "g6")  # black moves
     with self.subTest():
         self.assertEqual(g1.make_move("c5", "e7"), False)  # red moves
         g1.make_move("c5", "e3")  # red moves
     with self.subTest():
         self.assertEqual(g1.make_move("g6", "i4"), False)  # black moves
Example #18
0
    def initgame(self):
        self.board_status = XiangqiGame()
        self.selected = False
        self.last_position = ''
        # bind left click from mouse as user input
        self.bind_all('<Button-1>', self.click)
        # bind right click from mouse as move options
        self.bind_all('<Button-3>', self.rightclick)

        self.board_setup()
        self.load_images()
        self.draw_pieces()
 def test_6(self):
     """A test to ensure the Advisor can't move outside of its moveset"""
     g1 = Game.XiangqiGame()
     g1.make_move("e4", "e5")  # red moves
     g1.make_move("d10", "e9")  # black moves
     g1.make_move("f1", "e2")  # red moves
     g1.make_move("e9", "d8")  # black moves
     g1.make_move("e2", "f3")  # red moves
     with self.subTest():
         self.assertEqual(g1.make_move("d8", "c9"), False)  # black moves
         g1.make_move("d8", "e9")  # black moves
     with self.subTest():
         self.assertEqual(g1.make_move("f3", "e4"), False)  # red moves
 def test_5(self):
     """A test to ensure the General can't move outside of its moveset"""
     g1 = Game.XiangqiGame()
     g1.make_move("e4", "e5")  # red moves
     g1.make_move("e10", "e9")  # black moves
     g1.make_move("e1", "e2")  # red moves
     g1.make_move("e9", "f9")  # black moves
     g1.make_move("e2", "e3")  # red moves
     with self.subTest():
         self.assertEqual(g1.make_move("f9", "g9"), False)  # black moves
         g1.make_move("f9", "e9")  # black moves
     with self.subTest():
         self.assertEqual(g1.make_move("e3", "e4"), False)  # red moves
 def test_14(self):
     """A test to see if the horse is blocked correctly"""
     g1 = Game.XiangqiGame()
     g1.make_move("b1", "c3")  # red
     g1.make_move("b8", "c8")  # black
     g1.make_move("h3", "d3")  # red
     g1.make_move("c8", "b8")  # black
     g1.make_move("a1", "a2")  # red
     g1.make_move("b8", "c8")  # black
     g1.make_move("a2", "c2")  # red
     self.assertEqual([],
                      g1.get_game_board().get_game_piece_by_location(
                          7, 2).get_legal_moves())
 def test_19(self):
     """A test to check the General's check status"""
     g1 = Game.XiangqiGame()
     g1.make_move("b3", "e3")  # red moves
     g1.make_move("a10", "a9")  # black moves
     g1.make_move("e3", "e7")  # red moves
     g1.make_move("a9", "e9")  # black moves
     g1.make_move("e7", "d7")  # red moves
     g1.make_move("e9", "e4")  # black moves
     g1.make_move("a1", "a2")  # red moves
     with self.subTest():
         self.assertEqual(True, g1.is_in_check("Red"))
     with self.subTest():
         self.assertEqual(False, g1.is_in_check("Black"))
 def test_18(self):
     """A test to check the flying general rules"""
     g1 = Game.XiangqiGame()
     g1.make_move("e1", "e2")  # red moves
     g1.make_move("e10", "e9")  # black moves
     g1.make_move("e2", "d2")  # red moves
     with self.subTest():
         self.assertEqual(['d8', 'd9'],
                          g1.get_game_board().get_game_piece_by_location(
                              8, 3).get_flying_moves())
     with self.subTest():
         self.assertEqual(['f9', 'e8', 'e10'],
                          g1.get_game_board().get_game_piece_by_location(
                              1, 4).get_legal_moves())
 def test_make_move_edge_cases(self):
     game = XiangqiGame()
     self.assertEqual(game.make_move("z1", "e3"),
                      False)  # tests from col out of range
     self.assertEqual(game.make_move("c1", "Z3"),
                      False)  # tests to col out of range
     self.assertEqual(game.make_move("c11", "e3"),
                      False)  # tests from row out of range
     self.assertEqual(game.make_move("c1", "e11"),
                      False)  # tests to row out of range
     self.assertEqual(vars(game.get_game_piece_at_square("c1")),
                      vars(Elephant("red")))  # tests function
     self.assertEqual(game.make_move("c1", "d1"),
                      False)  # tests friendly fire
    def test_early_checkmate(self):
        """tests game up to checkmate"""
        game = XiangqiGame.XiangqiGame()
        game.make_move('b3', 'e3')
        game.make_move('h8', 'e8')
        game.make_move('h3', 'h6')
        game.make_move('b8', 'b4')
        game.make_move('e3', 'e7')  # black is in check
        move6 = game.make_move('e8', 'e4')
        self.assertFalse(game.is_in_check('black'))  # black gets out of check
        self.assertEqual('UNFINISHED', game.get_game_state())

        move7 = game.make_move('h6', 'e6')  # black is checkmated here

        self.assertEqual('RED_WON', game.get_game_state())
        self.assertTrue(game.is_in_check('black'))  # black was put into check
        self.assertFalse(game.is_in_check('red'))  # red is not in check

        self.assertTrue(move6 and move7)
 def test_17(self):
     """A test to check soldier movement before and after the final row"""
     g1 = Game.XiangqiGame()
     g1.make_move("c4", "c5")  # red moves
     g1.make_move("g7", "g6")  # black moves
     g1.make_move("c5", "c6")  # red moves
     g1.make_move("g6", "g5")  # black moves
     g1.make_move("c6", "c7")  # red moves
     g1.make_move("g5", "g4")  # black moves
     g1.make_move("c7", "c8")  # red moves
     g1.make_move("g4", "g3")  # black moves
     g1.make_move("c8", "c9")  # red moves
     g1.make_move("g3", "g2")  # black moves
     g1.make_move("c9", "c10")  # red moves
     with self.subTest():
         self.assertEqual(['d10', 'b10'],
                          g1.get_game_board().get_game_piece_by_location(
                              0, 2).get_legal_moves())
     with self.subTest():
         self.assertEqual(['h2', 'f2', 'g1'],
                          g1.get_game_board().get_game_piece_by_location(
                              8, 6).get_legal_moves())
 def test_check(self):
     game = XiangqiGame()
     game.make_move("e4", "e5")
     game.make_move("e7", "e6")
     game.make_move("i4", "i5")
     game.make_move("a10", "a9")
     game.make_move("g4", "g5")
     game.make_move("h8", "c8")
     game.make_move("e5", "e6")
     game.make_move("a9", "e9")
     self.assertEqual(game.make_move("e6", "g6"),
                      False)  # will put general in check
     game.make_move("e6", "e7")
     game.make_move("e9", "e7")
     self.assertEqual(game.is_in_check('red'),
                      True)  # puts red general in check
     self.assertEqual(game.make_move("a4", "a5"),
                      False)  # will not save general
     self.assertEqual(game.make_move("f1", "e2"),
                      True)  # move saves the general
     self.assertEqual(game.is_in_check('red'),
                      False)  # previous move saved the general out of check
 def test_20(self):
     """A test to check if red won is correct"""
     g1 = Game.XiangqiGame()
     g1.make_move("a1", "a2")  # red
     g1.make_move("a10", "a9")  # black
     g1.make_move("e1", "e2")  # red
     g1.make_move("a9", "a10")  # black
     g1.make_move("a2", "d2")  # red
     g1.make_move("a7", "a6")
     g1.make_move("d2", "d9")  # red
     g1.make_move("b8", "b9")
     g1.make_move("e2", "d2")  # red
     g1.make_move("b9", "b8")
     g1.make_move("d9", "d10")  # red
     g1.make_move("b8", "b9")
     g1.make_move("e4", "e5")  # red
     g1.make_move("b9", "b8")
     g1.make_move("e5", "e6")  # red
     g1.make_move("b8", "b9")
     g1.make_move("e6", "e7")  # red
     g1.make_move("b9", "b8")
     g1.make_move("b3", "e3")
     self.assertEqual("RED_WON", g1.get_game_state())
 def test_real_game_1(self):
     """
     The following code is a game copied from an online match found at:
     http://wxf.ca/wxf/index.php/xiangqi-news/news-from-europ/381-world-xiangqi-championships-2015-game-records
     :return:
     """
     game = XiangqiGame()
     game.make_move('g4', 'g5')
     game.make_move('c7', 'c6')
     game.make_move('b3', 'c3')
     game.make_move('c10', 'e8')
     game.make_move('b1', 'a3')
     game.make_move('b8', 'b4')
     game.make_move('h1', 'g3')
     game.make_move('b10', 'c8')
     game.make_move('g1', 'e3')
     game.make_move('c8', 'd6')
     game.make_move('a1', 'b1')
     game.make_move('a10', 'b10')
     game.make_move('f1', 'e2')
     game.make_move('b4', 'b2')
     game.make_move('i1', 'f1')
     game.make_move('f10', 'e9')
     game.make_move('f1', 'f6')
     game.make_move('d6', 'c8')
     game.make_move('a4', 'a5')
     game.make_move('b10', 'b3')
     game.make_move('c3', 'd3')
     game.make_move('c8', 'b6')
     game.make_move('g3', 'f5')
     game.make_move('b6', 'c4')
     game.make_move('d3', 'd9')
     game.make_move('c4', 'a3')
     game.make_move('h3', 'b3')
     game.make_move('a3', 'b1')
     game.make_move('b3', 'b10')
     self.assertEqual(game.get_game_state(), 'RED_WON')
    def test_early_check(self):
        """tests game up to check"""
        game = XiangqiGame.XiangqiGame()
        move1 = game.make_move('b3', 'e3')
        self.assertFalse(game.is_in_check('black'))
        self.assertFalse(game.is_in_check('red'))

        move2 = game.make_move('h8', 'e8')
        self.assertFalse(game.is_in_check('black'))
        self.assertFalse(game.is_in_check('red'))

        move3 = game.make_move('h3', 'h6')
        self.assertFalse(game.is_in_check('black'))
        self.assertFalse(game.is_in_check('red'))

        move4 = game.make_move('b8', 'b4')
        self.assertFalse(game.is_in_check('black'))
        self.assertFalse(game.is_in_check('red'))

        move5 = game.make_move('e3', 'e7')  # black is in check
        self.assertTrue(game.is_in_check('black'))
        self.assertFalse(game.is_in_check('red'))  # red is not in check

        self.assertTrue(move1 and move2 and move3 and move4 and move5)