Sign in to follow this  
Araxon

Chess rules problem

Recommended Posts

Araxon    122
Hello, i am coding a chess engine, for now i am just trying to make a simple console application that can apply correctly the chess rules everything works fine but i have some problems with the castling checking function... the board is implementer in the 0x88 way which means that i have a 128 elements array... can any1 help me please with the castling function without redirecting me to an allready made chess engine ? thanx ...

Share this post


Link to post
Share on other sites
Think128    124
I'm not sure by what you mean by castling checking and I don't know what a 0x88 method is. But if you mean checking to make sure neither the rooks or king have moved then that could be done with a boolean flag and if statements.

If you wanted to test the spacial capability then I would use this:



//function checks if a castle can occur assuming
//neither rook or king have moved
bool CastleSpatialCheck(piece ROOK) { //ROOK is the specific rook being used

//the x coord being tested, y is a constant so it does not need to be tested
int testingx;

//need this so you know which side of the board you are on
int increment;

//if the rook is on the left then the increment is positive
//else the rook is on the right and the increment is negative
if (ROOK.x == 0) increment = 1;
else increment = -1;

//the meat of the function
//this loop continues untill it encounters a king
//if the space tested is occupied or threatened the entire
//function will return false
for (testingx = (ROOK.x + increment); ; testingx += increment) {

//a check function for occupied is one I assume you already have
//or at least have a method for it
//my use of it requires it to return the value of the piece
//occupying the square
if (Occupied(testingx, ROOK.y) == KING) break;

//if the square is occupied by anything besides a king
//return false
if (Occupied(testingx, ROOK.y)) return false;

//the threatened rule is a stipulation to castling
//again Threatened(int x, int y) is a function I assume you
//already have, or at least a version/method of
if (Threatened(testingx, ROOK.y)) return false;

}

//if all spaces between the rook and king are unoccupied
//and not threatened then return true
return true;
}



Hope that helps somewhat. Sorry if the code is unclear, I suck at commenting ;)

Share this post


Link to post
Share on other sites
Araxon    122
Thanx for the reply but that not what i am searching .. :(

board representation is like that
char board[128] = {
4, 8, 16, 32, 64, 16, 8, 4, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1,
5, 9, 17, 33, 65, 17, 9, 5, 1, 1, 1, 1, 1, 1, 1, 1
};

I have allready implemented a function thats checks if a king in a specific square of the board is in check.
And a function that creates an 8 element array with the nearest non empty squares in all eight directions of a piece

my way of dealing with castling is such as follows
for white's little castling ( castling to the right)

check if white king and white right rook has moved
if NOT then check if the first piece to the right is the rook
if TRUE then check if board[4], board[5], board[6] are not in check.
if NOT then return move is legal

And thats just for 1 of 4 cases which i must code seperately ......:(

i find this way really inefficient and ask for help !! plz anyone.. !

Share this post


Link to post
Share on other sites
uckevin111    190
I can't see any other way to do it more efficiently, but I'm not an expert :).

One other thing you need to check is that that king is not already in check and that the king will not move into check. I am assuming you are looking from behind the white king towards the black pieces, so for the rook on the right you would need to check 4 squares to make sure they are not in check, the square with the king, the two between the king and rook, and the square the rook is on. I think you are checking the king and the two squares between but not the square the rook is on, if you are numbering the squares from the bottom left. In any case you are missing one of the 4 squares since you are only checking 3 in that case.

- Kevin

Edit: You don't have to code them separately though. You could create a function that takes the position of the king and the position of the rook and would check to make sure they haven't moved, would make sure the squares between them are open, would make sure they are not in check and the squares between them are not in check, and would return a true or false for a legal move. That would at least save you on code.

Share this post


Link to post
Share on other sites
Araxon    122
yes thanx for the check i have forgot that :D

well the problem is that i prompt the user to insert a from and a to square therefore i need to check in which side i will castle :S hmm an other problem that i face is that i think my in check function is not really efficient if anyone has the time to check it out and help me :)

btw how can i make this text box for inserting code????


//The array board is a global variable.

//swap function just swaps from 1 to 0 (CAN I DO IT WITH A BITWISE OPERATION?)

/*function takes square to move, destination square and side to move(0 white, 1 black)*/

