Esempio n. 1
0
    def test_get_color_of_player(self):

        board = ChessBoard()

        self.assertEqual(board.get_color_of_player(0), "w")
        self.assertEqual(board.get_color_of_player(1), "b")
        self.assertNotEqual(board.get_color_of_player(0), "b")
Esempio n. 2
0
    def test_clean_cell_after_get_or_move_figure(self):

        board = ChessBoard()

        cell_to_clean = Point(1, 1)
        board.clean_cell(cell_to_clean)
        self.assertEqual(board.get_figure_from_board(cell_to_clean), "  ")
Esempio n. 3
0
def test_promotion():
    """
    When pawns reach the back rank they should become queens (currently we don't support under-promotion).
    When other pieces move to the back rank, they should remain as themselves.
    """
    position = empty_position()
    position[(6, 0)] = ChessPiece['WP']  # a7
    position[(1, 4)] = ChessPiece['BP']  # e2
    position[(0, 0)] = ChessPiece['WK']  # a1
    position[(0, 7)] = ChessPiece['BK']  # h1
    position[(5, 5)] = ChessPiece['WB']  # f6
    position[(1, 6)] = ChessPiece['BR']  # g2
    can_castle = {'WKS': False, 'WQS': False, 'BKS': False, 'BQS': False}
    board = ChessBoard(position=position,
                       can_castle=can_castle,
                       last_move=None,
                       player_to_move=Player['W'])

    nb = board.make_move(((6, 0), (7, 0)))  # a8=Q
    assert nb.position[(7, 0)] == ChessPiece['WQ'], "white promotion"

    nb = board.make_move(((5, 5), (7, 3)))  # Bd8
    assert nb.position[(
        7, 3)] == ChessPiece['WB'], "white piece moves to back rank"

    nb2 = nb.make_move(((1, 4), (0, 4)))  # e1=Q
    assert nb2.position[(0, 4)] == ChessPiece['BQ'], "black promotion"

    nb2 = nb.make_move(((1, 6), (0, 6)))  # Rg1
    assert nb2.position[(
        0, 6)] == ChessPiece['BR'], "black piece moves to back rank"
Esempio n. 4
0
    def test__finally_move(self):
        board = ChessBoard()
        selected_figure = Point(3, 6)
        new_position = Point(3, 4)

        board._finally_move(new_position, selected_figure)
        self.assertEqual(board.get_figure_from_board(new_position), "wp")
Esempio n. 5
0
    def test_get_color_of_figure_on_the_certain_cell(self):

        board = ChessBoard()

        cell = Point(3, 6)  # by default is "wp"

        self.assertEqual(board.get_color_of_figure(cell), "w")
Esempio n. 6
0
    def test_if_try_to_jump_over_enemy_figure(self):

        board = ChessBoard()
        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', 'bp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', 'wr'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', '  ']]

        set_board(board, testBoard)

        selected_figure = Point(7, 4, "wr")
        final_position = Point(7, 0)

        next_cell_while_moving = Point(7, 3)

        motion = board._make_motion(selected_figure, final_position)

        step_x, step_y = board._set_step_to_move(motion)
        prepare_moving = Point(step_x, step_y)

        self.assertTrue(board._try_jump_enemy(
            next_cell_while_moving, selected_figure, prepare_moving, motion))
Esempio n. 7
0
    def test_get_the_board(self):
        board = ChessBoard()
        theBoard = board.get_board()

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        self.assertEqual(testBoard, theBoard)

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', '  ', 'bp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', 'bp', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        self.assertNotEqual(testBoard, theBoard)
Esempio n. 8
0
    def test_is_end_of_the_game(self):

        board = ChessBoard()

        self.assertFalse(board.is_end())
        # get one of the Kings
        board.set_end_of_the_game("wk")
        self.assertTrue(board.is_end())
Esempio n. 9
0
    def test_set_end_of_the_game(self):
        board = ChessBoard()

        # at the beginning of the game the state is zero
        self.assertNotEqual(1, board._finish_game)

        # get one of the Kings
        board.set_end_of_the_game("wk")
        self.assertEqual(1, board._finish_game)
Esempio n. 10
0
 def __init__(self, gameEngine):
     """
     Constructor.
     :param gameEngine: The initial game engine.
     """
     self.chessBoard = ChessBoard()
     self.lightBoard = gameEngine.lightBoard
     self.display = gameEngine.display
     self.loopingCall = gameEngine.loopingCall
     self.validMovesCounter = 0
Esempio n. 11
0
    def test_invalid_move_white_pawn(self):
        board = ChessBoard()

        # Test 1
        white_pawn = Point(1, 3)
        new_position = Point(1, 1)
        motion = board._make_motion(white_pawn, new_position)

        self.assertTrue(board.invalid_move_white_pawn(motion, new_position))

        # Test 2
        new_position = Point(1, 0)
        motion = board._make_motion(white_pawn, new_position)
        self.assertTrue(board.invalid_move_white_pawn(motion, new_position))

        # Test 3
        new_position = Point(1, 4)
        motion = board._make_motion(white_pawn, new_position)

        self.assertTrue(board.invalid_move_white_pawn(motion, new_position))

        # Test 4
        new_position = Point(1, 2)
        motion = board._make_motion(white_pawn, new_position)

        self.assertFalse(board.invalid_move_white_pawn(motion, new_position))
Esempio n. 12
0
 def __init__(self, whites, blacks, gameIndex):
     self.game_id = gameIndex
     self.validMovesCounter = 0
     self.white = whites
     self.white.game_id = self.game_id
     self.white.game = self
     self.black = blacks
     self.black.game_id = self.game_id
     self.black.game = self
     self.chessBoard = ChessBoard()
     self.lightBoard = LightBoard()
     self.notifyReady()
Esempio n. 13
0
def pygame_mainloop():

    chess_board = ChessBoard()
    player = 0
    chess_board.set_allowed_color(player)
    screen = pygame.display.set_mode((640, 480))
    board_image, chess_image = load_images()
    selected = surface()

    is_selected_cell = False
    selected_cell = Point(None, None)
    offset = 120

    print ("player's", player, "turn")

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return

            elif event.type == pygame.MOUSEBUTTONUP:
                new_position = make_a_point_from_mouse(event, offset)
                if is_into_board(new_position):
                    new_position.normalize_point()
                    if is_selected_cell:
                        is_selected_cell = False
                        can_play = chess_board.play(selected_cell, new_position)
                        if can_play:
                            player = change_player(player)
                            chess_board.set_allowed_color(player)
                            print ("player's", player, "turn")
                        else:
                            print("Invalid move!!! Try again")
                        selected_cell.set_point(None, None)
                    else:
                        selected_cell.set_point(
                            new_position.get_x(), new_position.get_y())
                        if chess_board.allowed_selection(selected_cell, player):
                            is_selected_cell = True

                        else:
                            selected_cell.set_point(None, None)

        screen.fill((109, 165, 165))
        screen.blit(board_image, (offset, offset))
        if selected_cell.get_x() is not None and \
                selected_cell.get_y() is not None:
            screen.blit(selected, (selected_cell.get_x()*32 + offset + 2,
                        selected_cell.get_y()*24 + offset + 2))
        prepare_board_to_update(chess_board, screen, chess_image, offset)
        pygame.display.update()
        time.sleep(0.04)
        if chess_board.is_end():
            print("The winner is Player {0}".format((player + 1) % 2))
            time.sleep(4)
            return
Esempio n. 14
0
    def test_allowed_selection_cell(self):

        # Test 1
        board = ChessBoard()
        selected_cell = Point(0, 0)
        player = 0
        self.assertFalse(board.allowed_selection(selected_cell, player))

        # Test 2
        selected_cell = Point(7, 7)
        player = 0
        self.assertTrue(board.allowed_selection(selected_cell, player))

        # Test 3
        selected_cell = Point(4, 4)
        player = 1
        self.assertFalse(board.allowed_selection(selected_cell, player))
Esempio n. 15
0
    def test_invalid_move_black_pawn(self):
        board = ChessBoard()
        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', 'wp', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', '  ', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)

        # Test 1
        selected_figure = Point(1, 1)
        new_position = Point(3, 1)
        motion = board._make_motion(selected_figure, new_position)
        self.assertTrue(board.invalid_move_black_pawn(motion, new_position))

        # Test 2
        new_position = Point(3, 2)
        motion = board._make_motion(selected_figure, new_position)
        self.assertTrue(board.invalid_move_black_pawn(motion, new_position))

        # Test 3
        selected_figure = Point(2, 2)
        new_position = Point(2, 3)
        motion = board._make_motion(selected_figure, new_position)
        self.assertFalse(board.invalid_move_black_pawn(motion, new_position))
Esempio n. 16
0
    def test_get_figure_from_board(self):

        board = ChessBoard()

        blackPawn = board.get_figure_from_board(Point(1, 1))
        whiteRock = board.get_figure_from_board(Point(7, 7))
        blackBishop = board.get_figure_from_board(Point(5, 0))
        whiteQueen = board.get_figure_from_board(Point(3, 7))
        whiteSpace = board.get_figure_from_board(Point(4, 4))

        self.assertEqual(blackPawn, "bp")
        self.assertEqual(whiteRock, "wr")
        self.assertEqual(blackBishop, "bb")
        self.assertEqual(whiteQueen, "wq")
        self.assertEqual(whiteSpace, "  ")

        self.assertNotEqual(blackPawn, "wp")
        self.assertNotEqual(blackBishop, "wb")
        self.assertNotEqual(whiteQueen, "  ")
Esempio n. 17
0
    def test_if_try_to_jump_over_your_figure(self):

        board = ChessBoard()
        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', 'wp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', 'wr'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', '  ']]

        set_board(board, testBoard)

        selected_figure = Point(7, 4, "wr")

        next_cell_while_moving = Point(7, 3)

        self.assertTrue(board._try_jump_yours(
            next_cell_while_moving, selected_figure))
Esempio n. 18
0
    def test_make_a_promotion_of_queen(self):
        board = ChessBoard()
        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'wp', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', '  ', 'bp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', '  ', 'wp'],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)

        # Test 1
        selected_figure = Point(6, 0)
        self.assertEqual(board.get_figure_from_board(selected_figure), "wp")

        # Test 2
        board._promotion_queen(selected_figure, "wq")
        self.assertEqual(board.get_figure_from_board(selected_figure), "wq")
Esempio n. 19
0
    def test_calculate_of_motion_of_figure(self):

        board = ChessBoard()

        # Test 1
        start_position = Point(1, 1)
        destination = Point(1, 2)

        motion = board._make_motion(start_position, destination)

        self.assertEqual(motion.get_x(), 0)
        self.assertEqual(motion.get_y(), 1)

        # Test 2
        start_position = Point(1, 1)
        destination = Point(1, 5)

        motion = board._make_motion(start_position, destination)

        self.assertNotEqual(motion.get_x(), 1)
        self.assertEqual(motion.get_y(), 4)
Esempio n. 20
0
    def test_set_allowed_color_of_player(self):

        board = ChessBoard()
        player = 0
        board.set_allowed_color(player)

        # Test 1 & 2
        self.assertEqual(board.get_allawod_color(), "w")
        self.assertNotEqual(board.get_allawod_color(), "b")

        # Test 3
        player = 1
        board.set_allowed_color(player)
        self.assertEqual(board.get_allawod_color(), "b")
Esempio n. 21
0
    def test_waiting_to_make_a_full_turn(self):

        board = ChessBoard()
        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)

        selected_figure = Point(7, 7, "wr")
        final_position = Point(7, 3)
        motion = board._make_motion(selected_figure, final_position)

        step_x, step_y = board._set_step_to_move(motion)
        prepare_moving = Point(step_x, step_y)

        self.assertTrue(
            board.waiting_to_finish_this_turn(prepare_moving, motion))
Esempio n. 22
0
    def test_valide_selected_cell(self):
        board = ChessBoard()
        player = 0
        board.set_allowed_color(player)
        selected_figure = Point(5, 5)

        # Test 1
        selected_figure.set_info("  ")
        self.assertTrue(board._invalid_selection(selected_figure))

        # Test 2
        selected_figure.set_info("wp")
        self.assertFalse(board._invalid_selection(selected_figure))

        # Test 3
        player = 1
        board.set_allowed_color(player)

        selected_figure.set_info("wp")
        self.assertTrue(board._invalid_selection(selected_figure))
Esempio n. 23
0
File: main.py Progetto: Nems/Chess
def main():
	cb = ChessBoard()
	color = ('w', 'b')
	player = 0
	cb.set_allowed_color(color[player])
	while(1):
		print '-------------------------------------\n', str(cb), '\n-------------------------------------\n'
		inp = raw_input("Player%s's move: " % (player,))
		try:
			sx, sy, dx, dy = inp.split(' ')
		except:
			if inp == 'exit':
				break
		if not cb.move(int(sx), int(sy), int(dx), int(dy)):
			print 'Invalid move'
			continue
		player = (player + 1) % 2
		cb.set_allowed_color(color[player])
Esempio n. 24
0
    def test_can__get_with_white_pawn(self):

        board = ChessBoard()
        # test 1
        white_pawn_position = Point(2, 2)
        black_pawn_position = Point(3, 1)
        motion = board._make_motion(white_pawn_position, black_pawn_position)

        self.assertTrue(board._get_with_white_pawn(motion, black_pawn_position))

        # test 2
        white_pawn_position = Point(3, 3)
        black_pawn_position = Point(4, 2)
        motion = board._make_motion(white_pawn_position, black_pawn_position)

        self.assertFalse(board._get_with_white_pawn(motion, black_pawn_position))

        # test 3
        white_pawn_position = Point(2, 2)
        black_pawn_position = Point(2, 1)
        motion = board._make_motion(white_pawn_position, black_pawn_position)

        self.assertFalse(board._get_with_white_pawn(motion, black_pawn_position))
Esempio n. 25
0
    def test_can__get_with_black_pawn(self):

        board = ChessBoard()
        # test 1
        black_pawn_position = Point(5, 5)
        white_pawn_position = Point(4, 6)
        motion = board._make_motion(black_pawn_position, white_pawn_position)

        self.assertTrue(board._get_with_black_pawn(motion, white_pawn_position))

        # test 2
        black_pawn_position = Point(5, 5)
        white_pawn_position = Point(5, 6)
        motion = board._make_motion(black_pawn_position, white_pawn_position)

        self.assertFalse(board._get_with_black_pawn(motion, white_pawn_position))

        # test 3
        black_pawn_position = Point(3, 3)
        white_pawn_position = Point(4, 4)
        motion = board._make_motion(black_pawn_position, white_pawn_position)

        self.assertFalse(board._get_with_black_pawn(motion, white_pawn_position))
