Check out Alice Chess, our featured variant for June, 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 Courier. PHP script for playing Chess variants online.[All Comments] [Add Comment or Rating]
🕸💡📝Fergus Duniho wrote on Sun, May 26 01:25 AM UTC in reply to Aurelian Florea from Sat May 25 06:47 PM:

I am still confused. How do I execute the postgame handler (whiteFirstTurn) for a second time?

See the Post-Move code for Musketeer Chess to get some idea of how it can be done.

Each begins with a series of if and elseif conditions to check the value of thismove or movenum. Post-Game 2 first checks whether thismove is null, because at the very beginning of the game, it is effectively at the end of the second player's turn. This is because each subsequent time it is the first player's turn to move, it will be after the second player has moved, which is the normal time to run Post-Game 2. This means that the first Post-Game section to run will be the second, not the first.

However, Musketeer Chess does things in a more complicated way than your game. Before it lets you place both extra pieces on the same move, it has each player select one of the pieces. First White selects one, and Black gets the same piece, and then Black selects one and White gets the same piece. After this stage, it moves the selected pieces to the center of the board, and each player gets to place them. First White moves each piece, and then Black does.

So we need to jump ahead to this stage to see what is being done. For White, this is done with this code in Post-Game 2:

elseif == movenum 2: // White's 2nd move
  if not empty d4 and not empty e4:
    setlegal d4 a0 b0 c0 d0 e0 f0 g0 h0;
    setlegal e4 a0 b0 c0 d0 e0 f0 g0 h0;
    end;
  else:
    if not empty e!9:
      set legalto (b9 c9 d9 f9 g9);
      remind "Since you placed your first piece behind your King, the second may not go behind a Rook.";
    elseif not empty a!9:
      set legalto (b9 c9 d9 f9 g9 h9);
      remind "Since you placed your first piece behind a Rook, the second may not go behind your King.";
    elseif not empty h!9:
      set legalto (a9 b9 c9 d9 f9 g9);
      remind "Since you placed your first piece behind a Rook, the second may not go behind your King.";
    else:
      set legalto (a9 b9 c9 d9 e9 f9 g9 h9);
    endif;
    set f cond empty d5 e5 d5;
    foreach c var legalto:
      if empty realname #c:
        setlegal #f #c;
      endif;
    next;
    end;
  endif;

If White has not moved either piece, it sets legal moves for White. Otherwise, it sets legal moves for Black. But why does it work this way? Post-Move 2 normally runs after a move by the second player. If no moves have been made, this is normally the start of the first player's turn, and it will calculate legal moves for the first player. But when a move has already been made, this is a continuation of a move by the second player. In that case, it needs to calculate the moves for the second player.

When handling gating on the first move instead of the second, code like this may be more appropriate:

elseif == thismove null: // White's ist move
  setlegal d4 a0 b0 c0 d0 e0 f0 g0 h0;
  setlegal e4 a0 b0 c0 d0 e0 f0 g0 h0;
  end;
elseif == movenum 1: // Second part of Black's move
    if not empty e!9:
      set legalto (b9 c9 d9 f9 g9);
      remind "Since you placed your first piece behind your King, the second may not go behind a Rook.";
    elseif not empty a!9:
      set legalto (b9 c9 d9 f9 g9 h9);
      remind "Since you placed your first piece behind a Rook, the second may not go behind your King.";
    elseif not empty h!9:
      set legalto (a9 b9 c9 d9 f9 g9);
      remind "Since you placed your first piece behind a Rook, the second may not go behind your King.";
    else:
      set legalto (a9 b9 c9 d9 e9 f9 g9 h9);
    endif;
    set f cond empty d5 e5 d5;
    foreach c var legalto:
      if empty realname #c:
        setlegal #f #c;
      endif;
    next;
    end;
  endif;

Note that this is just a modification of the Musketeer Chess code, which has not been finetuned for your game, and which has not been tested.

Accordingly, the second part of White's turn is handled in Post-Game 1 with this code:

elseif == movenum 2: // Second part of White's Second Move
  set f cond empty d4 e4 d4;
  if not empty e!0: 
    setlegal #f b0 c0 d0 f0 g0;
    remind "Since you placed your first piece behind your King, the second may not go behind a Rook.";
  elseif not empty a!0:
    setlegal #f b0 c0 d0 f0 g0 h0;
    remind "Since you placed your first piece behind a Rook, the second may not go behind your King.";
  elseif not empty h!0:
    setlegal #f a0 b0 c0 d0 f0 g0;
    remind "Since you placed your first piece behind a Rook, the second may not go behind your King.";
  else:
    remind "Place your piece on" #f "behind your back rank.";
    foreach x range a h:
      set t join #x !0;
      if empty #t:
        set t join #x 0;
        setlegal #f #t;
      endif;
    next;
  endif;
  end;

Unlike the section that dealt with the second part of Black's move, this just calculates legal moves for White. Note that the movenum value it checks for is the same in each case. For White, it is the same move whether at its start or continuing it.

Since we already saw how Black's move is continued, let's look at how it begins. It begins with this code in Post-Game 1:

elseif == movenum 3: // Black's Second Move
  setlegal d5 a9 b9 c9 d9 e9 f9 g9 h9;
  setlegal e5 a9 b9 c9 d9 e9 f9 g9 h9;
  end;

I'm not sure why it would begin on a later move number than it would continue on. Maybe I included some extraneous code that doesn't do anything. Looking at Post-Game 2, I see this code for continuing Black's move:

elseif == movenum 3: // Second Part of Black's Second Turn
  set f cond empty d5 e5 d5;
  if not empty e!9: 
    setlegal #f b9 c9 d9 f9 g9;
    remind "Since you placed your first piece behind your King, the second may not go behind a Rook.";
  elseif not empty a!9:
    setlegal #f b9 c9 d9 f9 g9 h9;
    remind "Since you placed your first piece behind a Rook, the second may not go behind your King.";
  elseif not empty h!9:
    setlegal #f a9 b9 c9 d9 f9 g9;
    remind "Since you placed your first piece behind a Rook, the second may not go behind your King.";
  else:
    remind "Place your piece on" #f "behind your back rank.";
    foreach x range a h:
      set t join #x !9;
      if empty #t:
        set t join #x 9;
        setlegal #f #t;
      endif;
    next;
  endif;
  end;

This is mostly the same as the code for Black found with "White's 2nd Move". I'll do some tests later to see if I can remove the other code.

For some reason, I have code for continuing a partial move only in Post-Game 1:

elseif == movenum 4: // White
  if not empty e5 or not empty d5:
    continuemove;
  endif;

Investigation reveals that I am also using continuemove in the Post-Game sections. So things are just getting more complicated. White's placing move is handled with this code in Post-Game 1:

elseif == turn 2: 
  restore premove;
  set mvs explode chr 59 thismove;
  eval join "MOVE: " trim elem 0 mvs;
  if and != origin d4 != origin e4 or != rankname dest !0:
    die All you may do right now is place an extra piece behind your back rank.;
  endif;
  if > count var mvs 1:
    eval join "MOVE: " trim elem 1 mvs;
    if and != origin d4 != origin e4 or != rankname dest !0:
      die All you may do right now is place an extra piece behind your back rank.;
    elseif not empty a!0 or not empty h!0 and not empty e!0:
      die You may not place your pieces behind a King and a Rook.;
    endif;
    foreach f range a h:
      set c join #f "!0";
      if empty #c:
        delete #c;
      endif;
    next;
  remind "Now that you have placed your extra pieces behind your back rank, the remaining spaces behind it have been deleted.";
  else:
    continuemove;
  endif;
  return;
endif;

Note that it is restoring the position, parsing thismove into separate moves, executing each one individually, and testing it for legality. It does not evaluate either move until both have been made. If only one has been made, it uses continuemove.

In a similar manner, Black uses this code to evaluate the placing move in Post-Game 2:

elseif == turn 2:
  restore premove;
  set mvs explode chr 59 thismove;
  eval join "MOVE: " trim elem 0 mvs;
  if and != origin d5 != origin e5 or != rankname dest !9:
    die All you may do right now is place an extra piece behind your back rank.;
  endif;
  if > count var mvs 1:
    eval join "MOVE: " trim elem 1 mvs;
    if and != origin d5 != origin e5 or != rankname dest !9:
      die All you may do right now is place an extra piece behind your back rank.;
    endif;
    foreach f range a h:
      set c join #f "!9";
      if empty #c:
        delete #c;
      endif;
    next;
    setsystem originalpieces piececount;
    remind "Now that you have placed your extra pieces behind your back rank, the remaining spaces behind it have been deleted.";
  else:
    continuemove;
  endif;
  return;
endif;