🕸💡📝Fergus Duniho wrote on Sun, May 20, 2018 12:45 PM UTC:
I just fixed a bug in the code. A Queen is not supposed to be able to pass through check, but the way the code was enforcing this rule, it was ignoring checks from a piece captured by the Queen. This is because it was evaluating the legality of the Queen's move after the Queen had moved, and at that point, the piece the Queen had captured was no longer on the board. Here is what the code originally looked like:
// This is for evaluating actual moves by the Queen
sub Q from to:
if not fn Q var from var to:
die Your Queen may not move from #from to #to;
endif;
store;
for pt path #from #to:
move #to #pt;
if sub checked var pt and != var pt var from and != var pt var to:
die You may not move your Queen through check.;
endif;
restore;
next;
return true;
endsub;
It first checks whether it is a legal Queen move, and if it is not, it is illegal. That much is good. It then stores the current position and starts looping through the path between its origin and its destination. That much is also good. The problem is that it is working with the position after the Queen has moved. Note that inside the loop, it moves the Queen from #to to #pt. It should not be doing this. Instead, the position should have been restored to what it was before the Queen moved, and each iteration through the loop should move the Queen from its original space to the space along its path that the loop has reached. To fix this, I added "store before" to the Pre-Move code for each side, and I rewrote this subroutine to look like this:
// This is for evaluating actual moves by the Queen
sub Q from to:
my pt;
if not fn Q var from var to:
die Your Queen may not move from #from to #to;
endif;
store;
for pt path #from #to:
restore before;
move #from #pt;
if sub checked var pt:
die You may not move your Queen through check.;
endif;
next;
restore;
return true;
endsub;
Note that I also deleted the "and != var pt var from and != var pt var to" part from one of the lines. This was making sure that the position indicated was not the Queen's original space or its destination, but these positions were not actually included in the path anyway, as the path function just returns the shortest path between two spaces. When the two spaces are adjacent, it returns no positions. Apart from that, the main change was to distinguish between "restore before" and just "restore". The former would restore the position before the Queen moved, and the latter would restore the position to what it was after the Queen moved.
I just fixed a bug in the code. A Queen is not supposed to be able to pass through check, but the way the code was enforcing this rule, it was ignoring checks from a piece captured by the Queen. This is because it was evaluating the legality of the Queen's move after the Queen had moved, and at that point, the piece the Queen had captured was no longer on the board. Here is what the code originally looked like:
It first checks whether it is a legal Queen move, and if it is not, it is illegal. That much is good. It then stores the current position and starts looping through the path between its origin and its destination. That much is also good. The problem is that it is working with the position after the Queen has moved. Note that inside the loop, it moves the Queen from #to to #pt. It should not be doing this. Instead, the position should have been restored to what it was before the Queen moved, and each iteration through the loop should move the Queen from its original space to the space along its path that the loop has reached. To fix this, I added "store before" to the Pre-Move code for each side, and I rewrote this subroutine to look like this:
Note that I also deleted the "and != var pt var from and != var pt var to" part from one of the lines. This was making sure that the position indicated was not the Queen's original space or its destination, but these positions were not actually included in the path anyway, as the path function just returns the shortest path between two spaces. When the two spaces are adjacent, it returns no positions. Apart from that, the main change was to distinguish between "restore before" and just "restore". The former would restore the position before the Queen moved, and the latter would restore the position to what it was after the Queen moved.