Esempio n. 26
0
    def test_play(self):
        board = ChessBoard()
        player = 0
        board.set_allowed_color(player)

        # Test 1
        white_figure = Point(2, 6, "wp")
        new_position = Point(2, 4)

        self.assertTrue(board.play(white_figure, new_position))

        # Test 2
        selected_figure = Point(-1, -1, "  ")
        new_position = Point(2, 4)

        self.assertFalse(board.play(selected_figure, new_position))

        # Test 3

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', '  ', 'bp', 'bp', 'bp', 'bp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', 'bp', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)
        board.set_allowed_color(1)
        black_pawn = Point(3, 5, "bp")
        new_position = Point(4, 6)

        self.assertTrue(board.play(black_pawn, new_position))

        # Test 4

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', '  ', 'bp', 'bp', 'bp', 'bp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', 'bp', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)
        black_pawn = Point(3, 5, "bp")
        new_position = Point(4, 5)

        self.assertFalse(board.play(black_pawn, new_position))

        # Test 5

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
                     ['  ', '  ', '  ', 'wp', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', '  ', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)
        board.set_allowed_color(0)
        white_figure = Point(3, 2, "wp")
        new_position = Point(4, 1)

        self.assertTrue(board.play(white_figure, new_position))

        # Test 6

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
                     ['  ', '  ', '  ', 'wp', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', '  ', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)

        white_figure = Point(3, 2, "wp")
        new_position = Point(4, 2)

        self.assertFalse(board.play(white_figure, new_position))

        # Test 7

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', '  '],
                     ['  ', '  ', '  ', 'wp', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', 'bp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', '  ', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)

        white_rock = Point(7, 7, "wr")
        new_position = Point(7, 1)

        self.assertFalse(board.play(white_rock, new_position))

        # Test 8

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', '  '],
                     ['  ', '  ', '  ', 'wp', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', 'wp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', '  ', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)

        white_rock = Point(7, 7, "wr")
        new_position = Point(7, 1)

        self.assertFalse(board.play(white_rock, new_position))

        # Test 9

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', '  '],
                     ['  ', '  ', '  ', 'wp', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', 'wp'],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', '  ', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)

        white_knight = Point(6, 7, "wn")
        new_position = Point(4, 6)

        self.assertFalse(board.play(white_knight, new_position))

        # Test 10

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', '  '],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'wp'],
                     ['  ', '  ', '  ', 'wp', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', '  ', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)

        white_pawn = Point(7, 1, "wp")
        new_position = Point(7, 0)

        self.assertTrue(board.play(white_pawn, new_position))

        # Test 11

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', '  '],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'wp'],
                     ['  ', '  ', '  ', 'wp', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', '  ', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)

        white_pawn = Point(7, 1, "wp")
        new_position = Point(6, 0)

        self.assertTrue(board.play(white_pawn, new_position))

        # Test 12

        testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', '  '],
                     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'wp'],
                     ['  ', '  ', '  ', 'wp', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '],
                     ['wp', 'wp', 'wp', '  ', 'wp', 'wp', 'wp', '  '],
                     ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']]

        set_board(board, testBoard)

        white_pawn = Point(3, 3, "  ")
        new_position = Point(6, 0)

        self.assertFalse(board.play(white_pawn, new_position))
Esempio n. 27
0
    def test_get_color_of_figure_of_certain_cell(self):
        board = ChessBoard()

        self.assertEqual(board.get_current_figure_color(7, 7), "w")
        self.assertEqual(board.get_current_figure_color(1, 1), "b")
        self.assertNotEqual(board.get_current_figure_color(3, 3), "b")
Esempio n. 28
0
class TwoPlayersOnOneBoard(GameEngine):

    def __init__(self, gameEngine):
        """
        Constructor.
        :param gameEngine: The initial game engine.
        """
        self.chessBoard = ChessBoard()
        self.lightBoard = gameEngine.lightBoard
        self.display = gameEngine.display
        self.loopingCall = gameEngine.loopingCall
        self.validMovesCounter = 0

    @inlineCallbacks
    def updateLightBoard(self):
        nb = self.validMovesCounter
        for col in [0, 1]:
            for i, piece in enumerate(self.chessBoard.pieces[col]):
                if nb == self.validMovesCounter:
                    self.updateDeferred = Deferred()
                    self.updateDeferred.addCallback(self.chessBoard.all_legal_natures)
                    self.updateDeferred.addErrback(log.err)  # DEBUG
                    reactor.callLater(0, self.updateDeferred.callback, piece)
                    natures = yield self.updateDeferred
                    if nb == self.validMovesCounter:
                        color = piece.color
                        position = piece.position
                        pieceIndex = i + col * 24
                        self.lightBoard.setPiece(pieceIndex, color, position, natures)
                        if (piece.position is not None) and (piece.position is not False):
                            self.makeDisplayDrawBoard()
                        elif (piece.position is False):
                            self.display.updatePane()

    def moveTask(self, mov):
        x1, y1, x2, y2 = mov[0], mov[1], mov[2], mov[3]
        try:
            self.chessBoard.move(x1, y1, x2, y2, disp=False)
            self.lightBoard.move(x1, y1, x2, y2)
            color = "Whites" if (self.validMovesCounter % 2 == 0) else "Blacks"
            self.display.addMessage(color + " move from ({},{}) to ({},{})".format(x1, y1, x2, y2))
            self.validMovesCounter += 1
            self.display.setLastMove(x1, y1, x2, y2)
            self.makeDisplayDrawBoard()
            self.updateLightBoard()
        except IllegalMove as e:
            self.handleIllegalMove(str(e))

    def move(self, x1, y1, x2, y2):
        d = Deferred()
        d.addCallback(self.moveTask).addErrback(log.err)  # DEBUG
        reactor.callLater(0, d.callback, (x1, y1, x2, y2))

    def autoMove(self):
        try:
            return self.chessBoard.auto_move()
        except IllegalMove as e: # In case the game has ended
            self.handleIllegalMove(str(e))

    def checkEndTask(self):
        outcome = self.chessBoard.end_game()
        self.display.addMessage(outcome)
Esempio n. 29
0
File: main.py Progetto: Nems/Chess
def pygame_mainloop():
	
	#pygame board test
	
	cb = ChessBoard()
	color = ('w', 'b')
	player = 0
	cb.set_allowed_color(color[player])
	
	screen = pygame.display.set_mode((640, 480))
	board, chess = load_images()
	
	selected=pygame.Surface((32,24),pygame.SWSURFACE|pygame.SRCALPHA,32)
	#selected.set_alpha(60)
	selected.fill((255,255,0))
	
	selection = False
	sx, sy = None, None
	
	offset = 22
	
	print 'player\'s', player, 'turn'
	
	while 1:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				return
			elif event.type == pygame.KEYDOWN:
				if event.key == pygame.K_ESCAPE:
					return
			elif event.type == pygame.MOUSEBUTTONUP:
				x, y = event.pos
				x -= offset
				y -= offset
				if 0 <= x <= 255 and 0 <= y <= 191:
					x /= 32
					y /= 24
					if selection:
						selection = False
						if cb.move(int(sx), int(sy), int(x), int(y)):
							player = (player + 1) % 2
							cb.set_allowed_color(color[player])
							print 'player\'s', player, 'turn'
						sx, sy = None, None
					else:
						sx, sy = x, y
						if cb.getPiece(x, y)[0] == color[player]:
							selection = True
						else:
							sx, sy = None, None
							
				
		screen.fill((109,165, 165))
		screen.blit(board, (offset, offset))
		if sx != None and sy != None:
			screen.blit(selected, (sx*32+offset+2, sy*24+offset+2))
		for y, col in enumerate(cb.getBoard()):
			for x, piece in enumerate(col):
				if piece[0] != ' ':
					screen.blit(chess[piece], (x*32+offset, y*24+offset-12))
		pygame.display.update()
		time.sleep(0.04)
Esempio n. 30
0
class Game:
    """
    Class to represent a game instance.
    """
    def __init__(self, whites, blacks, gameIndex):
        self.game_id = gameIndex
        self.validMovesCounter = 0
        self.white = whites
        self.white.game_id = self.game_id
        self.white.game = self
        self.black = blacks
        self.black.game_id = self.game_id
        self.black.game = self
        self.chessBoard = ChessBoard()
        self.lightBoard = LightBoard()
        self.notifyReady()

    def notifyReady(self):
        self.sendMessageToAll({"type": "status", "status": "ready"})
        self.white.state = "PLAYING"
        self.black.state = "PLAYING"

    def move(self, x1, y1, x2, y2, color):
        piece = self.chessBoard.grid[x1][y1]
        if piece is not None and piece.color is not color:
            raise IllegalMove(
                "Trying to move a place that does not belong to the player.")
        self.chessBoard.move(x1, y1, x2, y2, disp=False)
        self.lightBoard.move(x1, y1, x2, y2)
        self.validMovesCounter += 1

    def autoMove(self):
        return self.chessBoard.auto_move()

    def checkEnd(self, color):
        outcome = self.chessBoard.end_game()
        msg = {"type": "chat", "content": outcome}
        self.sendMessageTo(msg, color)

    def updateLightBoardTask(self):
        for col in [0, 1]:
            for i, piece in enumerate(self.chessBoard.pieces[col]):
                if piece is not None:
                    color = piece.color
                    position = piece.position
                    natures = self.chessBoard.all_legal_natures(piece)
                    pieceIndex = i + col * 24
                    self.lightBoard.setPiece(pieceIndex, color, position,
                                             natures)
        msg = {"type": "lightboard", "description": self.lightBoard.wrapUp()}
        self.sendMessageToAll(msg)

    def updateLightBoard(self):
        """ Schedules an update board task. """
        reactor.callLater(0.5, self.updateLightBoardTask)

    def sendMessageToAll(self, msg):
        self.white.sendMessage(msg)
        self.black.sendMessage(msg)

    def sendMessageTo(self, msg, color):
        if color == 0:
            self.white.sendMessage(msg)
        elif color == 1:
            self.black.sendMessage(msg)
        else:
            pass

    def disconnectPlayers(self):
        self.white.disconnect()
        self.black.disconnect()
Esempio n. 31
0
    def test_whether_knight_try_to_moving_on_own_figure(self):
        board = ChessBoard()

        knight = Point(6, 7, "wn")
        new_position = Point(4, 6)
        self.assertTrue(board.knight_invalid_move(new_position, knight))
Esempio n. 32
0
    def test_whether_the_move_would_be_make_by_rules(self):

        board = ChessBoard()
        white_pawn = Point(6, 6, "wp")
        target_cell = Point(6, 5)
        motion = board._make_motion(white_pawn, target_cell)
        # Test 1
        self.assertTrue(
            board._checking_the_move_is_correct(white_pawn, motion))

        # Test 2
        white_pawn = Point(6, 7, "wn")
        target_cell = Point(5, 5)
        motion = board._make_motion(white_pawn, target_cell)

        self.assertTrue(
            board._checking_the_move_is_correct(white_pawn, motion))

        # Test 3
        white_knight = Point(6, 7, "wn")
        target_cell = Point(6, 5)
        motion = board._make_motion(white_knight, target_cell)

        self.assertFalse(
            board._checking_the_move_is_correct(white_knight, motion))

        # Test 4
        black_pawn = Point(1, 1, "bp")
        target_cell = Point(1, 5)
        motion = board._make_motion(black_pawn, target_cell)

        self.assertFalse(
            board._checking_the_move_is_correct(black_pawn, motion))

        # Test 5
        white_bishop = Point(3, 5, "wb")
        target_cell = Point(4, 4)
        motion = board._make_motion(white_bishop, target_cell)

        self.assertTrue(
            board._checking_the_move_is_correct(white_bishop, motion))

        # Test 6
        white_bishop = Point(3, 5, "wb")
        target_cell = Point(4, 5)
        motion = board._make_motion(white_bishop, target_cell)

        self.assertFalse(
            board._checking_the_move_is_correct(white_bishop, motion))

        # Test 7
        white_rock = Point(3, 5, "wr")
        target_cell = Point(4, 4)
        motion = board._make_motion(white_rock, target_cell)

        self.assertFalse(
            board._checking_the_move_is_correct(white_rock, motion))

        # Test 8
        white_queen = Point(3, 5, "wq")
        target_cell = Point(4, 4)
        motion = board._make_motion(white_queen, target_cell)

        self.assertTrue(
            board._checking_the_move_is_correct(white_queen, motion))

        # Test 9
        white_king = Point(3, 5, "wk")
        target_cell = Point(3, 3)
        motion = board._make_motion(white_king, target_cell)

        self.assertFalse(
            board._checking_the_move_is_correct(white_king, motion))

        # Test 10
        white_king = Point(3, 5, "wk")
        target_cell = Point(4, 4)
        motion = board._make_motion(white_king, target_cell)

        self.assertTrue(
            board._checking_the_move_is_correct(white_king, motion))
Esempio n. 33
0
def test_starting_pos_legal_moves():
    """Check that there are 20 legal moves in the starting position"""
    b = ChessBoard()
    lm = b.legal_moves()

    assert len(lm) == 20
Esempio n. 34
0
def test_castling_KS():
    """Test king side castling."""
    b = ChessBoard()
    b = b.make_move(((1, 4), (3, 4)))  # e4
    b = b.make_move(((6, 4), (4, 4)))  # e5
    b = b.make_move(((0, 6), (2, 5)))  # Nf3
    b = b.make_move(((7, 6), (5, 5)))  # Nf6
    b = b.make_move(((0, 5), (3, 2)))  # Bc4
    b = b.make_move(((7, 5), (4, 2)))  # Bc5

    # check white can castle now
    white_castle = ((0, 4), (0, 6))
    legal_moves = b.legal_moves()
    assert white_castle in legal_moves

    b = b.make_move(((0, 4), (1, 4)))  # Ke2

    # check black can castle now
    black_castle = ((7, 4), (7, 6))
    assert (black_castle in b.legal_moves())

    b = b.make_move(((7, 4), (6, 4)))  # Ke7

    b = b.make_move(((1, 4), (0, 4)))  # Ke1
    b = b.make_move(((6, 4), (7, 4)))  # Ke8

    # check white cannot castle
    assert white_castle not in b.legal_moves()

    b = b.make_move(((0, 1), (2, 2)))  # Nc3

    # check black cannot castle
    assert black_castle not in b.legal_moves()
Esempio n. 35
0
from chess import ChessBoard

if __name__ == "__main__":
    #test
    board = [
        [None ,None,"w3a",None,None,None,"b7a","b3a"],
        ["w5a","w6b",None,None,None,None,"b7b","b5a"],
        ["w4a","w6c",None,None,None,None,"b7c","b4a"],
        [None,None,"w2",None,None,None,"b7d","b2" ],
        ["w1" ,"w6e",None,None,None,None,"b7e","b1" ],
        ["w4b","w6f",None,None,None,None,"b7f","b4b"],
        ["w5b","w6g",None,None,None,None,"b7g","b5b"],
        ["w3b","w6h",None,None,None,None,"b7h","b3b"]
    ]
    game = ChessBoard(board)
    print "White Player plays:"
    game.play("white")
    print
    print "Black Player plays:"
    game.play("black")
    print
Esempio n. 36
0
from flask import Flask, request
from chess import Board as ChessBoard
from chess import Piece, QUEEN, Color, WHITE, BLACK

from src.board import init_board, init_gui_board, print_board, game_over
from src.state import State, Turn
from src.checkers import move_function, play_move


BOARD = ChessBoard(None)
init_gui_board(BOARD)
state_board = init_board()

app = Flask(__name__)


def human_move(src, dest):
    piece = BOARD.remove_piece_at(src)
    if dest > 55:
        BOARD.set_piece_at(dest, Piece(QUEEN, Color(WHITE)))
    else:
        BOARD.set_piece_at(dest, piece)

    jump = play_move(state_board, [src, dest], Turn.WHITE)
    if jump is not None:
        BOARD.remove_piece_at(jump)

    print_board(state_board)


def computer_move(move):