On 4/19/2016 1:02 AM, Michael Selik wrote:

Why relocate rather than remove? What message would you provide that's
better than ``KeyError: 42`` with a traceback that shows exactly which
dictionary is being used and how?

I think you misread my code. No dictionary exception occurs in the sanity checks. Below is the full function with the revised sanity check for positions that compares the input list with the two valid lists of board positions.


def generate_set(color, positions):

    if positions not in [VARS['COORDINATES'][VARS['BOARD_BOTTOM']],
                         VARS['COORDINATES'][VARS['BOARD_TOP']]]:
raise Exception("List for positions contains no valid coordinates, "
                        "got {} instead.".format(positions))

    # generate objects according to color and position
    for position in positions:
        rank, file = position
        if rank in VARS['RANK_NOBILITY']:
            if file in VARS['FILE_ROOK']:
                yield Rook(color, position)
            elif file in VARS['FILE_BISHOP']:
                yield Bishop(color, position)
            elif file in VARS['FILE_KNIGHT']:
                yield Knight(color, position)
            elif file is VARS['FILE_QUEEN']:
                yield Queen(color, position)
            elif file is VARS['FILE_KING']:
                yield King(color, position)
        else:
            yield Pawn(color, position)


I meant, what goes wrong if the number of positions input is other than
16? Without these "sanity" checks, your functions might be reusable in,
say, a checkers game.

Chess has 16 pieces (six types) on each side that are set up on the board in a particular order. Checkers has 12 pieces (one type) on each side that are set up on alternating squares in no particular order. Since I'm writing a chess engine because it has endless supply of programming changes, I have no desire to write reusable code for two similar but different games at the same time.

Thanks,

Chris R.

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to