Commit 8c8d5d58 authored by Radia EL HAMDOUNI's avatar Radia EL HAMDOUNI

added basic legal moves for pieces

parent f32479bd
/* Move generation */
int legal_move(int sq, int side){
if(!side) // white side
{
// moving pawns
if( (sq+17 & 0x88)==0 && board[sq+17] == P )
{
return 1;
}
if( (sq+15 & 0x88)==0 && board[sq+15] == P )
{
return 1;
}
}
else{
if( (sq-17 & 0x88)==0 && board[sq-17] == p )
{
return 1;
}
if( (sq-15 & 0x88)==0 && board[sq-15] == p )
{
return 1;
}
}
// moving knights
for (int i = 0; i < 8; i++)
{
int target_square = sq + moves_knight[i];
int target_piece = board[target_square];
if (!(target_square & 0x88))
{
if (!side ? target_piece == N : target_piece == n)
return 1;
}
}
// moving bishop
for (int i = 0; i < 4; i++)
{
int target_square = sq + moves_bishop[i];
while (!(target_square & 0x88))
{
int target_piece = board[target_square];
if (!side ? (target_piece == B || target_piece == Q) : (target_piece == b || target_piece == q))
return 1;
if (target_piece)
break;
target_square += moves_bishop[i];
}
}
// Moving king
for (int i = 0; i < 8; i++)
{
int target_square = sq + moves_king[i];
int target_piece = board[target_square];
if (!(target_square & 0x88))
{
if (!side ? target_piece == K : target_piece == k)
return 1;
}
}
// moving the queen and the rook
for (int i = 0; i < 4; i++)
{
int target_square = sq + moves_rook[i];
while (!(target_square & 0x88))
{
int target_piece = board[target_square];
if (!side ? (target_piece == R || target_piece == Q) : (target_piece == r || target_piece == q))
return 1;
if (target_piece)
break;
target_square += moves_rook[i];
}
}
return 0;
}
void print_possible_moves(int side){
for(int i = 0 ; i<8 ; i++){//loop over the ranks
for(int j=0;j<16;j++ ){// loop over the files
int square = 16 * i + j;
if(j == 0){
printf("%d " , 8-i); // print ranks
}
// print the board's valid quares
if(!(square & 0x88)){
printf(" %c " ,legal_move(square,side) ? 'x' : '.');
}
}
printf("\n");
}
printf("\n a b c d e f g h \n\n\n"); // print files
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment