Given a chess app built with magic bitboard how do I check for checkmate? - chess

OK, I have this chess app built with bitboard and I want to check if a given move put the opponent pieces in checkmate.
Verifying a check situation is easy. You build the bitmask of the enemy pieces' attack and you AND that with the bitmask of the opposite king, if the result is not zero, you have a check.
But what about check mate? A checkmate is something that will happen after the check. I mean, I move a piece, the app detects that the move generated a check. Then how do I know if this check is a check mate? Do I have to generate all possible bitboards for all possible plays of the opponent and check if there is a move that can remove the king from check? This not appears to be practical. Is there another way?

I don't think there is any other way. The final algorithm to me looks like:
Obviously, firstly check if by moving the king the opponent may run away from the check.
For each opponent's piece check if it is "forked" (that means that by moving it the opponents clears the way for any other check). Just "remove" it from the board and see if after that a new check is created.
If it is not forked: check if by moving that piece the opponent may block the check condition. This is done by intersecting two subsets of cells: one is a set of cells, where the opponent's piece may move, the other is a "line" between the opponent's king and the figure that attacks, and, therefore, "checks" it.
If these subsets intersect and the condition in the second step is passed - looks like the current situation is not a checkmate.
If there is no figure that is not forked AND can block the check - the situation is a checkmate.

Related

Optaplanner: How to calculate score delta for move

I'm using Optaplanner to automatically solve school timetables. After a timetable has been solved the user will manually change some lessons and will get feedback on how this affects the score via the following call:
scoreManager.updateScore(timetable);
This call takes some 200ms and will, I assume, do a complete evaluation. Im trying to optimize this and want to only pass in a Move object so that Optaplanner only has to recalculate the delta, like:
scoreManager.updateScore(previousTimetable,changeMove);
Is there a way to do this?
There really is no way how to do just a single move. You don't do moves - the solver does moves. You can only make external problem changes to the solution. You should look into the ProblemChange interface and its use in the SolverManager.
However, the problem change will likely reset the entire working solution anyway. And after the external change is done, you're not guaranteed that the solution will still make sense. (What if it breaks hard constraints now?) You simply need to expect and account for the fact that, after users submit their changes, the solver will need to run; possibly even for a prolonged period of time.

Handling multiple moves in chained planning variable

I'm trying to implement a variation of the vehicle routing example where instead of customers I have "pick ups" and "drop offs". My hard constraints are:
Each associated pick up/drop off pair must be carried out by the same vehicle (e.g. if vehicle A picks up an item, that item cannot be dropped off by vehicle B).
A pick up must be carried out before it's associated drop off (e.g. you can't drop something off unless you've already picked it up).
A vehicle cannot exceed it's maximum capacity.
Other than these hard constraints my solution works much like the vehicle routing example, where each vehicle has a chain of locations (either a PickUp or a DropOff).
The problem I'm having is that using the default moves it cannot easily move both a PickUp and a DropOff to a different vehicle. For example the following change move results in an invalid state and so will be rejected:
To finish the move properly, I would need to do an additional move so that the drop off belongs to the same chain as the pickup:
It feels like the right thing to do with be to implement some kind of composite move which carries out both moves simultaneously, however I'm not sure of the best way to approach this. Has anyone come across a similar issue to this before?
I've seen users do this before. The optaplanner-examples itself doesn't have a VRPPD example yet (PD stands for Pick Up and Delivery), so you can't just copy paste.
Reuse CompositieMove, see it's static methods to build one.
What usually works: build a custom MoveListFactory (and later refactor it to a MoveIteratorFactory to scale out) and have it produce CompositeMove's of ChainedChangeMove and/or ChainedSwapMove.

Need SpriteKit collision to occur only on first contact

