| Author |
Message |
Martin Omander (Momander)
| | Posted on Saturday, November 20, 2004 - 12:43 am: | |
Here's a ZRF syntax question from a newbie. I am trying to implement a game where legal pawn moves have to start or end adjacent to a king. It's relatively easy to figure out if the piece starts next to a king using this syntax: (if "condition_for_start_position" n add) Here is the implementation (dots used to preserve indentation): (define PawnMove ..( ....(if ......(and ........(not-friend? $1) ........(or ..........(piece? King n) ..........(piece? King e) ..........(piece? King s) ..........(piece? King w) ........) ......) ......$1 add ....) ..) ) To test a condition for the end position I would do this: (n (if "condition_for_end_position") add) Here is that implementation: (define PawnMove2 ..($1 ....(verify not-friend?) ....(if ......(or ........(piece? King n) ........(piece? King e) ........(piece? King s) ........(piece? King w) ......) ......add ....) ..) ) But I'm not adept enough at ZRF yet to test the condition for _both_ the start and the end position. Any suggestions? Thanks /Martin |
L. Lynn Smith (Interrupt27)
| | Posted on Saturday, November 20, 2004 - 1:02 am: | |
Use the VERIFY statement instead of the IF, thus: (define PawnMove ..( ....(verify ......(and ........(not-friend? $1) ........(or ..........(piece? King n) ..........(piece? King e) ..........(piece? King s) ..........(piece? King w) ........) ......) ....) ....$1 ....(verify ......(or ........(piece? King n) ........(piece? King e) ........(piece? King s) ........(piece? King w) ......) ....) ....add ..) ) This should fulfill your requirements. |
Martin Omander (Momander)
| | Posted on Saturday, November 20, 2004 - 1:21 am: | |
Thanks for the reply! I ran the code from your posting. However, it seems to only allow pawn moves that end next to king, not ones that start next to a king. Any thoughts? /Martin |
Martin Omander (Momander)
| | Posted on Saturday, November 20, 2004 - 1:29 am: | |
I was mistaken in my last post. The code posted only allows moves moves that start AND end next to a king. How would one go about writing code that allows moves that start OR end next to king? /Martin |
Martin Omander (Momander)
| | Posted on Saturday, November 20, 2004 - 1:37 am: | |
OK, I think I have figured it out now. L. Lynn's code only had a "verify" in one place where an "if" was needed: (define PawnMove ..( ....(if ......(and ........(not-friend? $1) ........(or ..........(piece? King n) ..........(piece? King e) ..........(piece? King s) ..........(piece? King w) ........) ......) ....) ....$1 ....(verify ......(or ........(piece? King n) ........(piece? King e) ........(piece? King s) ........(piece? King w) ......) ....) ....add ..) ) I still need to verify that we don't capture a friendly piece in the second part, but that shouldn't be too hard. Again, thanks for the help! /Martin |
L. Lynn Smith (Interrupt27)
| | Posted on Saturday, November 20, 2004 - 5:05 pm: | |
My bad. I was thinking AND instead of OR. ;-) Glad my rambling helped. |
L. Lynn Smith (Interrupt27)
| | Posted on Saturday, November 20, 2004 - 5:16 pm: | |
But you might consider the following code: (define PawnMove ..( ....(verify (not-friend? $1)) ....(set-flag King_near ......(or ........(piece? King n) ........(piece? King e) ........(piece? King s) ........(piece? King w) ......) ....) ....$1 ....(if (not-flag? King_near) ......(verify ........(or ..........(piece? King n) ..........(piece? King e) ..........(piece? King s) ..........(piece? King w) ........) ......) ....) ....add ..) ) Double-check for syntax. |
Martin Omander (Momander)
| | Posted on Saturday, November 20, 2004 - 9:43 pm: | |
Aha, that was it! Despite the brave words in my last post that checking for not-friendly wouldn't be too hard, I wasn't able to get it right. But with your latest code snippet everything works perfectly, and I got a real-life example of flag usage as well. Just reading the language reference wasn't enough for me to understand flags. Thanks, I really appreciate it! /Martin |