def test_taking_king_not_allowed_pawn(self): a4_pawn = Pawn(ChessCoord('C', '4'), black, directions.go_south) b3_king = King(ChessCoord('B', '3'), white) pieces = [a4_pawn, b3_king] move_inspect_result = a4_pawn.inspect_move(pieces, ChessCoord('B', '3')) self.failUnless(move_inspect_result == MoveInspectResult(False, True, [], b3_king))
def setUp(self): black_chess_coord = ChessCoord('B', '7') white_chess_coord = ChessCoord('A', '2') black_colour = black white_colour = white north_direction = go_north south_direction = go_south self.black_pawn = Pawn(black_chess_coord, black_colour, south_direction) self.white_pawn = Pawn(white_chess_coord, white_colour, north_direction)
def test_pawn_threat_squares_blocking_pieces(self): pawn_black_1 = Pawn(ChessCoord('C', '7'), black, go_south) pawn_black_2 = Pawn(ChessCoord('D', '6'), black, go_south) pieces = [pawn_black_1, pawn_black_2] expected_squares_chess = [ ChessCoord('B', '6') ] pawn_black_1.analyze_threats_on_board_for_new_move(pieces, ChessCoord('C', '7')) expected_squares_grid = map(chess_coord_to_grid_coord, expected_squares_chess) self.failUnless(util.compare_lists(expected_squares_grid, pawn_black_1.is_threat_to_these_squares))
def test_pawn_threat_squares_en_passant(self): pawn_black = Pawn(ChessCoord('C', '3'), black, go_south) pawn_white = Pawn(ChessCoord('D', '2'), white, go_north) pawn_white.update_coord(ChessCoord('D', '4')) pieces = [pawn_black, pawn_white] expected_squares_chess = [ ChessCoord('B', '2'), ChessCoord('D', '2') ] pawn_black.analyze_threats_on_board_for_new_move(pieces, ChessCoord('C', '3')) expected_squares_grid = map(chess_coord_to_grid_coord, expected_squares_chess) self.failUnless(util.compare_lists(expected_squares_grid, pawn_black.is_threat_to_these_squares))
def new_piece(color: int, piece_type: int, position: tuple[int, int], tilesize: int): """ Créée et renvoit une pièce du type passé. :param color: La couleur de la pièce :param piece_type: Le type de la pièce :param position: La case de l'échiquier :param tilesize: La taille en pixels des cases, pour mettre l'image de la pièce à l'échelle :return: La pièce appropriée """ if piece_type == ROI: return King(color, position, tilesize) if piece_type == REINE: return Queen(color, position, tilesize) if piece_type == TOUR: return Rook(color, position, tilesize) if piece_type == CAVALIER: return Knight(color, position, tilesize) if piece_type == FOU: return Bishop(color, position, tilesize) if piece_type == PION: return Pawn(color, position, tilesize)
def create_board(self): # A chess board is constitued of 64 tiles for tile in range(64): self.gameTiles[tile] = Tile(tile, NullPiece()) # Placing the pieces on the board # Placing black pices # First Line self.gameTiles[0] = Tile(0, Rook("Black", 0)) self.gameTiles[1] = Tile(1, Knight("Black", 1)) self.gameTiles[2] = Tile(2, Bishop("Black", 2)) self.gameTiles[3] = Tile(3, Queen("Black", 3)) self.gameTiles[4] = Tile(4, King("Black", 4)) self.gameTiles[5] = Tile(5, Bishop("Black", 5)) self.gameTiles[6] = Tile(6, Knight("Black", 6)) self.gameTiles[7] = Tile(7, Rook("Black", 7)) self.gameTiles[8] = Tile(8, Pawn("Black", 8)) self.gameTiles[9] = Tile(9, Pawn("Black", 9)) # Second Line self.gameTiles[10] = Tile(10, Pawn("Black", 10)) self.gameTiles[11] = Tile(11, Pawn("Black", 11)) self.gameTiles[12] = Tile(12, Pawn("Black", 12)) self.gameTiles[13] = Tile(13, Pawn("Black", 13)) self.gameTiles[14] = Tile(14, Pawn("Black", 14)) self.gameTiles[15] = Tile(15, Pawn("Black", 15)) # Placing the white pieces # First line self.gameTiles[48] = Tile(48, Pawn("White", 48)) self.gameTiles[49] = Tile(49, Pawn("White", 49)) self.gameTiles[50] = Tile(50, Pawn("White", 50)) self.gameTiles[51] = Tile(51, Pawn("White", 51)) self.gameTiles[52] = Tile(52, Pawn("White", 52)) self.gameTiles[53] = Tile(53, Pawn("White", 53)) self.gameTiles[54] = Tile(54, Pawn("White", 54)) self.gameTiles[55] = Tile(55, Pawn("White", 55)) # Second line self.gameTiles[56] = Tile(56, Rook("White", 56)) self.gameTiles[57] = Tile(57, Knight("White", 57)) self.gameTiles[58] = Tile(58, Bishop("White", 58)) self.gameTiles[59] = Tile(59, Queen("White", 59)) self.gameTiles[60] = Tile(60, King("White", 60)) self.gameTiles[61] = Tile(61, Bishop("White", 61)) self.gameTiles[62] = Tile(62, Knight("White", 62)) self.gameTiles[63] = Tile(63, Rook("White", 63))
def test_add_piece(self): board = Board(tk.Toplevel()) pawn = Pawn('white', 'white_pawn_x') board.add_piece(pawn) self.assertIsNotNone(board.canvas.find_withtag(pawn.name))
def __init__(self): self.state = [ [ Lance(p1), Knight(p1), Silver(p1), Gold(p1), King(p1), Gold(p1), Silver(p1), Knight(p1), Lance(p1) ], [None, Rook(p1), None, None, None, None, None, Bishop(p1), None], [ Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1) ], [None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None], [ Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2) ], [None, Bishop(p2), None, None, None, None, None, Rook(p2), None], [ Lance(p2), Knight(p2), Silver(p2), Gold(p2), King(p2), Gold(p2), Silver(p2), Knight(p2), Lance(p2) ], ]
def initiate_pieces(self): if self.player_color == "White": pieces = [ Rook(0, 0, "Black"), Knight(1, 0, "Black"), Bishop(2, 0, "Black"), Queen(3, 0, "Black"), King(4, 0, "Black"), Bishop(5, 0, "Black"), Knight(6, 0, "Black"), Rook(7, 0, "Black"), Pawn(0, 1, "Black", "Down"), Pawn(1, 1, "Black", "Down"), Pawn(2, 1, "Black", "Down"), Pawn(3, 1, "Black", "Down"), Pawn(4, 1, "Black", "Down"), Pawn(5, 1, "Black", "Down"), Pawn(6, 1, "Black", "Down"), Pawn(7, 1, "Black", "Down"), Rook(0, 7, "White"), Knight(1, 7, "White"), Bishop(2, 7, "White"), Queen(3, 7, "White"), King(4, 7, "White"), Bishop(5, 7, "White"), Knight(6, 7, "White"), Rook(7, 7, "White"), Pawn(0, 6, "White", "Up"), Pawn(1, 6, "White", "Up"), Pawn(2, 6, "White", "Up"), Pawn(3, 6, "White", "Up"), Pawn(4, 6, "White", "Up"), Pawn(5, 6, "White", "Up"), Pawn(6, 6, "White", "Up"), Pawn(7, 6, "White", "Up") ] else: pieces = [ Rook(0, 0, "White"), Knight(1, 0, "White"), Bishop(2, 0, "White"), Queen(3, 0, "White"), King(4, 0, "White"), Bishop(5, 0, "White"), Knight(6, 0, "White"), Rook(7, 0, "White"), Pawn(0, 1, "White", "Down"), Pawn(1, 1, "White", "Down"), Pawn(2, 1, "White", "Down"), Pawn(3, 1, "White", "Down"), Pawn(4, 1, "White", "Down"), Pawn(5, 1, "White", "Down"), Pawn(6, 1, "White", "Down"), Pawn(7, 1, "White", "Down"), Rook(0, 7, "Black"), Knight(1, 7, "Black"), Bishop(2, 7, "Black"), Queen(3, 7, "Black"), King(4, 7, "Black"), Bishop(5, 7, "Black"), Knight(6, 7, "Black"), Rook(7, 7, "Black"), Pawn(0, 6, "Black", "Up"), Pawn(1, 6, "Black", "Up"), Pawn(2, 6, "Black", "Up"), Pawn(3, 6, "Black", "Up"), Pawn(4, 6, "Black", "Up"), Pawn(5, 6, "Black", "Up"), Pawn(6, 6, "Black", "Up"), Pawn(7, 6, "Black", "Up") ] for piece in pieces: self.board[piece.row][piece.col] = piece
def init_pieces(width): return [ Rook(0, 0, PieceColor.BLACK, width), Knight(0, 1, PieceColor.BLACK, width), Bishop(0, 2, PieceColor.BLACK, width), Queen(0, 3, PieceColor.BLACK, width), King(0, 4, PieceColor.BLACK, width), Bishop(0, 5, PieceColor.BLACK, width), Knight(0, 6, PieceColor.BLACK, width), Rook(0, 7, PieceColor.BLACK, width), Pawn(1, 0, PieceColor.BLACK, width), Pawn(1, 1, PieceColor.BLACK, width), Pawn(1, 2, PieceColor.BLACK, width), Pawn(1, 3, PieceColor.BLACK, width), Pawn(1, 4, PieceColor.BLACK, width), Pawn(1, 5, PieceColor.BLACK, width), Pawn(1, 6, PieceColor.BLACK, width), Pawn(1, 7, PieceColor.BLACK, width), Pawn(6, 0, PieceColor.WHITE, width), Pawn(6, 1, PieceColor.WHITE, width), Pawn(6, 2, PieceColor.WHITE, width), Pawn(6, 3, PieceColor.WHITE, width), Pawn(6, 4, PieceColor.WHITE, width), Pawn(6, 5, PieceColor.WHITE, width), Pawn(6, 6, PieceColor.WHITE, width), Pawn(6, 7, PieceColor.WHITE, width), Rook(7, 0, PieceColor.WHITE, width), Knight(7, 1, PieceColor.WHITE, width), Bishop(7, 2, PieceColor.WHITE, width), Queen(7, 3, PieceColor.WHITE, width), King(7, 4, PieceColor.WHITE, width), Bishop(7, 5, PieceColor.WHITE, width), Knight(7, 6, PieceColor.WHITE, width), Rook(7, 7, PieceColor.WHITE, width), ]
def createNewBoard(self): self.board = [] for i in range(8): row = [] for j in range(8): row.append(NullPiece((i, j))) self.board.append(row) rookB1 = Rook('B') rookB2 = Rook('B') knightB1 = Knight('B') knightB2 = Knight('B') bishopB1 = Bishop('B') bishopB2 = Bishop('B') queenB = Queen('B') kingB = King('B') pawnB1 = Pawn('B') pawnB2 = Pawn('B') pawnB3 = Pawn('B') pawnB4 = Pawn('B') pawnB5 = Pawn('B') pawnB6 = Pawn('B') pawnB7 = Pawn('B') pawnB8 = Pawn('B') rookW1 = Rook('W') rookW2 = Rook('W') knightW1 = Knight('W') knightW2 = Knight('W') bishopW1 = Bishop('W') bishopW2 = Bishop('W') queenW = Queen('W') kingW = King('W') pawnW1 = Pawn('W') pawnW2 = Pawn('W') pawnW3 = Pawn('W') pawnW4 = Pawn('W') pawnW5 = Pawn('W') pawnW6 = Pawn('W') pawnW7 = Pawn('W') pawnW8 = Pawn('W') self.addPiece(rookB1, (0, 0)) self.addPiece(knightB1, (0, 1)) self.addPiece(bishopB1, (0, 2)) self.addPiece(queenB, (0, 3)) self.addPiece(kingB, (0, 4)) self.addPiece(bishopB2, (0, 5)) self.addPiece(knightB2, (0, 6)) self.addPiece(rookB2, (0, 7)) self.addPiece(pawnB1, (1, 0)) self.addPiece(pawnB2, (1, 1)) self.addPiece(pawnB3, (1, 2)) self.addPiece(pawnB4, (1, 3)) self.addPiece(pawnB5, (1, 4)) self.addPiece(pawnB6, (1, 5)) self.addPiece(pawnB7, (1, 6)) self.addPiece(pawnB8, (1, 7)) self.addPiece(pawnW1, (6, 0)) self.addPiece(pawnW2, (6, 1)) self.addPiece(pawnW3, (6, 2)) self.addPiece(pawnW4, (6, 3)) self.addPiece(pawnW5, (6, 4)) self.addPiece(pawnW6, (6, 5)) self.addPiece(pawnW7, (6, 6)) self.addPiece(pawnW8, (6, 7)) self.addPiece(rookW1, (7, 0)) self.addPiece(knightW1, (7, 1)) self.addPiece(bishopW1, (7, 2)) self.addPiece(queenW, (7, 3)) self.addPiece(kingW, (7, 4)) self.addPiece(bishopW2, (7, 5)) self.addPiece(knightW2, (7, 6)) self.addPiece(rookW2, (7, 7))
def test_silver_movement(): from_x = 4 from_y = 4 to_x = 5 to_y = 3 board = Board() board.state = [ [ Lance(p1), Knight(p1), None, Gold(p1), King(p1), Gold(p1), Silver(p1), Knight(p1), Lance(p1) ], [None, Rook(p1), None, None, None, None, None, Bishop(p1), None], [ Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1) ], [None, None, None, None, None, None, None, None, None], [None, None, None, None, Silver(p1), None, None, None, None], [None, None, None, None, None, None, None, None, None], [ Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2) ], [None, Bishop(p2), None, None, None, None, None, Rook(p2), None], [ Lance(p2), Knight(p2), Silver(p2), Gold(p2), King(p2), Gold(p2), Silver(p2), Knight(p2), Lance(p2) ], ] selected_piece = board.state[from_x][from_y] # If the target position (to_x_y) is on the board, we check if the selected piece can make the move if selected_piece.on_board(from_x, from_y, to_x, to_y): # Then the board is updated and the turn is changed if (selected_piece.correct_move(from_x, from_y, to_x, to_y)): board.state[from_x][from_y] = None board.state[to_x][to_y] = selected_piece # target piece must be equal to selected piece assert board.state[5][3].piece_type == "S"
def _create_pawns(tiles, row, owner, allowed_move_direction, allowed_attack_directions): for tile in row: tile.piece = Pawn(owner, allowed_move_direction, allowed_attack_directions)
def test_king_checked(self): matrix = self.initialize_empty_grid() matrix[(1, 4)]['piece'] = Pawn('white', 'white_pawn_1') matrix[(0, 3)]['piece'] = King('black', 'black_king') self.assertTrue(check_all(matrix, GameState.blackcoord, 'black'))
def createBoard(self): for c in range(65): self.gameTiles.append(None) for tile in range(65): self.gameTiles[tile] = Tile(tile, NullPiece()) self.gameTiles[1] = Tile(1, Rook(1, "Black")) self.gameTiles[2] = Tile(2, Knight(2, "Black")) self.gameTiles[3] = Tile(3, Bishop(3, "Black")) self.gameTiles[4] = Tile(4, Queen(4, "Black")) self.gameTiles[5] = Tile(5, King(5, "Black")) self.gameTiles[6] = Tile(6, Bishop(6, "Black")) self.gameTiles[7] = Tile(7, Knight(7, "Black")) self.gameTiles[8] = Tile(8, Rook(8, "Black")) self.gameTiles[9] = Tile(9, Pawn(9, "Black")) self.gameTiles[10] = Tile(10, Pawn(10, "Black")) self.gameTiles[11] = Tile(11, Pawn(11, "Black")) self.gameTiles[12] = Tile(12, Pawn(12, "Black")) self.gameTiles[13] = Tile(13, Pawn(13, "Black")) self.gameTiles[14] = Tile(14, Pawn(14, "Black")) self.gameTiles[15] = Tile(15, Pawn(15, "Black")) self.gameTiles[16] = Tile(16, Pawn(16, "Black")) #self.gameTiles[35] = Tile(35, Bishop(35, "White")) #self.gameTiles[14] = Tile(14,Queen(14, "White")) self.gameTiles[49] = Tile(49, Pawn(49, "White")) self.gameTiles[50] = Tile(50, Pawn(50, "White")) self.gameTiles[51] = Tile(51, Pawn(51, "White")) self.gameTiles[52] = Tile(52, Pawn(52, "White")) self.gameTiles[53] = Tile(53, Pawn(53, "White")) self.gameTiles[54] = Tile(54, Pawn(54, "White")) self.gameTiles[55] = Tile(55, Pawn(55, "White")) self.gameTiles[56] = Tile(56, Pawn(56, "White")) self.gameTiles[57] = Tile(57, Rook(57, "White")) self.gameTiles[58] = Tile(58, Knight(58, "White")) self.gameTiles[59] = Tile(59, Bishop(59, "White")) self.gameTiles[60] = Tile(60, Queen(60, "White")) self.gameTiles[61] = Tile(61, King(61, "White")) self.gameTiles[62] = Tile(62, Bishop(62, "White")) self.gameTiles[63] = Tile(63, Knight(63, "White")) self.gameTiles[64] = Tile(64, Rook(64, "White"))
def move_pawn(self, p, y, player): # make the move piece = Pawn(self.board) move = piece.move(p, y, player) return move
def gatekeeper(self, x: int, y: int, nx: int, ny: int, review_mode: bool, promote_to: str = '') -> bool: legal = True piece = self.chessboard[y][x] destination = self.chessboard[ny][nx] piece_class = self.get_class(piece) # check that a piece was selected if piece == '': legal = False else: # piece selected was of the players color if self.white_turn != piece.isupper(): legal = False # no friendly fire if legal and destination != '' and self.white_turn == destination.isupper( ): legal = False # check if player wants to castle if legal and piece.upper() == 'K' and not self.check() and abs( x - nx) == 2: legal = self.can_castle(x, y, nx, ny) # check if can do en passant elif legal and self.en_passant and piece.upper() == 'P'\ and nx == self.en_passant_x and ny == self.en_passant_y: legal = Pawn.can_move(x, y, nx, ny, True, piece.isupper()) # all normal moves elif legal and not piece_class.can_move( x, y, nx, ny, destination != '', piece.isupper()): legal = False # check that there's no jumping pieces if legal and piece.upper() not in ('N', 'K'): legal = not self.jumps(x, y, nx, ny) # check white king doesn't move to a controlled square if legal and piece.upper( ) == 'K' and self.white_turn and self.black_controlled[ny][nx]: legal = False # check black king doesn't move to a controlled square elif legal and piece.upper( ) == 'K' and not self.white_turn and self.white_controlled[ny][nx]: legal = False # check that we're not moving pinned pieces if legal and piece.upper() != 'K' and self.check_laser( x, y, nx, ny): legal = False # check if the player can promote if legal and piece.upper() == 'P' \ and ((self.white_turn and ny == 0) or (not self.white_turn and ny == 7)): legal = promote_to in ('queen', 'rook', 'bishop', 'knight') else: promote_to = '' # if the player is in check, see if he manages to get out of check if legal and self.check(): attacking_pieces = self.get_attacking() # moving outside of check if piece.upper() == 'K': if self.white_turn and self.black_controlled[ny][nx]: legal = False elif not self.white_turn and self.white_controlled[ny][nx]: legal = False # if the king is being attacked by more than one piece, there's no way to block or take both of them elif len(attacking_pieces) > 1: legal = False # take the attacking piece elif destination != '': if (ny, nx) != attacking_pieces[0]: legal = False # block the attacking piece elif destination == '': ty, tx = attacking_pieces[0] if (ny, nx) not in self.check_block( tx, ty, not self.white_turn): legal = False if not review_mode and legal: self.execute(x, y, nx, ny, promote_to) return legal
def createBoard(self): square_color = ["light", "dark"] x = 0 for i in range(64): self.squares[i] = Square(square_color[x % 2], i, nullPiece()) if i % 8 == 0 and i != 0: x += 2 else: x += 1 self.squares[0] = Square("light", 0, Rook("Black", 0)) self.squares[1] = Square("dark", 1, Knight("Black", 1)) self.squares[2] = Square("light", 2, Bishop("Black", 2)) self.squares[3] = Square("dark", 3, Queen("Black", 3)) self.squares[4] = Square("light", 4, self.black_king) self.squares[5] = Square("dark", 5, Bishop("Black", 5)) self.squares[6] = Square("light", 6, Knight("Black", 6)) self.squares[7] = Square("dark", 7, Rook("Black", 7)) self.squares[8] = Square("dark", 8, Pawn("Black", 8)) self.squares[9] = Square("light", 9, Pawn("Black", 9)) self.squares[10] = Square("dark", 10, Pawn("Black", 10)) self.squares[11] = Square("light", 11, Pawn("Black", 11)) self.squares[12] = Square("dark", 12, Pawn("Black", 12)) self.squares[13] = Square("light", 13, Pawn("Black", 13)) self.squares[14] = Square("dark", 14, Pawn("Black", 14)) self.squares[15] = Square("light", 15, Pawn("Black", 15)) self.squares[16] = Square("light", 16, nullPiece()) self.squares[24] = Square("dark", 24, nullPiece()) self.squares[32] = Square("light", 32, nullPiece()) self.squares[40] = Square("dark", 40, nullPiece()) self.squares[48] = Square("light", 48, Pawn("White", 48)) self.squares[49] = Square("dark", 49, Pawn("White", 49)) self.squares[50] = Square("light", 50, Pawn("White", 50)) self.squares[51] = Square("dark", 51, Pawn("White", 51)) self.squares[52] = Square("light", 52, Pawn("White", 52)) self.squares[53] = Square("dark", 53, Pawn("White", 53)) self.squares[54] = Square("light", 54, Pawn("White", 54)) self.squares[55] = Square("dark", 55, Pawn("White", 55)) self.squares[56] = Square("dark", 56, Rook("White", 56)) self.squares[57] = Square("light", 57, Knight("White", 57)) self.squares[58] = Square("dark", 58, Bishop("White", 58)) self.squares[59] = Square("light", 59, Queen("White", 59)) self.squares[60] = Square("dark", 60, self.white_king) self.squares[61] = Square("light", 61, Bishop("White", 61)) self.squares[62] = Square("dark", 62, Knight("White", 62)) self.squares[63] = Square("light", 63, Rook("White", 63))
def createBoard(self): for square in range(64): self.squares[square] = Square(square, EmptyPiece()) self.squares[0] = Square(0, Rook("Black", 0)) self.squares[1] = Square(1, Knight("Black", 1)) self.squares[2] = Square(2, Bishop("Black", 2)) self.squares[3] = Square(3, Queen("Black", 3)) self.squares[4] = Square(4, King("Black", 4)) self.squares[5] = Square(5, Bishop("Black", 5)) self.squares[6] = Square(6, Knight("Black", 6)) self.squares[7] = Square(7, Rook("Black", 7)) self.squares[8] = Square(8, Pawn("Black", 8)) self.squares[9] = Square(9, Pawn("Black", 9)) self.squares[10] = Square(10, Pawn("Black", 10)) self.squares[11] = Square(11, Pawn("Black", 11)) self.squares[12] = Square(12, Pawn("Black", 12)) self.squares[13] = Square(13, Pawn("Black", 13)) self.squares[14] = Square(14, Pawn("Black", 14)) self.squares[15] = Square(15, Pawn("Black", 15)) self.squares[56] = Square(56, Rook("White", 56)) self.squares[57] = Square(57, Knight("White", 57)) self.squares[58] = Square(58, Bishop("White", 58)) self.squares[59] = Square(59, Queen("White", 59)) self.squares[60] = Square(60, King("White", 60)) self.squares[61] = Square(61, Bishop("White", 61)) self.squares[62] = Square(62, Knight("White", 62)) self.squares[63] = Square(63, Rook("White", 63)) self.squares[48] = Square(48, Pawn("White", 48)) self.squares[49] = Square(49, Pawn("White", 49)) self.squares[50] = Square(50, Pawn("White", 50)) self.squares[51] = Square(51, Pawn("White", 51)) self.squares[52] = Square(52, Pawn("White", 52)) self.squares[53] = Square(53, Pawn("White", 53)) self.squares[54] = Square(54, Pawn("White", 54)) self.squares[55] = Square(55, Pawn("White", 55))
class PawnTests(unittest.TestCase): def setUp(self): black_chess_coord = ChessCoord('B', '7') white_chess_coord = ChessCoord('A', '2') black_colour = black white_colour = white north_direction = go_north south_direction = go_south self.black_pawn = Pawn(black_chess_coord, black_colour, south_direction) self.white_pawn = Pawn(white_chess_coord, white_colour, north_direction) def test_constructor_black(self): self.failUnless(self.black_pawn.letter is 'P') self.failUnless(self.black_pawn.colour is black) self.failUnless(self.black_pawn.move_directions == [go_south]) self.failUnless(self.black_pawn.chess_coord == ChessCoord('B', '7')) self.failUnless(self.black_pawn.grid_coord == GridCoord(1, 6)) def test_constructor_white(self): self.failUnless(self.white_pawn.letter is 'P') self.failUnless(self.white_pawn.colour is white) self.failUnless(self.white_pawn.move_directions == [go_north]) self.failUnless(self.white_pawn.chess_coord == ChessCoord('A', '2')) self.failUnless(self.white_pawn.grid_coord == GridCoord(0, 1)) def test_is_invalid_move_two_step_from_not_start(self): pieces = [] self.white_pawn.update_coord(ChessCoord('D', '3')) self.failIf(self.white_pawn.inspect_move(pieces, ChessCoord('D', '5')). is_valid_move) def test_is_invalid_move_two_step_from_start_wrong_direction(self): pieces = [] self.white_pawn.update_coord(ChessCoord('A', '7')) self.failIf(self.white_pawn.inspect_move(pieces, ChessCoord('A', '5')). is_valid_move) def test_update_coors_white(self): self.white_pawn.update_coord(ChessCoord('A', '3')) self.failUnless(self.white_pawn.grid_coord == GridCoord(0, 2)) def test_update_coors_black(self): self.black_pawn.update_coord(ChessCoord('C', '6')) self.failUnless(self.black_pawn.grid_coord == GridCoord(2, 5)) def test_is_valid_move_one_step_black(self): pieces = [] self.failUnless(self.black_pawn.inspect_move(pieces, ChessCoord('B', '6')). is_valid_move) def test_is_valid_move_one_step_white(self): pieces = [] self.failUnless(self.white_pawn.inspect_move(pieces, ChessCoord('A', '3')). is_valid_move) def test_is_invalid_move_backwards(self): pieces = [] self.black_pawn.update_coord(ChessCoord('B', '5')) self.failIf(self.white_pawn.inspect_move(pieces, ChessCoord('B', '4')). is_valid_move) def test_is_valid_move_two_step_from_start_white(self): pieces = [] self.black_pawn.update_coord(ChessCoord('A', '2')) self.failUnless(self.white_pawn.inspect_move(pieces, ChessCoord('A', '4')). is_valid_move) def test_is_valid_move_two_step_from_start_black(self): pieces = [] self.black_pawn.update_coord(ChessCoord('C', '7')) self.failUnless(self.black_pawn.inspect_move(pieces, ChessCoord('C', '5')). is_valid_move) def test_inspect_move_taking_enemy(self): pieces = [self.white_pawn, self.black_pawn] self.white_pawn.update_coord(ChessCoord('B', '6')) self.black_pawn.update_coord(ChessCoord('C', '7')) move_inspect_result = self.black_pawn.inspect_move(pieces, ChessCoord('B', '6')) self.failUnless(move_inspect_result == MoveInspectResult(True, False, [GridCoord(1, 5)], self.white_pawn)) def test_inspect_move_blocked_by_friendly_taking(self): pieces = [Pawn(ChessCoord('C', '6'), white, go_north), self.white_pawn] self.white_pawn.update_coord(ChessCoord('B', '5')) move_inspect_result = self.white_pawn.inspect_move(pieces, ChessCoord('C', '6')) self.failUnless(move_inspect_result == MoveInspectResult(False, True, [], pieces[0])) def test_inspect_move_blocked_by_enemy(self): pieces = [self.white_pawn, self.black_pawn] self.white_pawn.update_coord(ChessCoord('B', '2')) self.black_pawn.update_coord(ChessCoord('B', '3')) move_inspect_result = self.white_pawn.inspect_move(pieces, ChessCoord('B', '3')) self.failUnless(move_inspect_result == MoveInspectResult(False, True, [GridCoord(1, 2)], self.black_pawn)) def test_inspect_move_blocked_by_friendly(self): pieces = [Pawn(ChessCoord('D', '4'), black, go_south), self.black_pawn] self.black_pawn.update_coord(ChessCoord('D', '5')) move_inspect_result = self.black_pawn.inspect_move(pieces, ChessCoord('D', '4')) self.failUnless(move_inspect_result == MoveInspectResult(False, True, [GridCoord(3, 3)], pieces[0])) def test_inspect_move_two_steps_blocked_by_friendly(self): pieces = [Pawn(ChessCoord('C', '6'), black, go_south), self.black_pawn] self.black_pawn.update_coord(ChessCoord('C', '7')) move_inspect_result = self.black_pawn.inspect_move(pieces, ChessCoord('C', '5')) self.failUnless(move_inspect_result == MoveInspectResult(False, True, [], pieces[0])) def test_inspect_move_two_steps_blocked_by_enemy(self): pieces = [Pawn(ChessCoord('C', '6'), white, go_south), self.black_pawn] self.black_pawn.update_coord(ChessCoord('C', '7')) move_inspect_result = self.black_pawn.inspect_move(pieces, ChessCoord('C', '5')) self.failUnless(move_inspect_result == MoveInspectResult(False, True, [], pieces[0])) def test_en_passant_square_set(self): self.black_pawn.update_coord(ChessCoord('C', '7')) self.failIf(self.black_pawn.en_passant_square) self.black_pawn.update_coord(ChessCoord('C', '5')) self.failUnless(self.black_pawn.en_passant_square == GridCoord(2, 5)) def test_en_passant_can_be_taken(self): self.black_pawn.update_coord(ChessCoord('E', '7')) self.black_pawn.update_coord(ChessCoord('E', '5')) self.failUnless(self.black_pawn.en_passant_square == GridCoord(4, 5)) pieces = [self.black_pawn, self.white_pawn, Rook(ChessCoord('A', '1'), black)] self.white_pawn.update_coord(ChessCoord('F', '5')) move_inspect_result = self.white_pawn.inspect_move(pieces, ChessCoord('E', '6')) self.failUnless(move_inspect_result == MoveInspectResult(True, False, [GridCoord(4, 5)], self.black_pawn)) def test_en_passant_is_removed_when_pawn_is_moved_again(self): self.white_pawn.update_coord(ChessCoord('A', '2')) self.failIf(self.white_pawn.en_passant_square) self.white_pawn.update_coord(ChessCoord('A', '4')) self.failUnless(self.white_pawn.en_passant_square == GridCoord(0, 2)) self.white_pawn.update_coord(ChessCoord('A', '5')) self.failIf(self.white_pawn.en_passant_square) def test_black_pawn_not_allowed_to_take_pawn_across_board(self): all_pieces = copy.deepcopy(starting_pieces) g7_pawn = select_piece(ChessCoord('G', '7'), all_pieces) inspect_move_result = g7_pawn.inspect_move(all_pieces, ChessCoord('H', '2')) self.failIf(inspect_move_result.is_valid_move)
def _populate_pawns(self, row, color, direction): for i in range(8): self.grid[row][i] = Pawn(self, [row, i], color, direction)
def pawn_en_passant_test(): print("========================================") print("Performing the pawn en passant test") print("========================================") board = Board() white_pawn_one = Pawn(Colour.WHITE) black_pawn_one = Pawn(Colour.BLACK) black_pawn_two = Pawn(Colour.BLACK) wp1_loc = Location('b', 2) bp1_loc = Location('a', 7) bp2_loc = Location('c', 7) board.__add_piece__(white_pawn_one, wp1_loc) board.__add_piece__(black_pawn_one, bp1_loc) board.__add_piece__(black_pawn_two, bp2_loc) print("The board was seeded with the following pieces: ") print("{} pawn at position {}".format(white_pawn_one.colour, wp1_loc)) print("{} pawn at position {}".format(black_pawn_one.colour, bp1_loc)) print("{} pawn at position {}".format(black_pawn_two.colour, bp2_loc)) print(board) # White pawn moves print("For the {} pawn starting at position {} the moves are:".format( white_pawn_one.colour, wp1_loc)) allowed_moves = white_pawn_one.allowed_moves(wp1_loc, board) print(allowed_moves) new_location = allowed_moves[1] board.move_piece(white_pawn_one, wp1_loc, new_location) wp1_loc = new_location print("Moved the pawn to position {}".format(wp1_loc)) print(board) # Black pawn moves print("For the {} pawn starting at position {} the moves are:".format( black_pawn_one.colour, bp1_loc)) allowed_moves = black_pawn_one.allowed_moves(bp1_loc, board) print(allowed_moves) new_location = allowed_moves[1] print("For the {} pawn starting at position {} the moves are:".format( black_pawn_two.colour, bp2_loc)) allowed_moves = black_pawn_two.allowed_moves(bp2_loc, board) print(allowed_moves) board.move_piece(black_pawn_one, bp1_loc, new_location) print("Moved the {} pawn at {} to position {}".format( black_pawn_one.colour, bp1_loc, new_location)) bp1_loc = new_location print(board) # White pawn moves print("For the {} pawn at position {} the moves are:".format( white_pawn_one.colour, wp1_loc)) allowed_moves = white_pawn_one.allowed_moves(wp1_loc, board) print(allowed_moves) new_location = allowed_moves[0] board.move_piece(white_pawn_one, wp1_loc, new_location) wp1_loc = new_location print("Moved the pawn to position {}".format(wp1_loc)) print(board) # Black pawn moves print("For the {} pawn starting at position {} the moves are:".format( black_pawn_one.colour, bp1_loc)) allowed_moves = black_pawn_one.allowed_moves(bp1_loc, board) print(allowed_moves) print("For the {} pawn starting at position {} the moves are:".format( black_pawn_two.colour, bp2_loc)) allowed_moves = black_pawn_two.allowed_moves(bp2_loc, board) print(allowed_moves) new_location = allowed_moves[1] board.move_piece(black_pawn_two, bp2_loc, new_location) print("Moved the {} pawn at {} to position {}".format( black_pawn_two.colour, bp2_loc, new_location)) bp2_loc = new_location print(board) # White pawn moves print("For the {} pawn at position {} the moves are:".format( white_pawn_one.colour, wp1_loc)) allowed_moves = white_pawn_one.allowed_moves(wp1_loc, board) print(allowed_moves) new_location = allowed_moves[1] board.move_piece(white_pawn_one, wp1_loc, new_location) wp1_loc = new_location print("Moved the pawn to position {}".format(wp1_loc)) print(board) print("The move history for this board is: ") [print(item) for item in board.move_history]
return {} if __name__ == '__main__': pygame.init() chessGrid = Grid() chessGrid.setScreen() chessGrid.drawGrid() net = Network() # lets players switch turn by sending opponents ID when turn changes so they match and gameState saves (server 122) temp = [net.id] gameState = { '1': { '1': Rook(1, 1, 'images/blackRook.png', 1), '2': Pawn(1, 2, 'images/blackPawn.png', 1), '3': None, '4': None, '5': None, '6': None, '7': Pawn(1, 7, 'images/whitePawn.png', 0), '8': Rook(1, 8, 'images/whiteRook.png', 0) }, '2': { '1': Knight(2, 1, 'images/blackKnight.png', 1), '2': Pawn(2, 2, 'images/blackPawn.png', 1), '3': None, '4': None, '5': None, '6': None, '7': Pawn(2, 7, 'images/whitePawn.png', 0),