Check out Janggi (Korean Chess), our featured variant for December, 2024.


[ Help | Earliest Comments | Latest Comments ]
[ List All Subjects of Discussion | Create New Subject of Discussion ]
[ List Earliest Comments Only For Pages | Games | Rated Pages | Rated Games | Subjects of Discussion ]

Single Comment

GAME code table-driven move generator[Subject Thread] [Add Response]
H. G. Muller wrote on Thu, Aug 13, 2020 04:56 AM UTC in reply to Fergus Duniho from 01:46 AM:

What would help me out is having access to a game with optional deletions or freedrops. This would help me make sure the code is working properly for those.

I made two such games (also for testing purposes). Chu-Shogi Lions can optionally capture a piece they jump over, and I used one in Mighty Lion Chess. And in Sandbox I used a piece (incidentally also depicted as a Lion) that (next to being a range-3 Queen) can jump as an Alfil, and then optionally take the piece it jumps over. And the preset for Odin's Rune Chess I just made features a Knight that can optionally take out a piece adjacent to its destination (Forest Ox), and a Queen (Valkyrie) that causes a drop of a friendly piece it captures. (Not optional, but often on multiple possible locations).

These all highlight (pseudo-)legal moves. The executable code for all of those is in the included file betza.txt. I have changed it now to write moves with side effects in the $extralegal array, in the move generation before the move. (I didn't touch it for continuation moves.)

Something completely different:

What would you consider a reasonable execution time for the GAME code in a preset? I tried the stopwatch trick to measure that for my code, and the results were a bit discouraging. I tried full legality checking of highlighted moves, and in the Mighty Lion preset the execution time quickly went up to 4 sec! That seems prohibitively long. If I stick to highlighting pseudo-legal moves the typical time was about 0.4 sec.

One of the problems is that the Lion is a complex piece, which has 88 potential moves: 24 direct leaps, and 64 combinations of a capturing and continuation King step. Unrolling of the loops over directions backfires a bit here, because the 64 two-leg moves have 8 groups of 8 that start with the same capture, and only differ in the second leg. If there is nothing to capture on that square, it is still having to discover this 8 times. More typical multi-leg moves have just one or two continuations, and there unrolling the directional part is a big time saver over having to run some complex algorithm for determining which directions you can continue in. Perhaps I should encode multi-leg moves as a tree rather than a list. Or add some 'skip number' to the list for each leg, that can be used to tell the interpreter "if this leg fails, don't bother to try the next N moves in the list, because these use the same legs up to here". That would reduce attempting the 64 Lion locust captures to 8 tries for the first leg, if there is nothing to capture.

It would of course still all be a waste of time to generate these moves in order to test if they happen to capture a King when it was known beforehand that the King was outside the Lion's range, because the distance was more than 2. A preliminaty test on the footprint of the piece would in most cases relieve you of the task of trying any of the 88 Lion moves in the check test.

A more fundamental problem is that, with fully-legal highlighting, the check test is re-run on every move from scratch, generating exactly the same moves for almost all pieces every time, as the move they want to check the legality of did not have any consequences for them. A much more efficient algorithm would be to run the move generator once for the opponent, keeping track for each attempted move which squares it passed through or attempted to pass through, and for which it would have made a difference what was on that square. (If the leg of the move is both a hop and a non-riding non-capture, (un)occupying the square would not affect the possibility to make it.) To test the legality of a given move you would then only have to try the opponent moves listed for the squares the given move mutated. With only simple riding an leaping moves, this would amount to recording all slider hits (that still have range left) on opponent pieces (that then could potentially be pinned). And when there is a pre-existing attack on the King, recording all empty squares this attack passes through. Moves of pieces that were attacked by a slider would then require re-running of that single slider move that attacked them. And a pre-existing checking move would have to be re-run. (And if the move under test had not landed on one of the squares that checking move passed through, you would not even have to bother re-running it, because it will not be affected, and still check you.) If the re-run opponent move does not capture your King (or, even more frequently, if there is nothing to re-run) the move can be declared legal.

That would take care of all moves except those of the King itsef. To handle those the initial generation of opponent moves could be done without your own King present, and every capture-capable move to an empty square should mark that empty square as being under attack. King moves to a thus marked square are then illegal. Then the entire operation of legality testing all moves would require only a single run of the move generator, with an occasional re-run of a single slider move that hit upon a piece that moved away.