Check out Grant Acedrex, our featured variant for April, 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

The Fairychess Include File Tutorial. How to use the fairychess include file to program games for Game Courier.[All Comments] [Add Comment or Rating]
🕸📝Fergus Duniho wrote on Tue, Jan 10, 2023 06:55 PM UTC:

I just modified the stalemated subroutine to list each Pawn promotion as a separate legal move. This will affect how Pawn promotions are handled. Instead of loading another page with an image of each piece, it will pop up a requester asking you which move you want to make. To accommodate this, the promotion move identifies the space by its coordinate and not by the keyword dest.

To make use of this for promotable pieces besides Pawns, you need to set the value of promotable to a list of your promotable pieces. If you don't, your code should simply work as it has previously worked before, still going to another page to ask what to promote the piece to.

I have done this for the sake of getting a complete list of legal moves for the purpose of using it with code for finding the solutions to mate-in-one problems. In time, I hope to do mate-in-two problems as well, but I have to start with what is simpler.

For now, the stalemated subroutine does not check whether each promotion is separately legal. This could matter if you had Korean Cannons in your game, because this piece captures by hopping over another piece but cannot hop over another Korean Cannon. In that case, what you promoted to could affect whether a promotion would put your King in check. But there is no possibility of this making a difference in Chess and most Chess variants, in which any possible promotion is equally legal. To avoid complication, the simpler scenario is assumed.

Here is the new subroutine:

// Goes through all possible moves, putting all legal moves into the array $legalmoves
// Returns false if any legal moves are found, and returns true if none are found.

sub stalemated kingpos:
    store;
    local cspaces friend friends from piece to movetype np prom;

    set movetype MOVE;

    if hasupper space #kingpos:
        // Array of pieces on same side
        set friends lambda (onlyupper);
        // Indicates whether a space is free to move to
        set free lambda (haslower #0 or not hasupper #0);
        set pzone lambda (not onboard where #0 0 var pzs);
        set cspaces var wcastle;
        set prom var wprom;
    else:
        set friends lambda (onlylower);
        set free lambda (hasupper #0 or not haslower #0);
        set pzone lambda (not onboard where #0 0 neg var pzs);
        set cspaces var bcastle;
        set prom var bprom;
    endif;

    // While the royal piece is called the King in these comments,
    // it may be any piece. These variables determine what the royal piece is.
    set royal space var kingpos;

    store;

 // Can any piece legally move?
    for (from piece) fn #friends:
        for to fn join const alias #piece "-Range" #from:
            if fn const alias #piece #from #to and fn #free alias space #to and onboard #to:
                move #from #to;
                if not sub checked cond == #from #kingpos #to #kingpos:
                    if fn #pzone #to and match #piece #promotable:
                        for np var prom:
                            setlegal "{#piece} {#from}-{#to}; {#np}-{#to}";
                        next;
                    else:
                        setlegal #from #to;
                    endif;
                endif;
            endif;
            restore;
        next;
    next;

    // Castling moves are handled separately
    if > count var cspaces 0:
        for to var cspaces:
            if sub castlepos #kingpos #to:
                setlegal #kingpos #to;
            endif;
        next;
    endif;

    setsystem autorules sub describe_rules;

    // All done. Set $legalmoves and return;
    return cond count system legalmoves false true;
endsub;