First post, very new to programming. This is my solution for the chapter 5 project of Automate the Boring Stuff and I wondered if I am making any mistakes and is there a way to make it more efficient using what the book has taught thus far.
The project description:
In this chapter, we used the dictionary value
{'1h': 'bking', '6c': 'wqueen', '2g': 'bbishop', '5h': 'bqueen', '3e': 'wking'}
to represent a chess board. Write a function namedisValidChessBoard()
that takes a dictionary argument and returnsTrue
orFalse
depending on if the board is valid. A valid board will have exactly one black king and exactly one white king. Each player can only have at most 16 pieces, at most 8 pawns, and all pieces must be on a valid space from'1a'
to'8h'
; that is, a piece can’t be on space'9z'
. The piece names begin with either a'w'
or'b'
to represent white or black, followed by'pawn'
,'knight'
,'bishop'
,'rook'
,'queen'
, or'king'
. This function should detect when a bug has resulted in an improper chess board.
My code:
MyBoard = {'a8': 'bR', 'b8': 'bN', 'c8': 'bB', 'd8': 'bK', 'e8': 'bQ', 'f8': 'bB', 'g8': 'bN', 'h8': 'bR',
'a7': 'bp', 'b7': 'bp', 'c7': 'bp', 'd7': 'bp', 'e7': 'bp', 'f7': 'bp', 'g7': 'bp', 'h7': 'bp',
'a6': '', 'b6': '', 'c6': '', 'd6': '', 'e6': '', 'f6': '', 'g6': '', 'h6': '',
'a5': '', 'b5': '', 'c5': '', 'd5': '', 'e5': '', 'f5': '', 'g5': '', 'h5': '',
'a4': '', 'b4': '', 'c4': '', 'd4': '', 'e4': '', 'f4': '', 'g4': '', 'h4': '',
'a3': '', 'b3': '', 'c3': '', 'd3': '', 'e3': '', 'f3': '', 'g3': '', 'h3': '',
'a2': 'wp', 'b2': 'wp', 'c2': 'wp', 'd2': 'wp', 'e2': 'wp', 'f2': 'wp', 'g2': 'wp', 'h2': 'wp',
'a1': 'wR', 'b1': 'wN', 'c1': 'wB', 'd1': 'wQ', 'e1': 'wK', 'f1': 'wB', 'g1': 'wN', 'h1': 'wR'}
def isValidChessBoard(board):
valid_count = True
valid_position = True
valid_board = True
piece_count = {}
black_pieces = 0
white_pieces = 0
board_pieces = list(board.values())
position = list(board.keys())
position_message = 'Correct position.'
piececount_message = ''
message = ''
#Counting blacks and whites
for i in board_pieces:
if i == '':
pass
elif i[0] == 'b':
black_pieces += 1
elif i[0] == 'w':
white_pieces += 1
piececount_message = 'There are ' + str(white_pieces) + ' white pieces and ' + str(black_pieces) + ' black pieces.'
#Checking valid position
for p in position:
position_str = int(p[1])
if position_str > 8 or position_str < 1:
valid_position = False
position_message = 'Invalid position.'
#Counting the number of each piece and setting default for empty spaces
for v in MyBoard.values():
piece_count.setdefault(v, 0)
piece_count[v] += 1
print(piece_count)
#Checking King numbers
if piece_count.get('wK', 0) > 1 or piece_count.get('bK', 0) > 1\
or piece_count.get('wK', 0) < 1 or piece_count.get('bK', 0) < 1:
valid_count = False
message = 'King count error.'
#Checking Queen numbers
elif piece_count.get('wQ', 0) > 1 or piece_count.get('bQ', 0) > 1\
or piece_count.get('wQ', 0) < 1 or piece_count.get('bQ', 0) < 1:
valid_count = False
message = 'Queen count Error.'
#Checking Bishop numbers
elif piece_count.get('wB', 0) > 2 or piece_count.get('bB', 0) > 2\
or piece_count.get('wB', 0) < 2 or piece_count.get('bB', 0) < 2:
valid_count = False
message = 'Bishop count error.'
#Checking Knight numbers
elif piece_count.get('wN', 0) > 2 or piece_count.get('bN', 0) > 2\
or piece_count.get('wN', 0) < 2 or piece_count.get('bN', 0) < 2:
valid_count = False
message = 'Knight count error.'
#Checking Rook numbers
elif piece_count.get('wR', 0) > 2 or piece_count.get('bR', 0) > 2\
or piece_count.get('wR', 0) < 2 or piece_count.get('bR', 0) < 2:
valid_count = False
message = 'Rook count error.'
#Checking Pawn numbers
elif piece_count.get('wp', 0) > 8 or piece_count.get('bp', 0) > 8\
or piece_count.get('wp', 0) < 8 or piece_count.get('bp', 0) < 8:
valid_count = False
message = 'Pawn count error.'
else:
message = 'Correct number of pieces on the board.'
valid_board = valid_count and valid_position
print(piececount_message)
print(position_message)
print(message)
print('Board Valid: ' + str(valid_board) + '.')
isValidChessBoard(MyBoard)