示例#1
0
    def test_move(self):
        board = ChessBoard()
        p1 = board.get_white_pieces()[0]
        p1_row, p1_col = p1.get_position()

        with (self.assertRaises(ValueError)):
            p1.move(p1_row - 1, p1_col + 1)
示例#2
0
 def __init__(self, flip_board):
     self.flip_board = flip_board
     self.board = ChessBoard(self.flip_board)
     self.player1 = ChessPlayer(constants.BLANK_CHAR, self.board, None)
     self.player2 = ChessPlayer(constants.BLANK_CHAR, self.board, None)
     if self.flip_board:
         constants.BOARD_CHARS.reverse()
     else:
         constants.BOARD_NUMS.reverse()
示例#3
0
    def test_is_blocked_vert(self):
        board = ChessBoard()

        rook = Rook(board, sample.helpers.constants.WHITE, 5, 2)
        board.set_square(rook)

        #Test vert. non blocked
        self.assertEqual(board.is_blocked(5, 2, 2, 2), False)
        rook.set_position(2, 3)
        board.set_square(rook)
        self.assertEqual(board.is_blocked(2, 3, 5, 3), False)
示例#4
0
 def test_is_blocked_invalid_path(self):
     board = ChessBoard()
     bishop = Bishop(board, sample.helpers.constants.WHITE, 3, 1)
     board.set_square(bishop)
     # Non straight path given
     with (self.assertRaises(ValueError)):
         board.is_blocked(3, 1, 4, 4)
示例#5
0
    def test_is_blocked_horz(self):
        board = ChessBoard()
        rook = Rook(board, sample.helpers.constants.WHITE, 5, 2)
        board.set_square(rook)

        # Test horiz. non-blocked
        # Rook 5,2 --> 5,6
        self.assertEqual(board.is_blocked(5, 2, 5, 6), False)
        # Rook 5,2 --> 5,0
        self.assertEqual(board.is_blocked(5, 2, 5, 0), False)

        self.assertEqual(board.is_blocked(5, 2, 6, 2), True)
示例#6
0
    def test_is_blocked_diag(self):
        board = ChessBoard()

        #Test diagonal directions non blocked
        bishop = Bishop(board, sample.helpers.constants.WHITE, 4, 4)
        board.set_square(bishop)
        self.assertEqual(board.is_blocked(4, 4, 2, 2), False)
        self.assertEqual(board.is_blocked(4, 4, 2, 6), False)
        self.assertEqual(board.is_blocked(4, 4, 5, 3), False)
        self.assertEqual(board.is_blocked(4, 4, 5, 5), False)
示例#7
0
class ChessGame(Game):
    def __init__(self, flip_board):
        self.flip_board = flip_board
        self.board = ChessBoard(self.flip_board)
        self.player1 = ChessPlayer(constants.BLANK_CHAR, self.board, None)
        self.player2 = ChessPlayer(constants.BLANK_CHAR, self.board, None)
        if self.flip_board:
            constants.BOARD_CHARS.reverse()
        else:
            constants.BOARD_NUMS.reverse()

    def start(self):
        self.init_players()
        self.play_game()

    def print_board(self):
        print(self.board)

    def change_current_player(self):
        if self.current_player == self.player1:
            self.current_player = self.player2
        else:
            self.current_player = self.player1

    def init_players(self):
        p1_name = input("Enter player 1 name : ")
        p2_name = input("Enter player 2 name : ")
        self.player1.set_name(p1_name)
        self.player2.set_name(p2_name)
        while True:
            try:
                p1_color = input(p1_name + ", do you want black or white? ")
                self.player1.set_color(p1_color)
                break
            except ValueError:
                print("Color must be either black or white.")

        self.player2.set_color(
            constants.get_other_color(self.player1.get_color()))
        if (self.player1.color == "white"):
            self.current_player = self.player1
        else:
            self.current_player = self.player2

    def get_player_1(self):
        return self.player1

    def get_player_2(self):
        return self.player2

#   TODO// handle switched board

    def parse_move(self, move):
        move = move.lower()

        if len(move) == 2:
            row, col = self.convert_to_row_col(move)
            print(row, col)
            return 'p', self.current_player.get_color(), row, col
        elif len(move) == 3:
            row, col = self.convert_to_row_col(move[1:])
            return move[0], self.current_player.get_color(), row, col
        elif len(move) == 4:
            pass  # specific pawn move
        elif len(move) == 6:
            pass  #specific move:
        else:
            raise ValueError("Invalid move input.")

    def convert_to_row_col(self, letter_number):
        letter = letter_number[0]
        number = letter_number[1]
        return constants.BOARD_NUMS.index(
            int(number)), constants.BOARD_CHARS.index(letter)

    def play_game(self):
        game_over = False
        while (not game_over):
            os.system('clear')
            print(self.board)
            print(self.current_player.get_name() + ", it's your turn. ")

            moveSuccess = False
            move_string = input("Move: ")
            while (not moveSuccess):
                if (move_string == "quit"):
                    game_over = True
                    break
                try:
                    (pieceType, color, row, col) = self.parse_move(move_string)
                except ValueError:
                    move_string = input(
                        "Bad move dude. Baaaaaaad move. Try again: ")
                    continue
                moveSuccess = self.board.makeMove(pieceType, color, row, col)
                if not moveSuccess:
                    move_string = input(
                        "Invalid move. Please enter a new, valid move: ")
                    continue
                else:
                    break

            self.change_current_player()
            input("PAUSED for debugging. Press any key to continue.")
示例#8
0
    def test_is_blocked_taking_piece(self):
        board = ChessBoard()
        p1 = Pawn(board, sample.helpers.constants.WHITE, 4, 3)
        p2 = Pawn(board, sample.helpers.constants.BLACK, 3, 4)
        board.set_square(p1)
        board.set_square(p2)
        p3 = Pawn(board, sample.helpers.constants.BLACK, 2, 1)
        board.set_square(p3)
        p4 = Pawn(board, sample.helpers.constants.BLACK, 6, 1)
        board.set_square(p4)
        p5 = Pawn(board, sample.helpers.constants.BLACK, 6, 5)
        board.set_square(p5)
        p6 = Pawn(board, sample.helpers.constants.BLACK, 4, 5)
        board.set_square(p6)
        p7 = Pawn(board, sample.helpers.constants.BLACK, 4, 0)
        board.set_square(p7)
        p8 = Pawn(board, sample.helpers.constants.BLACK, 1, 3)
        board.set_square(p8)
        p9 = Pawn(board, sample.helpers.constants.BLACK, 5, 3)
        board.set_square(p9)

        self.assertEqual(board.is_blocked(4, 3, 6, 5), False)
        self.assertEqual(board.is_blocked(4, 3, 2, 1), False)
        self.assertEqual(board.is_blocked(4, 3, 3, 4), False)
        self.assertEqual(board.is_blocked(4, 3, 6, 1), False)
        self.assertEqual(board.is_blocked(4, 3, 4, 5), False)
        self.assertEqual(board.is_blocked(4, 3, 4, 0), False)
        self.assertEqual(board.is_blocked(4, 3, 1, 3), False)
        self.assertEqual(board.is_blocked(4, 3, 5, 3), False)
示例#9
0
 def test_is_blocked_missing_start_piece(self):
     board = ChessBoard()
     #Test error cases:
     # No piece exists at start position
     with (self.assertRaises(ValueError)):
         board.is_blocked(3, 3, 4, 4)
示例#10
0
 def test_flipped(self):
     ChessBoard(flip_board=True)
示例#11
0
 def test_initialization(self):
     ChessBoard()