char checkCheck(char from, char to, char side)
{
char i,k,temp1,temp2,tmpKingPos;
/*temporary variable if the move i not valid then original values must be restored.*/
temp1 = board[from];
temp2 = board[to];
board[to] = board[from];
board[from] = 0;
/*end of temporary variables

/* the variable that holds the king position must also be help in a temporary variable if the king is to be move*/
if (kingPos[side] == from)
tmpKingPos = to;
else
tmpKingPos = kingPos[side];
/*call to the function at the end of the post.
updateKingPerimeter(side, tmpKingPos);

/* the king perimeter array is a 16 element array the first 0-7 for white 8-15 for black*/
if (!side) k = 0;
else k = 8;

//Check if a piece checks the king, does not check horses.
for( i = 0; i < 8; i++)
{
/*if there is a piece blocking the king*/
if (!(kingBlockers[i + k] & 128))
{
/*check if it's legal for a piece that blocks the king perimeter to attack him*/
if (isLegal(kingBlockers[i + k], tmpKingPos, (swap(side))))
{
board[from] = temp1;
board[to] = temp2;
return 0; /* the king is checked
}
}
}

//Checks if a horse checks the king.
for( i = 0; i < 8; i++)
{
if (!((tmpKingPos + horseMv[i]) & 0x88))
{
if (board[tmpKingPos + horseMv[i]] == (8 + (swap(side))))
{
board[from] = temp1;
board[to] = temp2;
return 0;
}
}
}
board[from] = temp1;
board[to] = temp2;
return 1; //no check
}

// Set up king perimeter 0 - 7 is for the white king 8 - 15 is for the black king
void updateKingPerimeter(char side, char tmpKingPos)
{
char i,k;
if (side) k = 8;
else k = 0;
for( i = 0; i < 8; i++)
{
kingBlockers[i + k] = tmpKingPos;

do
{
kingBlockers[i + k] += kingMv[i];
if (((kingBlockers[i + k]) & 0x88))
{
kingBlockers[i + k] = 128;
break;
}
}
while (!(board[kingBlockers[i + k]]));
}
}

Share this post


Link to post
Share on other sites
DeepButi    140
Quote:
Original post by uckevin111
you would need to check 4 squares to make sure they are not in check, the square with the king, the two between the king and rook, and the square the rook is on

Sorry but your rule is wrong. The only squares to be checked are the king actual position and squares the king will travel along including destinity, but NOT the rook actual position, neither in long castle the rook's adjacent square. Allways 3 squares, never 4 neither 5.

Think128 code is wrong also checking unnecesary rook adjacent square in long castle.

Quote:
Original post by Araxon
//swap function just swaps from 1 to 0 (CAN I DO IT WITH A BITWISE OPERATION?)

yes you can, use XOR (exclusive OR):

// To change it
side^=1;

// in your code
if (board[tmpKingPos + horseMv[i]] == (8 + (side^1)))




Hope it helps

[Edited by - DeepButi on February 15, 2005 6:25:27 AM]

Share this post


Link to post
Share on other sites
uckevin111    190
Quote:
Original post by DeepButi
Sorry but your rule is wrong. The only squares to be checked are the king actual position and squares the king will travel along including destinity, but NOT the rook actual position, neither in long castle the rook's adjacent square. Allways 3 squares, never 4 neither 5.


My rule is correct. I am not saying to check the square the rook is going to, I am saying you have to make sure the square the rook resides on (before the actual castling) is not in check. You must check the square the king is on currently (before castling) because you cannot castle out of check. You must check the squares between the rook and the king to make sure they are not in check because you cannot castle through check (there are 2 or 3 squares, depending on which side the rook is on). You also must check the square the rook currently is on because you cannot move your king into check. So that would be a total of 4 or 5 squares, depending on which rook it is.

- Kevin

Share this post


Link to post
Share on other sites
Kramy    126
Uhh, I'm not sure if this has any importance at all, but 0x88 in hexadecimal is 136, not 128. 0x80 is 128. And yes, I'm one of those crazy people that can add in hexadecimal, octal, and binary. :P

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by uckevin111
You also must check the square the rook currently is on because you cannot move your king into check.


That doesn't follow; the king does not end up on the space where the rook used to be. It simply moves two spaces towards the rook.

Share this post


Link to post
Share on other sites
DeepButi    140
Quote:
Original post by uckevin111
You must check the squares between the rook and the king to make sure they are not in check because you cannot castle through check (there are 2 or 3 squares, depending on which side the rook is on).

No. The king allways moves two squares, no matter wich castle you do.

Quote:
Original post by uckevin111
You also must check the square the rook currently is on because you cannot move your king into check. So that would be a total of 4 or 5 squares, depending on which rook it is.

No and no. The king never goes to where the rook is, no need to check it.

Check oficial rules anywhere you like please. Allways 3 squares to verify:

* actual king's position
* square the king will cross
* final square

The fact that rook is actually under attack is irrelevant
In long castle, the fact that the square adjacent to rook is under attack is irrelevant.

For short castle:
R..K -> .KR. -> NCCC (C means must be checked, N means it doesn't matter)

For long castle:
R...K -> ..KR. -> NNCCC

Hope it helps

Share this post


Link to post
Share on other sites
uckevin111    190
LOL... I was way off. Ok, sorry guys, you are right. For some reason I was thinking the king moved to where the rook was. I haven't played chess in years!

Thanks for explaining. Once you guys reminded me the king only moves 2 squares it all made sense :).

- Kevin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this