954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

HELP in chess project in C

Hi! I'm making a chess program, that need to move pieces on the chess board, get an input,check if the move is valid and print the output. The compiler finds some errors that i don't understand...I need some help with the pieces functions. Plz help me fix them!!!
my attempts :


//Chess project//


#include
#include

using namespace std;
#include
#include
#include
// all figures are represented as a constant integers//
//constants
// all columns are presented as an integers
#
int board[8][8];//chess board is presented as a 2D array of int
#define empty 0
#define pawn 1
#define rook 2
#define knight 3
#define bishop 4
#define queen 5
#define king 6

//functions declaration
int char_to_int(char c);
void setup_board();
void print_board();
void get_moove(int x1,int y1,int x2,int y2);
int check_if_empty(int x2, int y2);
int pawn_moove( int x1,int x2,int y1, int y2) ;
int rook_moove(int x1,int x2,int y1, int y2);
int knight_moove(int x1,int x2,int y1, int y2);
int bishop_moove(int x1,int x2,int y1, int y2);
int queen_moove(int x1,int x2,int y1, int y2);
int king_moove(int x1,int x2,int y1, int y2);
void update_board(int x1,int y1,int x2,int y2);
char print_piece(int x, int y);
void print_board();

void setup_board()
{
//Normal board setup
board[0][0]=rook;
board[0][1]=knight;
board[0][2]=bishop;
board[0][3]=queen;
board[0][4]=king;
board[0][5]=bishop;
board[0][6]=knight;
board[0][7]=rook;
board[1][0]=pawn;
board[1][1]=pawn;
board[1][2]=pawn;
board[1][3]=pawn;
board[1][4]=pawn;
board[1][5]=pawn;
board[1][6]=pawn;
board[1][7]=pawn;
board[2][0]=empty;
board[2][1]=empty;
board[2][2]=empty;
board[2][3]=empty;
board[2][4]=empty;
board[2][5]=empty;
board[2][6]=empty;
board[2][7]=empty;
board[3][0]=empty;
board[3][1]=empty;
board[3][2]=empty;
board[3][3]=empty;
board[3][4]=empty;
board[3][5]=empty;
board[3][6]=empty;
board[3][7]=empty;
board[4][0]=empty;
board[4][1]=empty;
board[4][2]=empty;
board[4][3]=empty;
board[4][4]=empty;
board[4][5]=empty;
board[4][6]=empty;
board[4][7]=empty;
board[5][0]=empty;
board[5][1]=empty;
board[5][2]=empty;
board[5][3]=empty;
board[5][4]=empty;
board[5][5]=empty;
board[5][6]=empty;
board[5][7]=empty;
board[6][0]=pawn;
board[6][1]=pawn;
board[6][2]=pawn;
board[6][3]=pawn;
board[6][4]=pawn;
board[6][5]=pawn;
board[6][6]=pawn;
board[6][7]=pawn;
board[7][0]=rook;
board[7][1]=knight;
board[7][2]=bishop;
board[7][3]=queen;
board[7][4]=king;
board[7][5]=bishop;
board[7][6]=knight;
board[7][7]=rook;
}

