home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / krieg.icn < prev    next >
Text File  |  2000-07-29  |  37KB  |  1,225 lines

  1. ############################################################################
  2. #
  3. #    File:     krieg.icn
  4. #
  5. #    Subject:  Program to play kriegspiel
  6. #
  7. #    Author:   David J. Slate
  8. #
  9. #    Date:     August 14, 1996
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #   Kriegspiel (German for "war game") implements a monitor and, if desired,
  18. #   an automatic opponent for a variation of the game of chess which has the
  19. #   same rules and goal as ordinary chess except that neither player sees
  20. #   the other's moves or pieces.  Thus Kriegspiel combines the intricacies
  21. #   and flavor of chess with additional elements of uncertainty, psychology,
  22. #   subterfuge, etc., which characterize games of imperfect information such
  23. #   as bridge or poker.
  24. #
  25. ############################################################################
  26. #   
  27. #   The version of the game implemented here was learned by the author
  28. #   informally many years ago.  There may be other variations, and perhaps
  29. #   the rules are actually written down somewhere in some book of games.
  30. #   
  31. #   The game is usually played in a room with three chess boards set up on
  32. #   separate tables.  The players sit at the two end tables facing away from
  33. #   each other.  A third participant, the "monitor", acts as a referee and
  34. #   scorekeeper and keeps track of the actual game on the middle board,
  35. #   which is also out of sight of either player.  Since each player knows
  36. #   only his own moves, he can only guess the position of the enemy pieces,
  37. #   so he may place and move these pieces on his board wherever he likes.
  38. #   
  39. #   To start the game, the "White" player makes a move on his board.  If the
  40. #   move is legal, the monitor plays it on his board and invites "Black" to
  41. #   make his response.  If a move attempt is illegal (because it leaves the
  42. #   king in check or tries to move through an enemy piece, etc.), the
  43. #   monitor announces that fact to both players and the moving player must
  44. #   try again until he finds a legal move.  Thus the game continues until it
  45. #   ends by checkmate, draw, or agreement by the players.  Usually the
  46. #   monitor keeps a record of the moves so that the players can play the
  47. #   game over at its conclusion and see what actually happened, which is
  48. #   often quite amusing.
  49. #   
  50. #   With no additional information provided by the monitor, the game is very
  51. #   difficult but, surprisingly, still playable, with viable tactical and
  52. #   strategic ideas.  Usually, however, the monitor gives some minimal
  53. #   feedback to both players about certain events.  The locations of
  54. #   captures are announced as well as the directions from which checks on
  55. #   the kings originate.
  56. #   
  57. #   Even with the feedback about checks and captures, a newcomer to
  58. #   Kriegspiel might still think that the players have so little information
  59. #   that they could do little more than shuffle around randomly hoping to
  60. #   accidentally capture enemy pieces or checkmate the enemy king.  But in
  61. #   fact a skilled player can infer a lot about his opponent's position and
  62. #   put together plans with a good chance of success.  Once he achieves a
  63. #   substantial material and positional advantage, with proper technique he
  64. #   can usually exploit it by mopping up the enemy pieces, promoting pawns,
  65. #   and finally checkmating the enemy king as he would in an ordinary chess
  66. #   game.  In the author's experience, a skilled Kriegspiel player will win
  67. #   most games against a novice, even if both players are equally matched at
  68. #   regular chess.
  69. #
  70. ############################################################################
  71. #   
  72. #   The implementation:
  73. #   
  74. #   The functions of this program are to replace the human monitor, whose
  75. #   job is actually fairly difficult to do without mistakes, to permit the
  76. #   players to play from widely separate locations, to produce a machine-
  77. #   readable record of the game, and to provide, if desired, a computer
  78. #   opponent for a single player to practice and spar with.
  79. #   
  80. #   When two humans play, each logs in to the same computer from a separate
  81. #   terminal and executes his own copy of the program.  This requires a
  82. #   multi-tasking, multi-user operating system.  For various reasons, the
  83. #   author chose to implement Kriegspiel under UNIX, using named pipes for
  84. #   inter-process communication.  The program has been tested successfully
  85. #   under Icon Version 7.5 on a DecStation 3100 running Ultrix (a Berkeley-
  86. #   style UNIX) and also under Icon Version 7.0 on the ATT UNIX-PC and
  87. #   another System V machine, but unanticipated problems could be
  88. #   encountered by the installer on other computers.  An ambitious user may
  89. #   be able to port the program to non-UNIX systems such as Vax-VMS.  It may
  90. #   also be possible to implement Kriegspiel on a non-multi-tasking system
  91. #   such as MS-DOS by using separate computers linked via serial port or
  92. #   other network.  See the "init" procedure for much of the system-
  93. #   dependent code for getting user name, setting up communication files,
  94. #   etc.
  95. #   
  96. #   Two prospective opponents should agree on who is to play "white", make
  97. #   sure they know each other's names, and then execute Kriegspiel from
  98. #   their respective terminals.  The program will prompt each player for his
  99. #   name (which defaults to his user or login name), his piece color, the
  100. #   name of his opponent, whether he wishes to play in "totally blind" mode
  101. #   (no capture or check information - not recommended for beginners), and
  102. #   the name of the log file on which the program will leave a record of the
  103. #   game (the program supplies a default in /tmp).  Each program will set up
  104. #   some communication files and wait for the opponent's to show up.  Once
  105. #   communication is established, each player will be prompted for moves and
  106. #   given information as appropriate.  The online "help" facility documents
  107. #   various additional commands and responses.
  108. #   
  109. #   A player who wants a computer opponent should select "auto" as his
  110. #   opponent's name.  Play then proceeds as with a human opponent.  "Auto"
  111. #   is currently not very strong, but probably requires more than novice
  112. #   skill to defeat.
  113. #
  114. ############################################################################
  115. #
  116. #   Known bugs and limitations:
  117. #
  118. #   No bugs are currently known in the areas of legal move generation,
  119. #   board position updating, checkmate detection, etc., but it is still
  120. #   possible that there are a few.
  121. #
  122. #   Some cases of insufficient checkmating material on both sides are
  123. #   not detected as draws by the program.
  124. #
  125. #   In the current implementation, a player may not play two
  126. #   simultaneous games under the same user name with the same piece color.
  127. #
  128. #   If the program is terminated abnormally it may leave a communication
  129. #   pipe file in /tmp.
  130. #
  131. ############################################################################
  132.  
  133.  
  134. record board( pcs, cmv, cnm, caswq, caswk, casbq, casbk, fepp, ply)
  135.  
  136. global    Me, Yu, Mycname, Yrcname, Mycomm, Yrcomm, Logname, Logfile,
  137.     Mycol, Yrcol, Blind, Bg, Frinclst, Lmv, Any, Tries, Remind
  138.  
  139.  
  140. procedure automov( )
  141.  
  142. #   Returns a pseudo-randomly selected move type-in to be used in
  143. #   "auto opponent" mode.  But if possible, try to recapture (unless in
  144. #   blind mode):
  145.  
  146.     local    m, ms
  147.     static    anyflag
  148.  
  149.     initial    anyflag := 0
  150.  
  151.     if anyflag = 0 then {
  152.     anyflag := 1
  153.     return "any"
  154.     }
  155.     anyflag := 0
  156.  
  157.     ms := set( )
  158.     every insert( ms, movgen( Bg))
  159.  
  160.     if / Any then {
  161.     if find( ":", \ Lmv) & not find( "ep", \ Lmv) & / Blind then {
  162.         every m := ! ms do {
  163.         if m[ 4:6] == Lmv[ 4:6]  & movlegal( Bg, m) then
  164.             return m[ 2:6] || "Q"
  165.         }
  166.         }
  167.     while * ms ~= 0 do {
  168.         if movlegal( Bg, m := ? ms) then
  169.         return m[ 2:6] || "Q"
  170.         delete( ms, m)
  171.         }
  172.     return "end"
  173.     }
  174.     else {
  175.     every m := ! ms do {
  176.         if m[ 1] == "P" & m[ 6] == ":" & movlegal( Bg, m) then
  177.         return m[ 2:6] || "Q"
  178.         }
  179.     return "end"
  180.     }
  181. end
  182.  
  183.  
  184. procedure chksqrs( b)
  185.  
  186. #   Generates the set of squares of pieces giving check in board b;
  187. #   fails if moving side's king not in check:
  188.  
  189.     local    sk
  190.  
  191.     sk := find( pc2p( "K", b.cmv), b.pcs)
  192.     suspend sqratks( b.pcs, sk, b.cnm)
  193. end
  194.  
  195.  
  196. procedure fr2s( file, rank)
  197.  
  198. #   Returns the square number corresponding to "file" and "rank"
  199. #   numbers; fails if invalid file and/or rank:
  200.  
  201.     return (0 < (9 > file)) + 8 * (0 < ( 9 > rank)) - 8
  202. end
  203.  
  204.  
  205. procedure gamend( b)
  206.  
  207. #   If the position b is at end of game,
  208. #   return an ascii string giving the result; otherwise, fail:
  209.  
  210.     local    nbn, sk
  211.  
  212.     sk := find( pc2p( "K", b.cmv), b.pcs)
  213.  
  214.     if not movlegal( b, movgen( b, sk)) & not movlegal( b, movgen( b)) then {
  215.     if chksqrs( b) then {
  216.         if b.cnm[ 1] == "W" then
  217.         return "1-0"
  218.         else
  219.         return "0-1"
  220.         }
  221.     else
  222.         return "1/2-1/2"
  223.     }
  224.     else if not upto( 'PRQprq', b.pcs) then {
  225.     nbn := 0
  226.     every upto( 'NBnb', b.pcs) do
  227.         nbn +:= 1
  228.     if nbn < 2 then
  229.         return "1/2-1/2"
  230.     }
  231. end
  232.     
  233.  
  234. procedure init( )
  235.  
  236. #   init initializes the program:
  237.  
  238.     local    whopipe, line, namdelim
  239.  
  240. #   Setup a data table for move generation:
  241.  
  242.     Frinclst := table( )
  243.     Frinclst[ "R"] := [ [1, 0],  [0, 1],  [-1, 0],  [0, -1] ]
  244.     Frinclst[ "N"] := [ [2, 1], [1, 2], [-1, 2], [-2, 1],
  245.             [-2, -1], [-1, -2], [1, -2], [2, -1] ]
  246.     Frinclst[ "B"] := [ [1, 1],  [-1, 1],  [-1, -1],  [1, -1] ]
  247.     Frinclst[ "Q"] := Frinclst[ "R"] ||| Frinclst[ "B"]
  248.     Frinclst[ "K"] := Frinclst[ "Q"]
  249.     Frinclst[ "r"] := Frinclst[ "R"]
  250.     Frinclst[ "n"] := Frinclst[ "N"]
  251.     Frinclst[ "b"] := Frinclst[ "B"]
  252.     Frinclst[ "q"] := Frinclst[ "Q"]
  253.     Frinclst[ "k"] := Frinclst[ "K"]
  254.  
  255. #   Setup a character set to delimit user names:
  256.  
  257.     namdelim := ~(&letters ++ &digits ++ '_.-')
  258.  
  259. #   Set reminder bell flag to off:
  260.  
  261.     Remind := ""
  262.  
  263. #   Set random number seed:
  264.  
  265.     &random := integer( map( "hxmysz", "hx:my:sz", &clock))
  266.  
  267. #   Get my name from user or "who am I" command and issue greeting:
  268.  
  269.     writes( "Your name (up to 8 letters & digits; default = user name)? ")
  270.     line := read( ) | kstop( "can't read user name")
  271.     Me := tokens( line, namdelim)
  272.     if /Me then {
  273.     whopipe := open( "who am i | awk '{print $1}' | sed 's/^.*!//'", "rp")
  274.     Me := tokens( read( whopipe), namdelim)
  275.     close( \whopipe)
  276.     }
  277.     if /Me then
  278.     write( "Can't get user name from system.")
  279.     while /Me do {
  280.     writes( "Your name? ")
  281.     line := read( ) | kstop( "can't get user name")
  282.     Me := tokens( line, namdelim)
  283.     }
  284.     write( "Welcome, ", Me, ", to Kriegspiel (double blind chess).")
  285.  
  286. #   Prompt user to enter color:
  287.  
  288.     while writes( "Your color (w or b)? ") do {
  289.     line := read( ) | kstop( "can't read color")
  290.     if find( line[ 1], "WwBb") then
  291.         break
  292.     }
  293.     Mycol := (find( line[ 1], "Ww"), "White") | "Black"
  294.     Yrcol := map( Mycol, "WhiteBlack", "BlackWhite")
  295.  
  296. #   Prompt user to enter opponent name:
  297.  
  298.     writes( "Enter opponent's name (default = auto): ")
  299.     Yu := tokens( read( ), namdelim) | "auto"
  300.  
  301. #   Prompt user to select "blind" mode, if desired:
  302.  
  303.     writes( "Totally blind mode (default is no)? ")
  304.     Blind := find( (tokens( read( )) \ 1)[ 1], "Yy")
  305.  
  306. #   Set communication file names and create my communication file:
  307.  
  308.     if Yu == "auto" then {
  309.     Mycname := "/dev/null"
  310.     Yrcname := "/dev/null"
  311.     }
  312.     else {
  313.     Mycname := "/tmp/krcom" || Mycol[ 1] || Me
  314.     Yrcname := "/tmp/krcom" || Yrcol[ 1] || Yu
  315.     remove( Mycname)
  316.     system( "/etc/mknod " || Mycname || " p && chmod 644 " ||
  317.         Mycname) = 0 | kstop( "can't create my comm file")
  318.     }
  319.  
  320. #   Get name of my log file, open it, then remove from directory:
  321.  
  322.     Logname := "/tmp/krlog" || Mycol[ 1] || Me
  323.     while /Logfile do {
  324.     writes( "Log file name (defaults to ", Logname, ")? ")
  325.     line := read( ) | kstop( "can't read log file name")
  326.     Logname := tokens( line)
  327.     Logfile := open( Logname, "cr")
  328.     }
  329.     remove( Logname)
  330.  
  331. #   Open our communication files, trying to avoid deadlock:
  332.  
  333.     write( "Attempting to establish communication with ", Yu)
  334.     if Mycol == "White" then
  335.     Mycomm := open( Mycname, "w") | kstop( "can't open my comm file")
  336.     while not (Yrcomm := open( Yrcname)) do {
  337.     write( "Still attempting to establish communication")
  338.     if system( "sleep 3") ~= 0 then
  339.         kstop( "gave up on establishing communications")
  340.     }
  341.     if Mycol == "Black" then
  342.     Mycomm := open( Mycname, "w") | kstop( "can't open my comm file")
  343.  
  344. #   Initialize board and moves:
  345.  
  346.     Bg := board(
  347.  
  348.     "RNBQKBNRPPPPPPPP                                pppppppprnbqkbnr",
  349.     "White", "Black", "W-Q", "W-K", "B-Q", "B-K", &null, 0)
  350.  
  351. #   Initialize set of move tries:
  352.  
  353.     Tries := set( )
  354.  
  355.     write( Logfile, "Kriegspiel game begins ", &dateline)
  356.     write( Logfile, Me, " is ", Mycol, "; ", Yu, " is ", Yrcol)
  357.     \ Blind & write( Logfile, Me, " is in 'totally blind' mode!")
  358.  
  359.     write( "You have the ", Mycol, " pieces against ", Yu)
  360.     \ Blind & write( "You have chosen to play in 'totally blind' mode!")
  361.     write( "At the \"Try\" prompt you may type help for assistance.")
  362.     write( "Initialization complete; awaiting first white move.")
  363.     return
  364. end
  365.  
  366.  
  367. procedure kstop( s)
  368.  
  369. #   Clean up and terminate execution with message s:
  370.  
  371.     local    logtemp
  372.  
  373.     close( \Mycomm)
  374.     remove( \Mycname)
  375.     write( \Logfile, "Kriegspiel game ends ", &dateline)
  376.     logboard( \ Logfile, \ Bg)
  377.     if seek( \Logfile) then {
  378.     logtemp := open( Logname, "w") | kstop( "can't open my log file")
  379.     every write( logtemp, ! Logfile)
  380.     write( "Game log is on file ", Logname)
  381.     }
  382.     stop( "Kriegspiel stop: ", s)
  383. end
  384.  
  385.  
  386. procedure logboard( file, b)
  387.  
  388. #   Print the full board position in b to file:
  389.  
  390.     local    f, r, p
  391.  
  392.     write( file, "Current board position:")
  393.     write( file, " a  b  c  d  e  f  g  h")
  394.     every r := 8 to 1 by -1 do {
  395.     write( file, "-------------------------")
  396.     every writes( file, "|", p2c( p := b.pcs[ fr2s( 1 to 8, r)])[ 1],
  397.         pc2p( p, "W"))
  398.     write( file, "|", r)
  399.     }
  400.     write( file, "-------------------------")
  401.     writes( file, b.cmv, " to move;")
  402.     writes( file, " enp file: ", "abcdefgh"[ \ b.fepp], ";")
  403.     writes( file, " castle mvs ", b.caswq || " " || b.caswk || " " ||
  404.     b.casbq || " " || b.casbk, ";")
  405.     write( file, " half-mvs played ", b.ply)
  406.     write( file, "")
  407. end
  408.  
  409.  
  410. procedure main( )
  411.  
  412.     local    line
  413.  
  414. #   Initialize player names and colors and establish communications:
  415.  
  416.     init( )
  417.  
  418. #   Loop validating our moves and processing opponent responses:
  419.  
  420.     repeat {
  421.     while Mycol == Bg.cmv do {
  422.         writes( Remind, "Try your (", Me, "'s) move # ",
  423.         Bg.ply / 2 + 1, ": ")
  424.         line := read( ) | kstop( "player read fail")
  425.         write( Mycomm, line)
  426.         write( Logfile, Me, " typed: ", line)
  427.         line := map( tokens( line)) | ""
  428.         case line of {
  429.         ""            : 0
  430.         left( "any", *line)    : myany( )
  431.         left( "board", *line)    : myboard( )
  432.         "end"            : myend( )
  433.         left( "help", *line)    : myhelp( )
  434.         left( "message", *line)    : mymessage( )
  435.         left( "remind", *line)    : myremind( )
  436.         default            : mytry( line)
  437.         }
  438.         }
  439.     while Yrcol == Bg.cmv do {
  440.         if Yu == "auto" then
  441.         line := automov( )
  442.         else
  443.         line := read( Yrcomm) | kstop( "opponent read fail")
  444.         write( Logfile, Yu, " typed: ", line)
  445.         line := map( tokens( line)) | ""
  446.         case line of {
  447.         ""            : 0
  448.         left( "any", *line)    : yrany( )
  449.         left( "board", *line)    : 0
  450.         "end"            : yrend( )
  451.         left( "help", *line)    : 0
  452.         left( "message", *line)    : yrmessage( )
  453.         left( "remind", *line)    : 0
  454.         default            : yrtry( line)
  455.         }
  456.         }
  457.     }
  458. end
  459.  
  460.  
  461. procedure movgen( b, s)
  462.  
  463. #   movgen generates the pseudo-legal moves in board position b from the
  464. #   piece on square s; if s is unspecified all pieces are considered.
  465. #   Note: pseudo-legal here means that the legality of the move has been
  466. #   determined up to the question of whether it leaves the moving side's
  467. #   king in check:
  468.  
  469.     local    r, f, p, snfr, m, fto, rto, sl, sh,
  470.         sto, fril, rp, r2, r4, r5, r7, ps
  471.  
  472.     ps := b.pcs
  473.  
  474.     sl := (\s | 1)
  475.     sh := (\s | 64)
  476.  
  477.     every s := sl to sh do {
  478.     if p2c( p := ps[ s]) == b.cmv then {
  479.         f := s2f( s)
  480.         r := s2r( s)
  481.         snfr := s2sn( s)
  482.  
  483. #   Pawn moves:
  484.  
  485.         if find( p, "Pp") then {
  486.         if p == "P" then {
  487.             rp :=  1; r2 := 2; r4 := 4; r5 := 5; r7 := 7
  488.             }
  489.         else {
  490.             rp := -1; r2 := 7; r4 := 5; r5 := 4; r7 := 2
  491.             }
  492.         if ps[ sto := fr2s( f, r + rp)] == " " then {
  493.             m := "P" || snfr || s2sn( sto)
  494.             if r = r7 then
  495.             suspend m || ! "RNBQ"
  496.             else {
  497.             suspend m
  498.             if r = r2 & ps[ sto := fr2s( f, r4)] == " " then
  499.                 suspend "P" || snfr || s2sn( sto)
  500.             }
  501.             }
  502.         every fto := 0 < (9 > (f - 1 to f + 1 by 2)) do {
  503.             m := "P" || snfr ||
  504.             s2sn( sto := fr2s( fto, r + rp)) || ":"
  505.             if p2c( ps[ sto]) == b.cnm then {
  506.             if r = r7 then
  507.                 every suspend m || ! "RNBQ"
  508.             else
  509.                 suspend m
  510.             }
  511.             if r = r5 & fto = \ b.fepp then
  512.             suspend m || "ep"
  513.             }
  514.         }
  515.  
  516. #   Sweep piece (rook, bishop, queen) moves:
  517.  
  518.         else if find( p, "RBQrbq") then {
  519.         every fril := ! Frinclst[ p] do {
  520.             fto := f
  521.             rto := r
  522.             while sto := fr2s( fto +:= fril[ 1], rto +:= fril[ 2]) do {
  523.             if ps[ sto] == " " then
  524.                 suspend pc2p( p, "W") || snfr || s2sn( sto)
  525.             else {
  526.                 if p2c( ps[ sto]) == b.cnm then
  527.                 suspend pc2p( p, "W") ||
  528.                     snfr || s2sn( sto) || ":"
  529.                 break
  530.                 }
  531.             }
  532.             }
  533.         }
  534.  
  535. #   Knight and king moves:
  536.  
  537.         else if find( p, "KNkn") then {
  538.         every fril := ! Frinclst[ p] do {
  539.             if sto := fr2s( f + fril[ 1], r + fril[ 2]) then {
  540.             if p2c( ps[ sto]) == b.cnm then
  541.                 suspend pc2p( p, "W") ||
  542.                 snfr || s2sn( sto) || ":"
  543.             else if ps[ sto] == " " then
  544.                 suspend pc2p( p, "W") || snfr || s2sn( sto)
  545.             }
  546.             }
  547.         if p == "K" then {
  548.             if (b.caswq ~== "", ps[ sn2s( "b1") : sn2s( "e1")] == "   ",
  549.             not sqratks( ps, sn2s( "d1"), "Black"),
  550.             not sqratks( ps, sn2s( "e1"), "Black")) then
  551.                 suspend "Ke1c1cas"
  552.             if (b.caswk ~== "", ps[ sn2s( "f1") : sn2s( "h1")] == "  ",
  553.             not sqratks( ps, sn2s( "f1"), "Black"),
  554.             not sqratks( ps, sn2s( "e1"), "Black")) then
  555.                 suspend "Ke1g1cas"
  556.             }
  557.         else if p == "k" then {
  558.             if (b.casbq ~== "", ps[ sn2s( "b8") : sn2s( "e8")] == "   ",
  559.             not sqratks( ps, sn2s( "d8"), "White"),
  560.             not sqratks( ps, sn2s( "e8"), "White")) then
  561.                 suspend "Ke8c8cas"
  562.             if (b.casbk ~== "", ps[ sn2s( "f8") : sn2s( "h8")] == "  ",
  563.             not sqratks( ps, sn2s( "f8"), "White"),
  564.             not sqratks( ps, sn2s( "e8"), "White")) then
  565.                 suspend "Ke8g8cas"
  566.             }
  567.         }
  568.         }
  569.     }
  570. end
  571.  
  572.  
  573. procedure movlegal( b, m)
  574.  
  575. #   Tests move m on board b and, if it does not leave the moving color in
  576. #   check, returns m; fails otherwise:
  577.  
  578.     local    ps, sfr, sto, sk
  579.  
  580.     ps := b.pcs
  581.     sfr := sn2s( m[ 2:4])
  582.     sto := sn2s( m[ 4:6])
  583.  
  584. #   Castling move:
  585.  
  586.     if m[ 6:9] == "cas" then {
  587.     if m == "Ke1c1cas" then
  588.         return not sqratks( ps, sn2s( "c1"), "Black") & m
  589.     if m == "Ke1g1cas" then
  590.         return not sqratks( ps, sn2s( "g1"), "Black") & m
  591.     if m == "Ke8c8cas" then
  592.         return not sqratks( ps, sn2s( "c8"), "White") & m
  593.     if m == "Ke8g8cas" then
  594.         return not sqratks( ps, sn2s( "g8"), "White") & m
  595.     }
  596.  
  597. #   Enpassant pawn capture:
  598.  
  599.     if m[ 6:9] == ":ep" then
  600.     ps[ fr2s( s2f( sto), s2r( sfr))] := " "
  601.  
  602. #   All non-castling moves:
  603.  
  604.     ps[ sto] := ps[ sfr]
  605.     ps[ sfr] := " "
  606.     sk := find( pc2p( "K", b.cmv), ps)
  607.     return not sqratks( ps, sk, b.cnm) & m
  608.  
  609. end
  610.  
  611.  
  612. procedure movmake( b, m)
  613.  
  614. #   Makes move m on board b:
  615.  
  616.     local    sfr, sto
  617.  
  618.     if m == "Ke1c1cas" then {
  619.     b.pcs[ sn2s( "a1")] := " "
  620.     b.pcs[ sn2s( "d1")] := "R"
  621.     }
  622.     else if m == "Ke1g1cas" then {
  623.     b.pcs[ sn2s( "h1")] := " "
  624.     b.pcs[ sn2s( "f1")] := "R"
  625.     }
  626.     else if m == "Ke8c8cas" then {
  627.     b.pcs[ sn2s( "a8")] := " "
  628.     b.pcs[ sn2s( "d8")] := "r"
  629.     }
  630.     else if m == "Ke8g8cas" then {
  631.     b.pcs[ sn2s( "h8")] := " "
  632.     b.pcs[ sn2s( "f8")] := "r"
  633.     }
  634.  
  635.     sfr := sn2s( m[ 2:4])
  636.     sto := sn2s( m[ 4:6])
  637.     b.pcs[ sto] := b.pcs[ sfr]
  638.     b.pcs[ sfr] := " "
  639.  
  640.     if find( m[ -1], "rnbqRNBQ") then
  641.     b.pcs[ sto] := pc2p( m[ -1], b.cmv)
  642.  
  643.     if sfr = sn2s( "e1") then    b.caswq := b.caswk := ""
  644.     if sfr = sn2s( "e8") then    b.casbq := b.casbk := ""
  645.  
  646.     if (sfr | sto) = sn2s( "a1") then    b.caswq := ""
  647.     if (sfr | sto) = sn2s( "h1") then    b.caswk := ""
  648.     if (sfr | sto) = sn2s( "a8") then    b.casbq := ""
  649.     if (sfr | sto) = sn2s( "h8") then    b.casbk := ""
  650.  
  651.     if m[ 6:9] == ":ep" then
  652.     b.pcs[ fr2s( s2f( sto), s2r( sfr))] := " "
  653.  
  654.     b.fepp := &null
  655.     if m[ 1] == "P" & abs( s2r( sfr) - s2r( sto)) = 2 then
  656.     b.fepp := s2f( sto)
  657.  
  658.     b.ply +:= 1
  659.     b.cmv :=: b.cnm
  660. end
  661.  
  662.  
  663. procedure movtry( m)
  664.  
  665. #   Tests whether the typed move m is legal in the global board Bg and, if so,
  666. #   returns the corresponding move returned from movgen (which will be in a
  667. #   different format with piece letter prefix, etc.).  Fails if m is not
  668. #   legal.  Note that if the any flag is set, only captures by pawns are
  669. #   allowed:
  670.  
  671.     local    ml, mt, sfr, sto
  672.  
  673.     mt := map( tokens( m)) | ""
  674.     if mt == "o-o" then
  675.     mt := (Bg.cmv == "White", "e1g1") | "e8g8"
  676.     else if mt == "o-o-o" then
  677.     mt := (Bg.cmv == "White", "e1c1") | "e8c8"
  678.  
  679.     sfr := sn2s( mt[ 1:3]) | fail
  680.     sto := sn2s( mt[ 3:5]) | fail
  681.  
  682.     if find( mt[ 5], "rnbq") then
  683.     mt[ 5] := map( mt[ 5], "rnbq", "RNBQ")
  684.     else mt := mt[ 1:5] || "Q"
  685.     
  686.     if \ Any then {
  687.     if Bg.pcs[ sfr] ~== pc2p( "P", Bg.cmv) then fail
  688.     every ml := movgen( Bg, sfr) do {
  689.         if ml[ 4:7] == mt[ 3:5] || ":" then {
  690.         if find( ml[ -1], "RNBQ") then
  691.             ml[ -1] := mt[ 5]
  692.         return movlegal( Bg, ml)
  693.         }
  694.         }
  695.     }
  696.     else {
  697.     every ml := movgen( Bg, sfr) do {
  698.         if ml[ 4:6] == mt[ 3:5] then {
  699.         if find( ml[ -1], "RNBQ") then
  700.             ml[ -1] := mt[ 5]
  701.         return movlegal( Bg, ml)
  702.         }
  703.         }
  704.     }
  705. end
  706.  
  707.  
  708. procedure myany( )
  709.  
  710. #   Process my any command.
  711. #   Check for captures by pawns and inform the player of any, and, if
  712. #   at least one, set Any flag to require that player try only captures
  713. #   by pawns:
  714.  
  715.     local    m, p, s
  716.  
  717.     if \ Any then {
  718.     write( "You have already asked 'Any' and received yes answer!")
  719.     fail
  720.     }
  721.  
  722.     p := pc2p( "P", Bg.cmv)
  723.     if movlegal( Bg, 1( m := movgen( Bg, 1(s := 9 to 56, Bg.pcs[ s] == p)),
  724.         m[ 6] == ":")) then {
  725.     write( "Yes; you must now make a legal capture by a pawn.")
  726.     Any := "Yes"
  727.     }
  728.     else
  729.     write( "No.")
  730. end
  731.  
  732.  
  733. procedure myboard( )
  734.  
  735. #   Process my board command by printing the board but omitting the
  736. #   opponent's pieces and the enpassant status; a count of pieces of
  737. #   both colors is printed:
  738. #   Note: no board printed in blind mode.
  739.  
  740.     local    f, r, p, nw, nb
  741.  
  742.     \ Blind & write( "Sorry; no board printout in blind mode!") & fail
  743.  
  744.     write( "Current board position (your pieces only):")
  745.     write( " a  b  c  d  e  f  g  h")
  746.     every r := 8 to 1 by -1 do {
  747.     write( "-------------------------")
  748.     every f := 1 to 8 do {
  749.         if (p2c( p := Bg.pcs[ fr2s( f, r)])) == Mycol then
  750.         writes( "|", Mycol[ 1], pc2p( p, "W"))
  751.         else
  752.         writes( "|  ")
  753.         }
  754.     write( "|", r)
  755.     }
  756.     write( "-------------------------")
  757.     writes( Bg.cmv, " to move; ")
  758.     writes( "castle mvs ", (Mycol == "White", Bg.caswq || " " || Bg.caswk) |
  759.     Bg.casbq || " " || Bg.casbk)
  760.     write( "; half-mvs played ", Bg.ply)
  761.     nw := nb := 0
  762.     every upto( &ucase, Bg.pcs) do nw +:= 1
  763.     every upto( &lcase, Bg.pcs) do nb +:= 1
  764.     write( nw, " White pieces, ", nb, " Black.")
  765.     write( "")
  766. end
  767.  
  768.  
  769. procedure myend( )
  770.  
  771. #   Process my end command:
  772.  
  773.     kstop( "by " || Me)
  774. end
  775.  
  776.  
  777. procedure myhelp( )
  778.  
  779. #   Process my help command:
  780.  
  781.     write( "")
  782.     write( "This is \"Kriegspiel\" (war play), a game of chess between two")
  783.     write( "opponents who do not see the location of each other's pieces.")
  784.     write( "Note: the moves of the special opponent 'auto' are played by the")
  785.     write( "program itself.  Currently, auto plays at a low novice level.")
  786.     write( "When it is your turn to move, you will be prompted to type")
  787.     write( "a move attempt or one of several commands.  To try a move,")
  788.     write( "type the from and to squares in algebraic notation, as in: e2e4")
  789.     write( "or b8c6.  Castling may be typed as o-o, o-o-o, or as the move")
  790.     write( "of the king, as in: e8g8.  Pawn promotions should look like")
  791.     write( "d7d8Q.  If omitted, the piece promoted to is assumed to be a")
  792.     write( "queen.  Letters may be in upper or lower case.  If the move is")
  793.     write( "legal, it stands, and the opponent's response is awaited.")
  794.     write( "If the move is illegal, the program will prompt you to")
  795.     write( "try again.  If the move is illegal because of the opponent's")
  796.     write( "position but not impossible based on the position of your")
  797.     write( "pieces, then your opponent will be informed that you tried")
  798.     write( "an illegal move (note: this distinction between illegal and")
  799.     write( "impossible is somewhat tricky and the program may, in some")
  800.     write( "cases, not get it right).  The program will announce the")
  801.     write( "result and terminate execution when the game is over.  You may")
  802.     write( "then inspect the game log file which the program generated.")
  803.     write( "")
  804.  
  805.     writes( "Type empty line for more or 'q' to return from help: ")
  806.     if map( read( ))[ 1] == "q" then
  807.     fail
  808.  
  809.     write( "")
  810.     write( "The program will let you know of certain events that take place")
  811.     write( "during the game.  For each capture move, both players will be")
  812.     write( "informed of the location of the captured piece.  The opponent")
  813.     write( "will be informed of a pawn promotion but not of the piece")
  814.     write( "promoted to or the square on which the promotion takes place.")
  815.     write( "When a player gives check, both players will be informed of the")
  816.     write( "event and of some information about the direction from which the")
  817.     write( "check arises, as in: check on the rank', 'check on the file',")
  818.     write( "'check on the + diagonal', 'check on the - diagonal', or 'check")
  819.     write( "by a knight'.  For a double check, both directions are given.")
  820.     write( "(A + diagonal is one on which file letters and rank numbers")
  821.     write( "increase together, like a1-h8, and a - diagonal is one in which")
  822.     write( "file letters increase while rank numbers decrease, as in a8-h1).")
  823.     write( "")
  824.     write( "Note: if you have selected the 'blind' mode, then you will")
  825.     write( "receive no information about checks, captures, or opponent")
  826.     write( "'any' or illegal move tries; nor will you be able to print")
  827.     write( "the board.  You will not even be told when your own pieces")
  828.     write( "are captured.  Except for answers to 'any' commands, the")
  829.     write( "program will inform you only of when you have moved, when")
  830.     write( "your opponent has moved, and of the result at end of game.")
  831.     write( "")
  832.  
  833.     writes( "Type empty line for more or 'q' to return from help: ")
  834.     if map( read( ))[ 1] == "q" then
  835.     fail
  836.  
  837.     write( "")
  838.     write( "Description of commands; note: upper and lower case letters")
  839.     write( "are not distinguished, and every command except 'end' may be") 
  840.     write( "abbreviated.")
  841.     write( "")
  842.     write( "any")
  843.     write( "")
  844.     write( "The 'any' command is provided to speed up the process of trying")
  845.     write( "captures by pawns.  Since pawns are the only pieces that capture")
  846.     write( "in a different manner from the way they ordinarily move, it is")
  847.     write( "often useful to try every possible capture, since such a move")
  848.     write( "can only be legal if it in fact captures something.  Since the")
  849.     write( "process of trying the captures can be time-consuming, the 'any'")
  850.     write( "command is provided to signal your intent to try captures by")
  851.     write( "pawns until you find a legal one.  The program will tell you if")
  852.     write( "you have at least one.  If you do then you must try captures by")
  853.     write( "pawns (in any order) until you find a legal one.  Note that the")
  854.     write( "opponent will be informed of your plausible 'any' commands (that")
  855.     write( "is, those that are not impossible because you have no pawns on")
  856.     write( "the board).")
  857.     write( "")
  858.  
  859.     writes( "Type empty line for more or 'q' to return from help: ")
  860.     if map( read( ))[ 1] == "q" then
  861.     fail
  862.  
  863.     write( "")
  864.     write( "board")
  865.     write( "")
  866.     write( "The 'board' command prints the current position of your")
  867.     write( "pieces only, but also prints a count of pieces of both sides.")
  868.     write( "Note: 'board' is disallowed in blind mode.")
  869.     write( "")
  870.     write( "end")
  871.     write( "")
  872.     write( "Then 'end' command informs the program and your")
  873.     write( "opponent of your decision to terminate the game")
  874.     write( "immediately.")
  875.     write( "")
  876.     write( "help")
  877.     write( "")
  878.     write( "The 'help' command prints this information.")
  879.     write( "")
  880.  
  881.     writes( "Type empty line for more or 'q' to return from help: ")
  882.     if map( read( ))[ 1] == "q" then
  883.     fail
  884.  
  885.     write( "")
  886.     write( "message")
  887.     write( "")
  888.     write( "The 'message' command allows you to send a one-line")
  889.     write( "message to your opponent.  Your opponent will be prompted")
  890.     write( "for a one-line response.  'message' may be useful for such")
  891.     write( "things as witty remarks, draw offers, etc.")
  892.     write( "")
  893.     write( "remind")
  894.     write( "")
  895.     write( "The 'remind' command turns on (if off) or off (if on) the")
  896.     write( "bell that is rung when the program is ready to accept your")
  897.     write( "move or command.  The bell is initially off.")
  898.     write( "")
  899.  
  900. end
  901.  
  902.  
  903. procedure mymessage( )
  904.  
  905. #   Process my message command:
  906.  
  907.     local    line
  908.  
  909.     write( "Please type a one-line message:")
  910.     line := read( ) | kstop( "can't read message")
  911.     write( Mycomm, line)
  912.     write( Logfile, line)
  913.     write( "Awaiting ", Yu, "'s response")
  914.     if Yu == "auto" then
  915.     line := "I'm just your auto opponent."
  916.     else
  917.     line := read( Yrcomm) | kstop( "can't read message response")
  918.     write( Yu, " answers: ", line)
  919.     write( Logfile, line)
  920. end
  921.  
  922.  
  923. procedure myremind( )
  924.  
  925. #   Process my remind command:
  926.  
  927.     if Remind == "" then
  928.     Remind := "\^g"
  929.     else
  930.     Remind := ""
  931. end
  932.  
  933.  
  934. procedure mytry( mt)
  935.  
  936. #   Process my move try mt:
  937.  
  938.     local    ml, result
  939.  
  940.     if ml := movtry( mt) then {
  941.     Lmv := ml
  942.     write( Me, " (", Mycol, ") has moved.")
  943.     write( Logfile, Me, "'s move ", Bg.ply / 2 + 1, " is ", ml)
  944.     / Blind & write( Me, " captures on ", s2sn( sqrcap( Bg, ml)))
  945.     movmake( Bg, ml)
  946.     / Blind & saycheck( )
  947.     Any := &null
  948.     Tries := set( )
  949.     if result := gamend( Bg) then {
  950.         write( "Game ends; result: ", result)
  951.         write( Logfile, "Result: ", result)
  952.         kstop( "end of game")
  953.         }
  954.     }
  955.     else
  956.     write( "Illegal move, ", Me, "; try again:")
  957. end
  958.  
  959.  
  960. procedure p2c( p)
  961.  
  962. #   Returns "White" if p is white piece code ("PRNBQK"), "Black"
  963. #   if p is black piece code ("prnbqk"), and " " if empty square
  964. #   (" "):
  965.  
  966.     if find( p, "PRNBQK") then
  967.     return "White"
  968.     else if find( p, "prnbqk") then
  969.     return "Black"
  970.     else
  971.     return " "
  972. end
  973.  
  974.  
  975. procedure pc2p( p, c)
  976.  
  977. #   Returns the piece letter for the piece of type p but color c;
  978. #   returns " " if p == " ".  Thus pc2p( "R", "Black") == "r".
  979. #   c may be abbreviated to "W" or "B":
  980.  
  981.     if c[ 1] == "W" then
  982.     return map( p, "prnbqk", "PRNBQK")
  983.     else
  984.     return map( p, "PRNBQK", "prnbqk")
  985. end
  986.  
  987.  
  988. procedure s2f( square)
  989.  
  990. #   Returns the file number of the square number "square"; fails
  991. #   if invalid square number:
  992.  
  993.     return ( (0 < ( 65 > integer( square))) - 1) % 8 + 1
  994. end
  995.  
  996.  
  997. procedure s2r( square)
  998.  
  999. #   Returns the rank number of the square number "square"; fails
  1000. #   if invalid square number:
  1001.  
  1002.     return ( (0 < ( 65 > integer( square))) - 1) / 8 + 1
  1003. end
  1004.  
  1005.  
  1006. procedure s2sn( square)
  1007.  
  1008. #   Returns the algebraic square name corresponding to square number
  1009. #   "square"; fails if invalid square number:
  1010.  
  1011.     return "abcdefgh"[ s2f( square)] || string( s2r( square))
  1012. end
  1013.  
  1014.  
  1015. procedure saycheck( )
  1016.  
  1017. #   Announce checks, if any, in global board Bg:
  1018.  
  1019.     local    s, sk
  1020.  
  1021.     sk := find( pc2p( "K", Bg.cmv), Bg.pcs)
  1022.  
  1023.     every s := chksqrs( Bg) do {
  1024.     writes( (Mycol == Bg.cnm, Me) | Yu, " checks ")
  1025.     if s2r( s) == s2r( sk) then
  1026.         write( "on the rank.")
  1027.     else if s2f( s) == s2f( sk) then
  1028.         write( "on the file.")
  1029.     else if ( s2f( s) - s2f( sk)) = ( s2r( s) - s2r( sk)) then
  1030.         write( "on the + diagonal.")
  1031.     else if ( s2f( s) - s2f( sk)) = ( s2r( sk) - s2r( s)) then
  1032.         write( "on the - diagonal.")
  1033.     else
  1034.         write( "by knight.")
  1035.     }
  1036. end
  1037.  
  1038.  
  1039. procedure sn2s( sn)
  1040.  
  1041. #   Returns the square number corresponding to the algebraic square
  1042. #   name sn; examples: sn2s( "a1") = 1, sn2s( "b1") = 2, sn2s( "h8") = 64.
  1043. #   Fails if invalid square name:
  1044.  
  1045.     return find( sn[ 1], "abcdefgh") + 8 * (0 < (9 > integer( sn[ 2]))) - 8
  1046. end
  1047.  
  1048.  
  1049. procedure sqratks( ps, s, c)
  1050.  
  1051. #   Generates the numbers of squares of pieces of color c that "attack"
  1052. #   square s in board piece array ps; fails if no such squares:
  1053.  
  1054.     local    file, rank, rfr, sfr, fril, p, ffr
  1055.  
  1056.     file := s2f( s)
  1057.     rank := s2r( s)
  1058.  
  1059. #   Check for attacks from pawns:
  1060.  
  1061.     rfr := (c == "White", rank - 1) | rank + 1
  1062.     every sfr := fr2s( file - 1 to file + 1 by 2, rfr) do {
  1063.     if ps[ sfr] == pc2p( "P", c) then
  1064.         suspend sfr
  1065.     }
  1066.  
  1067. #   Check for attack from king or knights:
  1068.  
  1069.     every fril := ! Frinclst[ p := ("K" | "N")] do {
  1070.     if sfr := fr2s( file + fril[ 1], rank + fril[ 2]) then {
  1071.         if ps[ sfr] == pc2p( p, c) then
  1072.         suspend sfr
  1073.         }
  1074.     }
  1075.  
  1076. #   Check for attacks from sweep (rook and bishop) directions:
  1077.  
  1078.     every fril := ! Frinclst[ p := ("R" | "B")] do {
  1079.     ffr := file
  1080.     rfr := rank
  1081.     while sfr := fr2s( ffr +:= fril[ 1], rfr +:= fril[ 2]) do {
  1082.         if ps[ sfr] ~== " " then {
  1083.         if ps[ sfr] == pc2p( p | "Q", c) then
  1084.             suspend sfr
  1085.         break
  1086.         }
  1087.         }
  1088.     }
  1089. end
  1090.  
  1091.  
  1092. procedure sqrcap( b, m)
  1093.  
  1094. #   Returns square of piece captured by move m in board b; fails if m
  1095. #   not a capture:
  1096.  
  1097.     local    fto, rfr
  1098.  
  1099.     if m[ 6:9] == ":ep" then {
  1100.     fto := find( m[ 4], "abcdefgh")
  1101.     rfr := integer( m[ 3])
  1102.     return fr2s( fto, rfr)
  1103.     }
  1104.     else if m[ 6] == ":" then
  1105.     return sn2s( m[ 4:6])
  1106. end
  1107.  
  1108.  
  1109. procedure tokens( s, d)
  1110.  
  1111. #   Generate tokens from left to right in string s given delimiters in cset
  1112. #   d, where a token is a contiguous string of 1 or more characters not in
  1113. #   d bounded by characters in d or the left or right end of s.
  1114. #   d defaults to ' \t'.
  1115.  
  1116.     s := string( s) | fail
  1117.     d := (cset( d) | ' \t')
  1118.  
  1119.     s ? while tab( upto( ~d)) do
  1120.     suspend( tab( many( ~d)) \ 1)
  1121. end
  1122.  
  1123.  
  1124. procedure yrany( )
  1125.  
  1126. #   Process opponent's any command:
  1127.  
  1128.     local    m, p, s
  1129.  
  1130.     if \ Any then fail
  1131.  
  1132.     p := pc2p( "P", Bg.cmv)
  1133.     if not find( p, Bg.pcs) then fail
  1134.  
  1135.     / Blind & writes( Yu, " asked 'any' and was told ")
  1136.  
  1137.     if movlegal( Bg, 1( m := movgen( Bg, 1(s := 9 to 56, Bg.pcs[ s] == p)),
  1138.         m[ 6] == ":")) then {
  1139.     / Blind & write( "yes.")
  1140.     Any := "Yes"
  1141.     }
  1142.     else
  1143.     / Blind & write( "no.")
  1144. end
  1145.  
  1146.  
  1147. procedure yrend( )
  1148.  
  1149. #   Process opponent's end command:
  1150.  
  1151.     write( "Game terminated by ", Yu, ".")
  1152.     kstop( "by " || Yu)
  1153. end
  1154.  
  1155.  
  1156. procedure yrmessage( )
  1157.  
  1158. #   Process opponent's message command:
  1159.  
  1160.     local    line
  1161.  
  1162.     line := read( Yrcomm) | kstop( "can't read opponent message")
  1163.     write( "Message from ", Yu, ": ", line)
  1164.     write( Logfile, line)
  1165.     write( "Please write a one-line response:")
  1166.     line := read( ) | kstop( "can't read response to opponent message")
  1167.     write( Mycomm, line)
  1168.     write( Logfile, line)
  1169. end
  1170.  
  1171.  
  1172. procedure yrtry( mt)
  1173.  
  1174. #   Process opponent move try (or other type-in!) mt:
  1175.  
  1176.     local    ml, result, s, mtr, b, po, sfr, sto
  1177.  
  1178.     if ml := movtry( mt) then {
  1179.     Lmv := ml
  1180.     write( Yu, " (", Yrcol, ") has moved.")
  1181.     write( Logfile, Yu, "'s move ", Bg.ply / 2 + 1, " is ", ml)
  1182.     / Blind & write( Yu, " captures on ", s2sn( sqrcap( Bg, ml)))
  1183.     if find( ml[ -1], "RNBQ") then
  1184.         / Blind & write( Yu, " promotes a pawn.")
  1185.     movmake( Bg, ml)
  1186.     / Blind & saycheck( )
  1187.     Any := &null
  1188.     Tries := set( )
  1189.     if result := gamend( Bg) then {
  1190.         write( "Game ends; result: ", result)
  1191.         write( Logfile, "Result: ", result)
  1192.         kstop( "end of game")
  1193.         }
  1194.     }
  1195.  
  1196. #   Inform Me if opponent move illegal but not impossible.  Don't inform
  1197. #   if illegal move already tried.  Note: distinction between "illegal"
  1198. #   and "impossible" is tricky and may not always be made properly.
  1199. #   Note: don't bother informing if in blind mode.
  1200.  
  1201.     else {
  1202.     \ Blind & fail
  1203.     mtr := map( tokens( mt)) | ""
  1204.     if mtr == "o-o" then
  1205.         mtr := (Bg.cmv == "White", "e1g1") | "e8g8"
  1206.     else if mtr == "o-o-o" then
  1207.         mtr := (Bg.cmv == "White", "e1c1") | "e8c8"
  1208.     mtr := mtr[ 1:5] | fail
  1209.     if member( Tries, mtr) then fail
  1210.     insert( Tries, mtr)
  1211.     b := copy( Bg)
  1212.     po := (b.cmv[ 1] == "W", "prnbqk") | "PRNBQK"
  1213.     b.pcs := map( b.pcs, po, "      ")
  1214.     sfr := sn2s( mtr[ 1:3]) | fail
  1215.     sto := sn2s( mtr[ 3:5]) | fail
  1216.     if sn2s( movgen( b, sfr)[ 4:6]) = sto then
  1217.         / Any & write( Yu, " tried illegal move.")
  1218.     else {
  1219.         b.pcs[ sto] := pc2p( "P", b.cnm)
  1220.         if sn2s( movgen( b, sfr)[ 4:6]) = sto then
  1221.         write( Yu, " tried illegal move.")
  1222.         }
  1223.     }
  1224. end
  1225.