It seems you are trading simplicity for speed, here. And I wonder if that is consistent with your goal for this project. If you would be going for speed you would eventually end up with something like I discussed in the 'Mailbox Trials' threads.
Remember a 'view distance' table that gives you the distance to the board edge will have to be initialized, which will require extra code. (Of course only executed at startup, so no impact on speed. But it still means extra code complexity.) Having a larger board array with edge guards is comparatively simple. And if you use clever piece encoding you can make it such that testing for edge guards and friendly pieces can be done in the same test. You still need a move table, but this is quite small. And must be initialized data anyway, because it defines the game rules. Something like
Code: Select all
typedef struct {
char step, range, capability;
} MoveDescriptor;
MoveDescriptor moveTable[] = {
{16,1,MOVE_ONLY}, {15,1,CAPTURE_ONLY}, {17,1,CAPTURE_ONLY}, {0,0,0}, // white Pawn
{-16,1,MOVE_ONLY}, {-15,1,CAPTURE_ONLY}, {-17,1,CAPTURE_ONLY}, {0,0,0}, // black Pawn
{14,1,0}, {18,1,0}, {-14,1,0}, {-18,1,0}, {31,1,0}, {33,1,0}, {-31,1,0}, {-33,1,0}, {0,0,0}, // Knight
{1,0,0}, {-1,0,0}, {16,0,0}, {-16,0,0}, {0,0,0}, // Rook
{1,0,0}, {-1,0,0}, {16,0,0}, {-16,0,0}, {15,0,0}, {17,0,0}, {-15,0,0}, {-17,0,0}, {0,0,0}, // Queen and Bishop
{1,1,0}, {-1,1,0}, {16,1,0}, {-16,1,0}, {15,1,0}, {17,1,0}, {-15,1,0}, {-17,1,0}, {0,0,0}, // King
};
int firstDirection[] = {3, 1, 4, 8, 26, 17, 22, 31 };
BTW, if you really want to go for view distances it might be better to go for a dynamic view-distance table, which does not hold the distance to the edge, but to the nearest obstacle. Whether that is edge or piece. This has the advantage that you can very easily make a capture-only generator: for slider you would just look up where the potential target is, without having to step through all empty squares. It is comparatively easy to incrementally update such a table.