int main()
{
char c;//to conert from letter to int
int x,y;
int x1,x2;//for column
int y1,y2;//for row

setup_board();


printf("**********************************************************\n");
printf("Hi. Wellcome to Kirills simple chess program!!!\n");

printf("Type 'Q' to to Quit\n");
print_board();
while(1)

printf("Please select a starting position for a figure for instance E,2,in upper case\n");

scanf("%c, %d",&c,&y);
x1=char_to_int(c);
if(c=='Q')//Exit chess
{
printf("Thank you ang goodbye\n");
return 0;
}
y1=y-1;
printf("Please select a finishing position for a figure for instance E,4");
scanf("%c, %d",&c,&y);
x2=char_to_int( c);
y2=y-1;

get_moove(x1,y1,x2,y2);
return 0;
}
//Fuctions definitions
int char_to_int(char c)
{

int x;
if(c=='Q')
{
printf("BYE BYE\n");
}
else if((c!='A')||(c!='B')||(c!='C')||(c!='D')||(c!='E')||(c!='F')||(c!='G')||(c!='H'))
{
printf("Sorry wrong column selection. Re enter.\n");
return -1 ;
}
else
x=c-65;//A will be converted to 0, B to 1, etc...
return x;
}
char print_piece(int x, int y)//converts number from the board to piece character
{
char piece = ' ';
switch(board[x][y])
{
case pawn:
piece='P';
break;
case rook:
piece='R';
break;
case knight:
piece='N';
break;
case bishop:
piece='B';
break;
case queen:
piece='Q';
break;
case king:
piece='K';
break;
default:
piece=' ';
break;
}

return piece;
}
void print_board()
{
char a8=print_piece(0,0), b8=print_piece(0,1), c8=print_piece(0,2), d8=print_piece(0,3),e8=print_piece(0,4), f8=print_piece(0,5), g8=print_piece(0,6), h8=print_piece(0,7);
char a7=print_piece(1,0), b7=print_piece(1,1), c7=print_piece(1,2), d7=print_piece(1,3),e7=print_piece(1,4), f7=print_piece(1,5), g7=print_piece(1,6), h7=print_piece(1,7);
char a6=print_piece(2,0), b6=print_piece(2,1), c6=print_piece(2,2), d6=print_piece(2,3),e6=print_piece(2,4), f6=print_piece(2,5), g6=print_piece(2,6), h6=print_piece(2,7);
char a5=print_piece(3,0), b5=print_piece(3,1), c5=print_piece(3,2), d5=print_piece(3,3),e5=print_piece(3,4), f5=print_piece(3,5), g5=print_piece(3,6), h5=print_piece(3,7);
char a4=print_piece(4,0), b4=print_piece(4,1), c4=print_piece(4,2), d4=print_piece(4,3),e4=print_piece(4,4), f4=print_piece(4,5), g4=print_piece(4,6), h4=print_piece(4,7);
char a3=print_piece(5,0), b3=print_piece(5,1), c3=print_piece(5,2), d3=print_piece(5,3),e3=print_piece(5,4), f3=print_piece(5,5), g3=print_piece(5,6), h3=print_piece(5,7);
char a2=print_piece(6,0), b2=print_piece(6,1), c2=print_piece(6,2), d2=print_piece(6,3),e2=print_piece(6,4), f2=print_piece(6,5), g2=print_piece(6,6), h2=print_piece(6,7);
char a1=print_piece(7,0), b1=print_piece(7,1), c1=print_piece(7,2), d1=print_piece(7,3),e1=print_piece(7,4), f1=print_piece(7,5), g1=print_piece(7,6), h1=print_piece(7,7);

printf(" +---+---+---+---+---+---+---+---+");
printf("\n");
printf("8 | %c | %c | %c | %c | %c | %c | %c | %c ",a8,b8,c8,d8,e8,f8,g8,h8);
printf("\n");
printf(" +---+---+---+---+---+---+---+---+");
printf("\n");
printf("7 | %c | %c | %c | %c | %c | %c | %c | %c ",a7,b7,c7,d7,e7,f8,g7,h7);
printf("\n");
printf(" +---+---+---+---+---+---+---+---+");
printf("\n");
printf("6 | %c | %c | %c | %c | %c | %c | %c | %c ",a6,b6,c6,d6,e6,f6,g6,h6);
printf("\n");
printf(" +---+---+---+---+---+---+---+---+");
printf("\n");
printf("5 | %c | %c | %c | %c | %c | %c | %c | %c ",a5,b5,c5,d5,e5,f5,g5,h5);
printf("\n");
printf(" +---+---+---+---+---+---+---+---+");
printf("\n");
printf("4 | %c | %c | %c | %c | %c | %c | %c | %c ",a4,b4,c4,d4,e4,f4,g4,h4);
printf("\n");
printf(" +---+---+---+---+---+---+---+---+");
printf("\n");
printf("3 | %c | %c | %c | %c | %c | %c | %c | %c ",a3,b3,c3,d3,e3,f3,g3,h3);
printf("\n");
printf(" +---+---+---+---+---+---+---+---+");
printf("\n");
printf("2 | %c | %c | %c | %c | %c | %c | %c | %c ",a2,b2,c2,d2,e2,f2,g2,h2);
printf("\n");
printf(" +---+---+---+---+---+---+---+---+");
printf("\n");
printf("1 | %c | %c | %c | %c | %c | %c | %c | %c ",a1,b1,c1,d1,e1,f1,g1,h1);
printf("\n");
printf(" +---+---+---+---+---+---+---+---+");
printf("\n");
printf(" A B C D E F G H ");
}
void get_moove(int x1,int y1,int x2,int y2)
{
int choice;
choice=board[x1][y1];

switch(choice)
{
case 1:pawn_moove(x1,y1,x2,y2);
break;
case 2:rook_moove(x1,y1,x2,y2);
break;
case 3:knight_moove(x1,y1,x2,y2);
break;
case 4:bishop_moove(x1,y1,x2,y2);
break;
case 5:queen_moove(x1,y1,x2,y2);
break;
case 6:king_moove(x1,y1,x2,y2);
break;
default:
printf("Wrong selection of start/finish combination\n");
break;
}
}

