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 <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
#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<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;
}
}
}
}
int knight_moove(int x1,int x2,int y1, int y2)
{
int dx,dy;
dx=abs(x2-x1);
dy=abs(y2-y1);
if (((dx )!< 3) &&((dy)! < 3 )&& ((dx+dy)! == 3)))
{
printf("Cannot moove- the moove is illegall!\n");
return 0;
}
else
{
if(check_if_empty(x2,y2))
{
update_board(x1,y1,x2,y2);
print_board();
return 1;
}
}
}
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)
{
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!=0)&&(dy=0))||((dy!=0)&&(dx=0)))||(dx!=dy))
{
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));
}
}
else
{
for(i=x1+1;i<x2;i++)
{
if(check_if_empty(i,y2));
}
else
{
for(j=y1+1;j<y2;j++)
{
if(check_if_empty(x1,j));
}
update_board(x1,y1,x2,y2);
print_board();
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!=dy)||(dx!=0)&&(dy=1)||(dy!=0)&&(dx==0))&&(dx+dy!=2))
printf("Cannot moove- the moove is illegall!\n");
return 0;
else
{
if(check_if_empty(x2,y2));
}
update_board(x1,y1,x2,y2);
print_board();
return 1;
}
}
Thanx!!!