I currently have two nodes in use, one of which is a boat (polygon) and the other a straight vertical rectangle. The boat needs to be a polygon so that it can be hit via projectiles, however this causes me problems when it collides with the vertical rectangle due to the many sides it's coming into contact with. Hence a piece of code I want ran only once is instead ran multiple times.
Is there anyway to make the collision detection only function for the first contact so that it doesn't repeatedly call?
I'm aware of setting the categoryBitMask to 0 however doing so renders the boat unable to be hit by anything else- something I don't want. Additionally I considered using a boolean variable to only run the code once whilst the boat is passing through, however as previously mentioned, due to the different sides, the code think it's passed through when in fact it's just hitting another side of the boat.
I don't have much code to show here other than setting physics bodies so I don't think it's necessary, but inside didBeginContact() I'm using a categoryBitMask comparison to check for collisions.
I may have not explained this very well, in which case I apologize, my English is not the best!
If anyone can help, it'd be much appreciated, thanks.
You can combine the individual physics bodies of your ship into a single body with
+ (SKPhysicsBody *)bodyWithBodies:(NSArray *)bodies
and then test for contacts with the composite body instead of the individual components.
If you are subclassing your node then simply create a BOOL property for it.
BOOL alreadyHit;
If you are not subclassing, use the SKNode's userData dictionary and create an entry for the same.

setting starting point of state before any move and the move path

i am trying to learn optaplanner. i went over the docs and examples. taking the nqueen example, i am trying to change a little bit the problem by replacing it to knight instead of queen.
i want to set the first knight in fixed position, like col0#row0. this should be the starting point without changing it. how can i do that?
i want to specify the legal move path of a knight (more complex from the queen) - where is the best place to implement it? to specify it on a custom move? on the knight object itself?
appreciate any assistance
Don't confuse a "chess move" with a "Local Search move". A chess move signals the valid places a chess piece can attack. A Local Search move is a way to go from one Solution state to another Solution state by changing 1 or more variables.
To change the Queens into a Knights, simply take the nqueens example and adjust the scoreRules.drl so it's a hard constraint match if 2 chess pieces are a knight's pattern away from each other.
Notice that with those changes, the Local Search move will still simply change the row of a Knight. Also, unlike in nqueens, the column could become a planning variable too, because multiple Knights can be on the same column without attacking each other.

How to design this particular finite state machine?

I am trying to get my head around how to design the following system, which I think can be defined as a finite state machine:
Say we have a pile of 16 building blocks (towers, walls, gates) together forming a castle. The player can drag the blocks to 16 places on a floorplan and if done right they will see the whole castle. All towers (there's four of them) are equal so they can go on any of the four corners. Same goes for some of the walls.
All in all there are 16 spots on the floorplan where you can put a building block and each of the spots can have 17 "states": empty + either one of the 16 building blocks. Doing some maths this leads to 17^16=a LOT of combinations.
The program starts with an empty floorplan and a pile of building blocks. It should then show a message like "build your own castle, start with the tower". When the user places a tower correctly, it should say "well done, now build all four towers". You get the idea.
Problem is: there are so many things a player can do. Put a block at the wrong place, remove a block, correctly put walls or towers all over the floorplan ignoring the directions given to them, etc.
It would be awesome if I could avoid having to use thousands of if-then statements to decide wether I should take the next step, show an error message or go back to the previous step based on what the player is doing.
How would you describe the NEXT, PREVIOUS and ERROR conditions for every step of the building sequence? Are there any design methods for this? Thanks a lot for your input.
Try to do this declaratively. Define an enum (or possibly classes) describing the kinds of blocks. Define and construct a 4x4 2D array describing the sets of permissible kinds of blocks in each position (implement the sets as lists, bitfields, whatever suits you best). Whenever a player tries to place a block in a position, check whether it is permissible against the 2D array. If you want to have particular messages for a position being correctly filled in, also put those in the same an array.
I don't know if a FSM is really what you are after: what kinds of sequencing constraints are you looking to verify? Does it matter whether towers are built first? From the rest of your description, it sounds like the above goal state description would be more suitable.