Detecting Opponent In Check Log Out | Topics | Search
Moderators | Register | Edit Profile

Zillions of Games Discussion Forum » Designing Games for Zillions » Detecting Opponent In Check « Previous Next »

Author Message
Pargat Perrer (Pargat_perrer)
New member
Username: Pargat_perrer

Post Number: 1
Registered: 5-2015
Posted on Friday, May 01, 2015 - 12:28 am:   

I have seen from some posts that it is possible to detect if your own King is in check after you make a move. What about the opposing King? This would require detecting whether (1) the piece you just moved has put the opposing King in check, and / or (2) the piece you just moved created a discovered check on the opposing King.
M Winther (Kalroten)
New member
Username: Kalroten

Post Number: 165
Registered: 1-2007
Posted on Saturday, May 02, 2015 - 7:27 am:   

The simplest way is to search for the king in a while loop. Then you check if the position is attacked with "(attacked? <position>)"

If you start the search at the kingside corner square for the player's colour (i.e. h1 or h8), then you will find the king immediately for most of the game. Later in the game, such a loop doesn't matter much, when there are so few pieces left. The search tree is what is costly.
--Mats
M Winther (Kalroten)
New member
Username: Kalroten

Post Number: 166
Registered: 1-2007
Posted on Saturday, May 02, 2015 - 7:36 am:   

Sorry, that won't work, because the piece hasn't yet moved to the square from which it can check the king. No, you have to do the same thing as above, but use "(defended? <position>)", instead.

It means that the king will be sought for each move generated by the AI. It could be written more effectively, by only searching once and storing the position in some clever way, but I don't think it's necessary.
--Mats
Pargat Perrer (Pargat_perrer)
New member
Username: Pargat_perrer

Post Number: 2
Registered: 5-2015
Posted on Sunday, May 03, 2015 - 12:54 am:   

Hi Mats, thanks for responding. On further thought, I don't think either attacked? nor defended? will do the job, because as you wrote, the piece hasn't been moved yet. You can (I think) only ask attacked? or defended? after or before a move has been made with an 'add' statement. I want to ask it without actually committing to the 'add' statement.

Example: White has Nc3 and Black has Ke7. Asking defended? for position e7 would answer false, because Knight hasn't moved to d5. I want to disallow Nd5+ because it gives check, so defended? won't work.

I discovered in another thread a very clever answer you gave that could possible handle the direct checks. You wrote:
"In order to hinder checks when dropping (pieces) you could perhaps do this cleverly by endowing the king with move-types k_bishop_moves, k_knight_moves, k_queen_moves, and k_rook_moves. But these should have a lower priority than the standard moves so that the king can never execute them. However, if you drop a bishop, then you can verify that this is not checking the enemy king by writing (verify not-attacked? k_bishop_moves). If you drop a knight, then you verify in this way: (verify not-attacked? k_knight_moves)."

That is really amazing! It should work... but then there is still discovered checks....argh! Discovered check would be VERY problematic. Just think of it: White has Bishop on b2 and Knight on c3. Black King is on d4. White moves Nc3-a4. Now the Black King is in check not from the Knight, but from the b2 Bishop. What logic can determine all the possible discovered checks? It looks like this is impossible in Zillions... but if you think of something, let me know!

The idea of my variant is that from a given (input) position, neither side may make moves that give check or capture material until N 'passive' moves have been made. So if N = 4, then both sides must try and improve their position using moves that don't capture nor check, then after the 4 moves they can start playing normal chess.
M Winther (Kalroten)
New member
Username: Kalroten

Post Number: 167
Registered: 1-2007
Posted on Tuesday, May 05, 2015 - 10:15 pm:   

Sorry, I should know this, but I haven't programmed seriously for a while. You could add "king-rook-moves", etc., to the king definition, with a lower priority. Then you can verify that the position you are going to (with the rook) is not "attacked? king-rook-moves <position>". So this part is fairly easy.

The discovered checks is more difficult. Perhaps you could add "king-bishop-xray-moves", etc., to the king definition. It verifies that the attacked piece is an enemy bishop and that there is an enemy piece in between. So when you move a rook, you first have to check the diagonal you are moving from. If you find a friendly bishop or queen there, you must verify that it is not "attacked? king-bishop-xray-moves <position>"

It should work, but there is a lot of programming.
/Mats
Pargat Perrer (Pargat_perrer)
New member
Username: Pargat_perrer

Post Number: 3
Registered: 5-2015
Posted on Wednesday, May 06, 2015 - 11:14 pm:   

Hi Mats,
I'm glad you are answering me even though you aren't programming seriously these days. I'm assuming you are still interested in chess variants, and if so, I'd like to say a little more about what I'm trying to do. Imagine you are sitting down to play a normal game of chess. Except right from the beginning, you are told that up to a certain point, you can't make any moves that give check or capture material. You never know when that 'certain point' is going to come, except you are told it will most definitely come within let's say your first 16 moves. So between moves 1 and N, where N could be anything from 1 to 16, you are doing something a little different: you are positioning your pieces for attack and defense as if at any moment hostilities are about to break out. This is a little more what war is really like, and it's something that is missing in chess. And when you think about it, constantly positioning your pieces in such a manner is DIFFICULT! You have to think about attack without attacking, and you must handle defense as well... and you never know when the bell rings!

So that is the purpose of my variant, except that N might not be as high as 16 and also the start position might be any legal chess position. Imagine if you sit down to play from a position where you are already in good attacking formation... what do you do while you wait for the bell to ring?

Well anyway, I've hit a snag with the parser... it won't parse even a very simple addition.

To see what I'm up against, take your chess.zrf file and make a copy, call it chess2.zrf if you like. Edit this file as follows (it's very simple):

Change the King's moves list to this:
(moves
(move-type normal_moves)
(king-shift n)
(king-shift e)
(king-shift s)
(king-shift w)
(king-shift ne)
(king-shift nw)
(king-shift se)
(king-shift sw)
(O-O)
(O-O-O)
(move_type checking_moves)
(k_pawn_capture_moves)
)

In the code section that begins
(game
(title "Chess")
...

right after the section that has the (board-setup ...) definition, add this statement:

(move-priorities normal_moves checking_moves)

Now somewhere near the top of the file, add these macro definitions:

;---- Defines a King move that would capture an enemy piece as a Pawn does.
(define k_pawn_capture_move
(
$1
(verify enemy?)
add
)
)

;---- To be given as a lower-priority King move
(define k_pawn_capture_moves
(
(k_pawn_capture_move nw)
(k_pawn_capture_move ne)
)
)

Now try and parse this file. You should get this ridiculous-seeming parser error:

133: parse error at nw
(( nw (verify enemy?) add ) ( ne (verify enemy?) add ) )


I cannot fathom this. This is the simplest addition I can image making, and it suddenly can't parse nw? And what does the number 133 indicate?

If you could help with this, it could help me figure out how to actually get this variant to compile.
Pargat Perrer (Pargat_perrer)
New member
Username: Pargat_perrer

Post Number: 4
Registered: 5-2015
Posted on Thursday, May 07, 2015 - 1:15 pm:   

Mats, forget that last problem. It turned out to be a typo, I typed "move_type" instead of "move-type" in the King move list. That's a pretty bad compiler that can't inform you of typos like that and instead leads you on a wild goose chase...

Anyway, I'm very close to eliminating discovered checks. In fact, I have eliminated them BUT I used an "add-partial" to a dummy square to do it. This causes a new problem I've never seen before: At a certain point, both opponents moves become "partial 0 Pass" whatever the bleep that is, and the game is declared a draw. I tried adding options to prevent passing moves in the game section, i.e.
(game
...
(option "pass turn" false)
(option "pass partial" false)
...
)

but this has NO EFFECT. For whatever reason, passing moves is still allowed. This product is so flawed it is ridiculous.
Any ideas how I can still use add-partial and NOT allow passing moves?
Pargat Perrer (Pargat_perrer)
New member
Username: Pargat_perrer

Post Number: 5
Registered: 5-2015
Posted on Thursday, May 07, 2015 - 9:16 pm:   

Well, I gave up on using add-partial since that allows players to pass on moves and the game gets
declared a draw, even from the start position. Setting the game option to disallow passing a turn
DOES NOT WORK. Probably a bug that has existed forever.

So I tried a new approach, and it was promising until I got to the part where I call
(if (attacked? k_bishop_capture_move_type)
(set-attribute may_move? false)

According to the Zillions documentation, calling attacked? with a move type is supposed to actually
play those possible moves for the opponent to see if the current position is attacked. This also
appears to be b.s. My debugging has proven that the special moves of this move type which I call
k_bishop_capture_move_type NEVER get called. The first thing I do in that move code is
(create White Rook h8)
which is a common debugging technique that has been working great so far to trace how far I'm
getting into the call stack.
But the rook never gets created in this special move code block, which means the moves themselves
are never being played. The (if (attacked? ...) is always returning false even when the current
position is being attacked according to how I defined the special (low-priority) King move code.

So the whole thing can't be done because the engine doesn't work as advertised. It is completely
false that attacked? <move-type> actually plays the moves of <move_type>. Which I guess means
attacked? is broken or it works in some other way.

I have checked over and over that I have all spelling right in this call stack. No more typos.

Well, I guess I have to give up on this engine (I am using 2.0 by the way). Too buggy.
Tony Britton (Captain)
New member
Username: Captain

Post Number: 24
Registered: 12-2009
Posted on Thursday, March 27, 2025 - 1:29 pm:   

H ello, I'm back at it again. Has anyone out there figured out a way to have every move a cascade type move, where the second move is to verify that the players king is not check. I Know you can have Zillions do it for you when using checkmate as the win/loss condition. But not when capturing the opposing players kings is a part of the game, and when a checkmate scenario is a forced pass situation. When I use capturing the kings as part of the rules. I need to ensure that none of the players pieces can move when a checkmate scenario occurs. Thus creating a forced pass situation.
M Winther (Kalroten)
New member
Username: Kalroten

Post Number: 181
Registered: 1-2007
Posted on Tuesday, April 22, 2025 - 6:43 am:   

The way to do it is perhaps like I did in Marseillais Chess, to add an "acknowledge button" and let the king check if it is threatened.

Add Your Message Here
Post:
Username: Posting Information:
This is a private posting area. Only registered users and moderators may post messages here.
Password:
Options: Enable HTML code in message
Automatically activate URLs in message
Action: