All this really needs is a little reformatting and an understanding that you've already defined the board, just go to the position.
Also, since you had the constants alread defined, I used them and then changed them to #define to remove the memory consumption.
I did not validate the positions on the board (for white/black placement).
#include<stdio.h>
#define empty 0
#define pawn 1
#define rook 2
#define knight 3
#define bishop 4
#define queen 5
#define king 6
int board[8][8];
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]=king;
board[7][4]=queen;
board[7][5]=bishop;
board[7][6]=knight;
board[7][7]=rook;
}
char print_piece(int x, int y)
{
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;
}
int main()
{
int x=0;
char letter=0;
/**/
setup_board();
letter=print_piece(6,6);
printf("%c\n", letter);//to check an output
printf("Press number key to exit:");
scanf("%d",&x);//to pause the program
return 0;
} thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402