|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Checkmate algorithm
I've made a 2 player chess game to be played over tcp/ip. i saved the best(and hardest) part for last, writing the algorithm that finds the "check" status. as of now, one player must physically "kill" the opposing player's king(run him over and take the square) in order to win. but i would like to have the computer give a "check" warning, or the "checkmate" message. i've been pseudo coding, and this is what i have so far:
the algorithm is run on the player who has just theoretically been put in check, not the attacking player. determine if king is "in check" - start at king's location and using the rulesets for the movement of pieces, step thru each possible line of attack for each piece. i.e. - for rook: check all the x/y values moving away from the king until either a) a friendly piece is encountered or b) the enemy rook is-thus signifying check or c) end of board -repeat this for each possible piece if in check: determine where there are open spaces to move to, then run each of these spaces thru the above algorithm recursively(i think) if none of those spaces are valid: can one of my own pieces sacrifice their life for me and block teh opposing piece? -- i have a pretty good grasp on the first two steps, but the last one causes my brain to stop functioning. am i going about this the right way? anyone have any suggestions/comments? i remember someone in read had this cool link in their signature all about this, but i cant remember who u r!!! |
|
#2
|
||||
|
||||
|
I'm probably reading into your first step wrong, but it sounds like you're checking all the enemy pieces on the board to see if they can reach the king, instead of just checking what pieces have a path to the king and seeing if that path is a valid move.
For example, from the king's position, check all paths extending from him and then check if end of board, friendly player, or enemy player. If you reach an enemy then check valid move rules. Then you just need to handle the special case of knights since they can jump over pieces and have unorthodox movement. Like I said, this maybe exactly what you were describing but if not maybe this can help. In regards to the third step, an initial thought (there's probably a better method) would be to put all paths for each piece that has the king in check into a list and then follow each square along each path and see if a friendly piece can move into that spot. (using an algorithm similar to finding out if your king is in check to). Then check if your friendly move will open up a path that will leave your king open. Just some thoughts, not sure how viable they are. -b
__________________
PostgreSQL, it's what's for dinner... |
|
#3
|
||||
|
||||
|
^^heh, yes what u were describing in the first part is exactly what i was trying to explain.... my explanatory skills are lacking, my bad.
as for the second part, that's kind of along the lines of what i was brainstorming. my problem is that as of now, i dont keep track of where all teh pieces are since i previously had no need for any intelligence on the computer's side. i guess i will have to go back and add some methods/members to track at least the friendly pieces. or else i would have to loop thru the whole board to find all the friendly pieces, which would be a pain i think. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > Checkmate algorithm |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|