| Author |
Message |
Jose Carrillo (J_carrillo_vii)
New member Username: J_carrillo_vii
Post Number: 6 Registered: 5-2008
| | Posted on Sunday, January 20, 2013 - 11:32 am: | |
Should be simple to do, but I can't figure it out. When generating moves for a piece, how do I do a boolean test as to whether the player's King is already in check or not? I have a piece that normally moves in a particular way, but when his King is in check, this piece is allowed to move in a different way to allow it to protect his King by blocking the check. Is there a "in-check?" function anywhere just returning a true or false value when the King is in check (regardless of where the King is located in the board)? Has someone already programmed such a function in any of their games that I can leverage? Thanks. |
Malcolm James Webb (Mjw)
New member Username: Mjw
Post Number: 9 Registered: 5-2008
| | Posted on Sunday, January 20, 2013 - 3:47 pm: | |
I can think of two ways for another piece to test for check. Both use the "attacked?" function. 1) Search for the King, and then find out if it is "attacked?". To search for the King, when defining the board create a link "next" linking all the squares on the board (see my recently posted game "Abagoren Chess" for an example, or any of numerous other games). Create the macro "test-check" for movement of pieces which needs to know if the King is in check: (define test-check (mark next (while not-marked? (if (and friend? (piece? King)) (set-flag in-check attacked?)) next))) After executng this function, the flag in-check can be tested as true or false. 2) Use a virtual King-move which only occurs when in check: Define an off board position in the board definition (could be a dummy position, as no piece actually moves there eg (dummy END)) Include in the definition of King moves the move (if attacked? (capture END) add) Note that this move cannot actually be executed if the goal is checkmate, as it is a capture which cannot possibly get your king out of check. If the goal is not defined as checkmate, you may need to use move-type & move-priorities to stop an actual move being generated. Then include in the move definition of the check-blocking move (verify (defended? END)) Both the defended? and the attacked? functions are time-consuming for Zillions. |
Jose Carrillo (J_carrillo_vii)
New member Username: J_carrillo_vii
Post Number: 7 Registered: 5-2008
| | Posted on Sunday, January 20, 2013 - 4:55 pm: | |
Thanks Malcom. After much work, I figured another way of testing for check in my variant: == (define in-check (and (piece? King $1) (attacked? $1) ) ) (define white-in-check (or (in-check d1) (in-check d2) (in-check d3) (in-check e1) (in-check e2) (in-check e3) (in-check f1) (in-check f2) (in-check f3) ) ) (define black-in-check (and (in-check d8) (in-check d9) (in-check d10) (in-check e8) (in-check e9) (in-check e10) (in-check f8) (in-check f9) (in-check f10) ) ) == In my case, the Kings are locked inside of 9-square palaces, so at the end, I decided to test every square. Thanks for your suggestions. I'll look them up for future aplications. |
Jose Carrillo (J_carrillo_vii)
New member Username: J_carrillo_vii
Post Number: 8 Registered: 5-2008
| | Posted on Sunday, January 20, 2013 - 5:00 pm: | |
Oops, just noticed that the "and" in the black-in-check function should actually be an "or". |
|