Skip to main content
deleted 66 characters in body
Source Link
Mark H
  • 2.4k
  • 14
  • 19

There is no restriction on the number of any other piece. Because of pawn promotion, a pawn can be converted into any other non-king piece. It is legal for there to be 2, 3, 4, or even 9 white queens on the board (unlikely, but legal). So, the line where you check the number of each type of piece if current_pieces.count(p) > all_pieces[p]: in is_valid_p() is incorrect. But, the fix does not require much change since the code in this

There is no restriction on the number of any other piece. Because of pawn promotion, a pawn can be converted into any other non-king piece. It is legal for there to be 2, 3, 4, or even 9 white queens on the board (unlikely, but legal). So, the line where you check the number of each type of piece if current_pieces.count(p) > all_pieces[p]: in is_valid_p() is incorrect. But, the fix does not require much change since the code in this

There is no restriction on the number of any other piece. Because of pawn promotion, a pawn can be converted into any other non-king piece. It is legal for there to be 2, 3, 4, or even 9 white queens on the board (unlikely, but legal). So, the line where you check the number of each type of piece if current_pieces.count(p) > all_pieces[p]: in is_valid_p() is incorrect.

Make list from iterator to get length
Source Link
Mark H
  • 2.4k
  • 14
  • 19
def isValidPieceSet(current_pieces): #create a dictionary with all the pieces of each color as the key # and the valid starting number of the pieces as value pieces = ['pawn', 'knight', 'bishop', 'rook', 'queen', 'king'] colors = ['w', 'b'] all_pieces = [] for color in colors: for p in pieces: all_pieces.append(color + p) #accepts a list, checking if the pieces of the chessboard are valid if 'bking' not in current_pieces or 'wking' not in current_pieces: return False for c_p in current_pieces: #checking if the pieces' names are valid if c_p not in all_pieces: return False if current_pieces.count('wpawn') > 8: return False if current_pieces.count('bpawn') > 8: return False # Count the white pieces if len(p[p for p in current_pieces if p.startswith('w')]) > 16: return False # Count the black pieces if len(p[p for p in current_pieces if p.startswith('b')]) > 16: return False return True 
def isValidPieceSet(current_pieces): #create a dictionary with all the pieces of each color as the key # and the valid starting number of the pieces as value pieces = ['pawn', 'knight', 'bishop', 'rook', 'queen', 'king'] colors = ['w', 'b'] all_pieces = [] for color in colors: for p in pieces: all_pieces.append(color + p) #accepts a list, checking if the pieces of the chessboard are valid if 'bking' not in current_pieces or 'wking' not in current_pieces: return False for c_p in current_pieces: #checking if the pieces' names are valid if c_p not in all_pieces: return False if current_pieces.count('wpawn') > 8: return False if current_pieces.count('bpawn') > 8: return False # Count the white pieces if len(p[p for p in current_pieces if p.startswith('w')]) > 16: return False # Count the black pieces if len(p[p for p in current_pieces if p.startswith('b')]) > 16: return False return True def isValidChessboard(chessboard): # function accept a dict for position in chessboard: #checking coordinates of the square if len(position) != 2: return False elif int(position[0]) not in range(1, 9) or position[1] not in 'abcdefgh': return False current_pieces = chessboard.values() return isValidPieceSet(current_pieces): #checking the pieces itself if not isValidChessboard(chessboard): print("Your board is invalid") else: print("Your board is valid") 
def isValidPieceSet(current_pieces): #create a dictionary with all the pieces of each color as the key # and the valid starting number of the pieces as value pieces = ['pawn', 'knight', 'bishop', 'rook', 'queen', 'king'] colors = ['w', 'b'] all_pieces = [] for color in colors: for p in pieces: all_pieces.append(color + p) #accepts a list, checking if the pieces of the chessboard are valid if 'bking' not in current_pieces or 'wking' not in current_pieces: return False for c_p in current_pieces: #checking if the pieces' names are valid if c_p not in all_pieces: return False if current_pieces.count('wpawn') > 8: return False if current_pieces.count('bpawn') > 8: return False # Count the white pieces if len(p for p in current_pieces if p.startswith('w')) > 16: return False # Count the black pieces if len(p for p in current_pieces if p.startswith('b')) > 16: return False return True 
def isValidPieceSet(current_pieces): #create a dictionary with all the pieces of each color as the key # and the valid starting number of the pieces as value pieces = ['pawn', 'knight', 'bishop', 'rook', 'queen', 'king'] colors = ['w', 'b'] all_pieces = [] for color in colors: for p in pieces: all_pieces.append(color + p) #accepts a list, checking if the pieces of the chessboard are valid if 'bking' not in current_pieces or 'wking' not in current_pieces: return False for c_p in current_pieces: #checking if the pieces' names are valid if c_p not in all_pieces: return False if current_pieces.count('wpawn') > 8: return False if current_pieces.count('bpawn') > 8: return False # Count the white pieces if len(p for p in current_pieces if p.startswith('w')) > 16: return False # Count the black pieces if len(p for p in current_pieces if p.startswith('b')) > 16: return False return True def isValidChessboard(chessboard): # function accept a dict for position in chessboard: #checking coordinates of the square if len(position) != 2: return False elif int(position[0]) not in range(1, 9) or position[1] not in 'abcdefgh': return False current_pieces = chessboard.values() return isValidPieceSet(current_pieces): #checking the pieces itself if not isValidChessboard(chessboard): print("Your board is invalid") else: print("Your board is valid") 
def isValidPieceSet(current_pieces): #create a dictionary with all the pieces of each color as the key # and the valid starting number of the pieces as value pieces = ['pawn', 'knight', 'bishop', 'rook', 'queen', 'king'] colors = ['w', 'b'] all_pieces = [] for color in colors: for p in pieces: all_pieces.append(color + p) #accepts a list, checking if the pieces of the chessboard are valid if 'bking' not in current_pieces or 'wking' not in current_pieces: return False for c_p in current_pieces: #checking if the pieces' names are valid if c_p not in all_pieces: return False if current_pieces.count('wpawn') > 8: return False if current_pieces.count('bpawn') > 8: return False # Count the white pieces if len([p for p in current_pieces if p.startswith('w')]) > 16: return False # Count the black pieces if len([p for p in current_pieces if p.startswith('b')]) > 16: return False return True 
def isValidPieceSet(current_pieces): #create a dictionary with all the pieces of each color as the key # and the valid starting number of the pieces as value pieces = ['pawn', 'knight', 'bishop', 'rook', 'queen', 'king'] colors = ['w', 'b'] all_pieces = [] for color in colors: for p in pieces: all_pieces.append(color + p) #accepts a list, checking if the pieces of the chessboard are valid if 'bking' not in current_pieces or 'wking' not in current_pieces: return False for c_p in current_pieces: #checking if the pieces' names are valid if c_p not in all_pieces: return False if current_pieces.count('wpawn') > 8: return False if current_pieces.count('bpawn') > 8: return False # Count the white pieces if len([p for p in current_pieces if p.startswith('w')]) > 16: return False # Count the black pieces if len([p for p in current_pieces if p.startswith('b')]) > 16: return False return True def isValidChessboard(chessboard): # function accept a dict for position in chessboard: #checking coordinates of the square if len(position) != 2: return False elif int(position[0]) not in range(1, 9) or position[1] not in 'abcdefgh': return False current_pieces = chessboard.values() return isValidPieceSet(current_pieces): #checking the pieces itself if not isValidChessboard(chessboard): print("Your board is invalid") else: print("Your board is valid") 
Grammar
Source Link
Mark H
  • 2.4k
  • 14
  • 19

The task description and this program both allow illegal board positions to be counted as valid: boards with 10 white queens, boards with pawns on the first and eighth ranks, boards with kings attacking each other, and many more. The reason for this is the problem of determining whether an arbitrary board position is legal is very hard. Such a program would have to implement all the rules of chess and then find a sequence of moves from that results in the input board. Such a search could take literal millions of years. So, since this task is meant to be programming practice, the scope of the checks is restricted to what is easily programmable.

The task description and this program both allow illegal board positions to be counted as valid: boards with 10 white queens, boards with pawns on the first and eighth ranks, boards with kings attacking each other, and many more. The reason for this is the problem of determining whether an arbitrary board position is legal is very hard. Such a program would have to implement all the rules of chess and then find a sequence of moves from that results in the input board. Such a search could take literal millions of years. So, since this task is meant to be programming practice, the scope of the checks is restricted to what is easily programmable.

The task description and this program both allow illegal board positions to be counted as valid: boards with 10 white queens, boards with pawns on the first and eighth ranks, boards with kings attacking each other, and many more. The reason for this is the problem of determining whether an arbitrary board position is legal is very hard. Such a program would have to implement all the rules of chess and then find a sequence of moves that results in the input board. Such a search could take literal millions of years. So, since this task is meant to be programming practice, the scope of the checks is restricted to what is easily programmable.

added 676 characters in body
Source Link
Mark H
  • 2.4k
  • 14
  • 19
Loading
Source Link
Mark H
  • 2.4k
  • 14
  • 19
Loading