//Function's definitions
int check_if_empty(int x2, int y2)//if board[x2][y2]=0 it's empty
{
if(board[x2][y2]> 0)
{
printf("The place is not empty\n");
return 0;
}
else
return 1;
}
void update_board(int x1,int x2,int y1, int y2)
{
int temp=0;
board[x2][y2]=board[x1][y1];
board[x1][y1]=0;
return;
}

int pawn_moove(int x1,int x2,int y1, int y2)
{
int dx,dy;
dx=abs(x2-x1);
dy=abs(y2-y1);
if((dx!=0)&&(dy>2))
{
printf("Cannot moove- the moove is illegall!\n");
return 0;
}
else
{
if((check_if_empty(x1,y1+1)||(check_if_empty(x2,y2))))
{
update_board(x1,y1,x2,y2);
print_board();
return 1;
}
}
}
int rook_moove(int x1,int x2,int y1, int y2)
{
int dx,dy;
int i,j;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(((dx!=0)&&(dy=0))||((dy!=0)&&(dx=0)))
{
printf("Cannot moove- the m oove is illegall!\n");
return 0;
}
else
{
for(i=x1+1;i

themenucha
Newbie Poster
19 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

*POST YOUR CODE USING CODE TAG
*FORMAT YOUR CODE
*POST THE ERRORS THAT THE COMPILER IS GIVING YOU

while(1) You need to put the opening braceif(check_if_empty(x2,y2)); What is this for?for(i=x1+1,j=y1+1;i Semicolon between two conditions -wrong !!!

There will be many more I suppose. Please follow the above three things, you will get your answers faster

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

Also , your entire code of setup_board() can be re written much efficiently using a for loop.

So many more... please do the formatting

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

What do you mean under formatting?
check_if_empty checks ifthe final position is not occupied, if its >0(occupied) it returns 0.
Actually I did the CODE mark, i don't know why isn't it working.
Those are compilation errors(VS6):


c:\users\kirill.avital\desktop\avital\chess\kirill_chess.cpp(110) : warning C4101: 'x' : unreferenced local variable
c:\users\kirill.avital\desktop\avital\chess\kirill_chess.cpp(325) : error C2181: illegal else without matching if
c:\users\kirill.avital\desktop\avital\chess\kirill_chess.cpp(345) : error C2143: syntax error : missing ')' before '!'
c:\users\kirill.avital\desktop\avital\chess\kirill_chess.cpp(345) : error C2059: syntax error : ')'
c:\users\kirill.avital\desktop\avital\chess\kirill_chess.cpp(345) : error C2059: syntax error : ')'
c:\users\kirill.avital\desktop\avital\chess\kirill_chess.cpp(346) : error C2143: syntax error : missing ';' before '{'
c:\users\kirill.avital\desktop\avital\chess\kirill_chess.cpp(350) : error C2181: illegal else without matching if
c:\users\kirill.avital\desktop\avital\chess\kirill_chess.cpp(381) : error C2143: syntax error : missing ';' before 'return'
c:\users\kirill.avital\desktop\avital\chess\kirill_chess.cpp(386) : error C2601: 'queen_moove' : local function definitions are illegal
c:\users\kirill.avital\desktop\avital\chess\kirill_chess.cpp(442) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

Kirill_chess.obj - 9 error(s), 1 warning(s)
thANX AGAIN

themenucha
Newbie Poster
19 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

By formatting , I mean something like this

if(c=='Q')
	{
            printf("BYE BYE\n");
	}
	else if((c!='A')||(c!='B')||(c!='C')||(c!='D')||(c!='E')||(c!='F')||(c!='G')||(c!='H'))
	{
		printf("Sorry wrong column selection. Re enter.\n");
		return -1 ;
	}
	else
		x=c-65;//A will be converted to 0, B to 1, etc...
DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 
error C2143: syntax error : missing ';' before 'return'


There must be a semicolon on line 380 after print_board()syntax error : missing ')' before '!'
There is nothing like !< . Use > (greater than) or >= whichever is appropriate

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 
(325) : error C2181: illegal else without matching if


Now this is how formatting would help you. You can clearly see one else is misplaced

int rook_moove(int x1,int x2,int y1, int y2)
	{
		int dx,dy;
		int i,j;
		dx=abs(x2-x1);
		dy=abs(y2-y1);
		if(((dx!=0)&&(dy=0))||((dy!=0)&&(dx=0)))
		{
			printf("Cannot moove- the m oove is illegall!\n");
			return 0;
		}
		else
		{
			for(i=x1+1;i<x2;i++)
			{
				if(check_if_empty(i,y2))
				{
					update_board(x1,y1,x2,y2);
					print_board();
					return 1;
				}
			}
		}
		else
		{
			for(j=y1+1;j<y2;j++)
			{
				if(check_if_empty(x1,j))
				{

					update_board(x1,y1,x2,y2);
					print_board();
					return 1;
				}
			}
		}
	}


More specifically , this is the else else
{
for(j=y1+1;j

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

What do you mean by misplaced?I tried to recompile but there's "expected primary expression before else"...Both elses have the same structire....

themenucha
Newbie Poster
19 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Now this is how formatting would help you. You can clearly see one else is misplaced

int rook_moove(int x1,int x2,int y1, int y2)
	{
		int dx,dy;
		int i,j;
		dx=abs(x2-x1);
		dy=abs(y2-y1);
		if(((dx!=0)&&(dy=0))||((dy!=0)&&(dx=0)))
		{
			printf("Cannot moove- the m oove is illegall!\n");
			return 0;
		}
		else
		{
			for(i=x1+1;i<x2;i++)
			{
				if(check_if_empty(i,y2))
				{
					update_board(x1,y1,x2,y2);
					print_board();
					return 1;
				}
			}
		}
		else
		{
			for(j=y1+1;j<y2;j++)
			{
				if(check_if_empty(x1,j))
				{

					update_board(x1,y1,x2,y2);
					print_board();
					return 1;
				}
			}
		}
	}

More specifically , this is the else

You can't use 2 else statements after an if statement, line 12 should be an else if statement or another if statement after the first else, etc.

I think he just didn't notice it :)

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
I think he just didn't notice it


possibly yes... :)

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

[TEX]Hi!This Is My Code after formatting and compiling- still need some help in main function.

//Chess project//


#include <cstdlib>
#include <iostream>

using namespace std;
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
// all figures are represented as a constant integers//
//constants
// all columns are presented as an integers
#
 int board[8][8];//chess board is presented as a 2D array of int


//functions declaration
int char_to_int(char c);
void setup_board();
void print_board();
int  check_if_empty(int x2, int y2);
int pawn_moove( int x1,int x2,int y1, int y2) ;
int rook_moove(int x1,int x2,int y1, int y2);
int knight_moove(int x1,int x2,int y1, int y2);
int bishop_moove(int x1,int x2,int y1, int y2);
int queen_moove(int x1,int x2,int y1, int y2);
int king_moove(int x1,int x2,int y1, int y2);
void update_board(int x1,int y1,int x2,int y2);
char print_piece(int x, int y);
void print_board();

void setup_board()
{
     //Normal/Initial board setup
   board[0][0]=2;
   board[0][1]=3;
   board[0][2]=4;
   board[0][3]=5;
   board[0][4]=6;
   board[0][5]=4;
   board[0][6]=3;
   board[0][7]=2;
   board[1][0]=1;
   board[1][1]=1;
   board[1][2]=1;
   board[1][3]=1;
   board[1][4]=1;
   board[1][5]=1;
   board[1][6]=1;
   board[1][7]=1;
   board[2][0]=0;
   board[2][1]=0;
   board[2][2]=0;
   board[2][3]=0;
   board[2][4]=0;
   board[2][5]=0;
   board[2][6]=0;
   board[2][7]=0;
   board[3][0]=0;
   board[3][1]=0;
   board[3][2]=0;
   board[3][3]=0;
   board[3][4]=0;
   board[3][5]=0;
   board[3][6]=0;
   board[3][7]=0;
   board[4][0]=0;
   board[4][1]=0;
   board[4][2]=0;
   board[4][3]=0;
   board[4][4]=0;
   board[4][5]=0;
   board[4][6]=0;
   board[4][7]=0;
   board[5][0]=0;
   board[5][1]=0;
   board[5][2]=0;
   board[5][3]=0;
   board[5][4]=0;
   board[5][5]=0;
   board[5][6]=0;
   board[5][7]=0;
   board[6][0]=1;
   board[6][1]=1;
   board[6][2]=1;
   board[6][3]=1;
   board[6][4]=1;
   board[6][5]=1;
   board[6][6]=1;
   board[6][7]=1;
   board[7][0]=2;
   board[7][1]=3;
   board[7][2]=4;
   board[7][3]=5;
   board[7][4]=6;
   board[7][5]=4;
   board[7][6]=3;
   board[7][7]=2;
}

  int main()
{
      char c;//to convert from letter to int
      int x,y;
	int x1,x2;//for column
	int y1,y2;//for row

	setup_board();



        printf("**********************************************************\n");
		printf("Hi. Wellcome to Kirill Zemenucha's simple chess program!!!\n");

		printf("Type 'Q' to to Quit\n");
		 print_board();
		while(1)
		{


		printf("\nPlease select a starting position for a figure for instance E2  in upper case\n");

		scanf("%1s%d",&c,&y);
		if(c=='Q')//Exit chess
		                  {
                          printf("Thank you and goodbye\n");
                          return 0;
                          }
        else
        x1=char_to_int(c);
		y1=y-1;
		printf("Please select a finishing position for a figure for instance E,4\n");
		scanf("%c,%d",&c,&y);
		if(c=='Q')//Exit chess
		                  {
                          printf("Thank you and goodbye\n");
                          return 0;
                          }
		x2=char_to_int(c);
		y2=y-1;



      int choice;
      choice=board[x1][y1];

      switch(choice)
      {
                    case 1:pawn_moove(x1,y1,x2,y2);
                    break;
                    case 2:rook_moove(x1,y1,x2,y2);
                    break;
                    case 3:knight_moove(x1,y1,x2,y2);
                    break;
                    case 4:bishop_moove(x1,y1,x2,y2);
                    break;
                    case 5:queen_moove(x1,y1,x2,y2);
                    break;
                    case 6:king_moove(x1,y1,x2,y2);
                    break;
                    default:
                            printf("Wrong selection of start/finish combination\n");
                            break;

          }


		return 0;
}
}
    //Fuctions definitions
    int char_to_int(char c)
    {

        int x=0;
       if( c < 'I')
                  {
                       x=c-65;//A will be converted to 0, B to 1, etc...
                       return x;
                   }
                   else
                   {
                       printf("NOT in range!!!\n");
                   }
                                      return -x;
    }
char print_piece(int x, int y)//converts number from the board to piece character
{
   char piece = ' ';
   switch(board[x][y])
   {
      case 1:
         piece='P';
         break;
      case 2:
         piece='R';
         break;
      case 3:
         piece='N';
         break;
      case 4:
         piece='B';
         break;
      case 5:
         piece='Q';
         break;
      case 6:
         piece='K';
         break;
      default:
         piece=' ';
         break;
   }

   return piece;
}
void print_board()
{
char a8=print_piece(0,0), b8=print_piece(0,1), c8=print_piece(0,2), d8=print_piece(0,3),e8=print_piece(0,4), f8=print_piece(0,5),  g8=print_piece(0,6), h8=print_piece(0,7);
char a7=print_piece(1,0), b7=print_piece(1,1), c7=print_piece(1,2), d7=print_piece(1,3),e7=print_piece(1,4), f7=print_piece(1,5),  g7=print_piece(1,6), h7=print_piece(1,7);
char a6=print_piece(2,0), b6=print_piece(2,1), c6=print_piece(2,2), d6=print_piece(2,3),e6=print_piece(2,4), f6=print_piece(2,5),  g6=print_piece(2,6), h6=print_piece(2,7);
char a5=print_piece(3,0), b5=print_piece(3,1), c5=print_piece(3,2), d5=print_piece(3,3),e5=print_piece(3,4), f5=print_piece(3,5),  g5=print_piece(3,6), h5=print_piece(3,7);
char a4=print_piece(4,0), b4=print_piece(4,1), c4=print_piece(4,2), d4=print_piece(4,3),e4=print_piece(4,4), f4=print_piece(4,5),  g4=print_piece(4,6), h4=print_piece(4,7);
char a3=print_piece(5,0), b3=print_piece(5,1), c3=print_piece(5,2), d3=print_piece(5,3),e3=print_piece(5,4), f3=print_piece(5,5),  g3=print_piece(5,6), h3=print_piece(5,7);
char a2=print_piece(6,0), b2=print_piece(6,1), c2=print_piece(6,2), d2=print_piece(6,3),e2=print_piece(6,4), f2=print_piece(6,5),  g2=print_piece(6,6), h2=print_piece(6,7);
char a1=print_piece(7,0), b1=print_piece(7,1), c1=print_piece(7,2), d1=print_piece(7,3),e1=print_piece(7,4), f1=print_piece(7,5),  g1=print_piece(7,6), h1=print_piece(7,7);

printf("   +---+---+---+---+---+---+---+---+");
printf("\n");
printf("8  | %c | %c | %c | %c | %c | %c | %c | %c |",a8,b8,c8,d8,e8,f8,g8,h8);
printf("\n");
printf("   +---+---+---+---+---+---+---+---+");
printf("\n");
printf("7  | %c | %c | %c | %c | %c | %c | %c | %c |",a7,b7,c7,d7,e7,f8,g7,h7);
printf("\n");
printf("   +---+---+---+---+---+---+---+---+");
printf("\n");
printf("6  | %c | %c | %c | %c | %c | %c | %c | %c |",a6,b6,c6,d6,e6,f6,g6,h6);
printf("\n");
printf("   +---+---+---+---+---+---+---+---+");
printf("\n");
printf("5  | %c | %c | %c | %c | %c | %c | %c | %c |",a5,b5,c5,d5,e5,f5,g5,h5);
printf("\n");
printf("   +---+---+---+---+---+---+---+---+");
printf("\n");
printf("4  | %c | %c | %c | %c | %c | %c | %c | %c |",a4,b4,c4,d4,e4,f4,g4,h4);
printf("\n");
printf("   +---+---+---+---+---+---+---+---+");
printf("\n");
printf("3  | %c | %c | %c | %c | %c | %c | %c | %c |",a3,b3,c3,d3,e3,f3,g3,h3);
printf("\n");
printf("   +---+---+---+---+---+---+---+---+");
printf("\n");
printf("2  | %c | %c | %c | %c | %c | %c | %c | %c |",a2,b2,c2,d2,e2,f2,g2,h2);
printf("\n");
printf("   +---+---+---+---+---+---+---+---+");
printf("\n");
printf("1  | %c | %c | %c | %c | %c | %c | %c | %c |",a1,b1,c1,d1,e1,f1,g1,h1);
printf("\n");
printf("   +---+---+---+---+---+---+---+---+");
printf("\n");
printf("     A   B   C   D   E   F   G   H   ");
printf("\n");

}

  //Function's definitions
  int  check_if_empty(int x2, int y2)//if board[x2][y2]=0 it's empty
{
	if(board[x2][y2]> 0)
	{
		printf("The place is not empty\n");
		return 0;
	}
	else
	return 1;
}
void  update_board(int x1,int x2,int y1, int y2)
{
      board[x2][y2]=board[x1][y1];
      board[x1][y1]=0;
      return;
      }

int pawn_moove(int x1,int x2,int y1, int y2)
{
    int dx,dy;
    dx=abs(x2-x1);
    dy=abs(y2-y1);
    if((dx!=0)&&(dy>2))
                     {
                     printf("Cannot moove- the moove is illegall!\n");
                     return 0;
                     }
                     else
                     {
                         if((check_if_empty(x1,y1+1)||(check_if_empty(x2,y2))))
                         {
                     update_board(x1,y1,x2,y2);
                     print_board();
                     return 1;
                         }
                     }
                     return 1;
}
int rook_moove(int x1,int x2,int y1, int y2)
{
		int dx,dy;
		int i,j;
		dx=abs(x2-x1);
		dy=abs(y2-y1);
		if(dx==0)//vertical move
		          {
                            for(j=y1+1;j<y2;j++)
			               {
			             	if(check_if_empty(x1,j))
				               {
 				                	update_board(x1,y1,x2,y2);
				                 	print_board();
				                	return 1;
                                 }
                            }
                 }
          else if(dy==0)//horizontal move
              {
		         	for(i=x1+1;i<x2;i++)
		             	{
			            	if(check_if_empty(i,y2))
				             {
				            	update_board(x1,y1,x2,y2);
				            	print_board();
				            	return 1;
			                 }
                        }
	    	}

          else
			printf("Cannot moove- the m oove is illegall!\n");
			return 0;
}

 int knight_moove(int x1,int x2,int y1, int y2)
{
    int dx,dy;
    dx=abs(x2-x1);
    dy=abs(y2-y1);
    if (((dx==1 && dy==2)&&(check_if_empty(x2,y2)) || (dx==2 && dy==1)&&(check_if_empty(x2,y2))))
                                {
                                update_board(x1,y1,x2,y2);
				            	print_board();
				            	return 1;
                                }
                                else
                                printf("Cannot moove- the moove is illegall!\n");
                                return 0;

}
int bishop_moove(int x1,int x2,int y1, int y2)
{
    int i,j;
    int dx,dy;
    dx=abs(x2-x1);
    dy=abs(y2-y1);
    if(dx!=dy)//if not diagonal move
          {
           printf("Cannot moove- the moove is illegall!\n");
           return 0;
          }
                    else
                        {
                         for(i=x1+1,j=y1+1;i<x2,j<y2;i++,j++)
                                  {
                                    if(check_if_empty(i,j));
                                  }
                        }
                          update_board(x1,y1,x2,y2);
                          print_board();
                          return 1;
}
  int queen_moove(int x1,int x2,int y1, int y2)
  {
      int i,j;
      int dx,dy;
      dx=abs(x2-x1);
      dy=abs(y2-y1);
                     if(dx==dy)
                       for(i=x1+1,j=y1+1;i<x2,j<y2;i++,j++)

                           {
                             if(check_if_empty(i,j));
                               {
                                update_board(x1,y1,x2,y2);
                                print_board();
                                return 1;
                               }
                            }
                         else if(dy==0)
                          {
                             for(i=x1+1;i<x2;i++)
                               {
                                 if(check_if_empty(i,y2));
                                update_board(x1,y1,x2,y2);
                                print_board();
                                return 1;
                                }
                          }
                         else if(dx==0)
                           {
                              for(j=y1+1;j<y2;j++)
                                 {
                                    if(check_if_empty(x1,j));
                                    update_board(x1,y1,x2,y2);
                                    print_board();
                                     return 1;
                                  }
                            }
                            else
                            {
                               printf("Cannot moove- the moove is illegall!\n");
                               return 0;
                            }
                            return 1;
}
     int king_moove(int x1,int x2,int y1, int y2)
{
    int dx,dy;
    dx=abs(x2-x1);
    dy=abs(y2-y1);
    if (((dx==1) && (dy<=1)) || ((dx<=1) && (dy==1)))
             {
                            if(check_if_empty(x2,y2));
                            update_board(x1,y1,x2,y2);
                            print_board();
                            return 1;
             }
             else
                 {
                   printf("Cannot moove- the moove is illegall!\n");
                    return 0;
                  }
}

[TEX]thanx,guys[/TEX]

themenucha
Newbie Poster
19 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 
Hi!This Is My Code after formatting and compiling- still need some help in main function.

What help do you exactly need ? Is your code working now or are there still any compiler errors. If so, post them too

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 
What help do you exactly need ? Is your code working now or are there still any compiler errors. If so, post them too


Hi! There is no compilation errors, but the program itself is not working. I checked my main funcs and they seems to be working.I cant't find my bug...
All I nee is
1.Get start position (E2 for example)
2.Get finish(E4)
3.Check legality
4.Move the piece and display
My problem is not a compilation but the main itself.
I'm using CodeBlocks.
Maybe it's because i have nested functions?
Thanx

themenucha
Newbie Poster
19 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 
printf("\nPlease select a starting position for a figure for instance E2 in upper case\n"); scanf("%1s%d",&c,&y);

Might be a problem here. Try printing values of c and y and see if you are getting proper values. I suppose it is because you are giving your input as E2 rather than E 2. Also use %c for c

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

In starting position im getting a proper values
E2
c->E
x->4
y->2
but in finishing position i'm getting
c1->4
x->2
y->-55
why??

themenucha
Newbie Poster
19 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 
In starting position im getting a proper values E2 c->E x->4 y->2 but in finishing position i'm getting c1->4 x->2 y->-55 why??

What did you input as finishing position.

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 
scanf("%c,%d",&c,&y);

There should be No comma between %c and %d!!!!

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

I removed it: still the values are not proper

printf("\nPlease select a starting position for a figure for instance E2  in upper case\n");

		scanf("%c %d",&c,&y);
		printf("%c\n",c);
		if(c=='Q')//Exit chess
		                  {
                          printf("Thank you and goodbye\n");
                          return 0;
                          }
        else
        x1=char_to_int(c);
        printf("%d\n",x1);
         printf("%d\n",y);
		y1=y-1;
		printf("Please select a finishing position for a figure for instance E,4\n");
		scanf("%c %d",&c1,&y0);
		printf("%c\n",c1);
				if(c=='Q')//Exit chess
		                  {
                          printf("Thank you and goodbye\n");
                          return 0;
                          }
         else
		x2=char_to_int(c1);
		 printf("%d\n",x2);
         printf("%d\n",y1);
		y2=y1-1;



      int choice;
      choice=board[x1][y1];

      switch(choice)
      {
                    case 1:pawn_moove(x1,y1,x2,y2);
                    break;
                    case 2:rook_moove(x1,y1,x2,y2);
                    break;
                    case 3:knight_moove(x1,y1,x2,y2);
                    break;
                    case 4:bishop_moove(x1,y1,x2,y2);
                    break;
                    case 5:queen_moove(x1,y1,x2,y2);
                    break;
                    case 6:king_moove(x1,y1,x2,y2);
                    break;
                    default:
                            printf("Wrong selection of start/finish combination\n");
                            break;

          }


		return 0;
}
}
themenucha
Newbie Poster
19 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

I think you are giving the wrong input here. E,4 or E4. Post EXACTLY what input you have given and the exact output that you have got. Also ,no space here : use

scanf("%c%d",&c,&y);
DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

Next (although not related to your problem but what you have done is bad programming) : For setup_board() , use for loop ,don't assign values individually

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: