# Thread: Simple Multi-Array Chess Piece Relative Value Problem (Beginner)

1. ## Simple Multi-Array Chess Piece Relative Value Problem (Beginner)

Hello all,

I've been working on a chess piece relative value problem. The user inputs chess piece positions for both White and Black, and the output is the sum of the relative value of chess pieces on the board.

I have some style issues that I'd like to resolve.

First, I am using the despicable "goto" statement to repeat my loop. I would rather not do it, but need some help figuring that part out.

Second, I wish I didn't have to repeat my loop for both White and Black. I'd rather have one loop, if possible. Any suggestions would be appreciated here too.

Third, if you have any other style suggestions, I would be grateful for the advice.

Below is the ugly code.

Code:
```#include <stdio.h>

int evaluate_position(char board[8][8]);

int main(){

char chess[8][8] = {0};
int x, y, z, white_number, black_number, row = 0;
char col = 'a', rank;

printf("Please enter White's pieces on the chessboard\n\n");

printf("How many pieces do you have on the board? ");
scanf("%d", &white_number);

printf("Please enter the rank and position of your pieces: ");

z = 1;

repeat:
while (z <= white_number) {
scanf(" %c%d%c", &rank, &row, &col);

if (row < 9 && row > 0)
y = row - 1;
else{
printf("Not a valid row!\n");
goto repeat;
}

if (col < 'i' && col >= 'a')
x = col - 'a';
else{
printf("Not a valid column!\n");
goto repeat;
}

if (chess[y][x] == 0){
chess[y][x] = rank;
z++;
}
else
printf("Illegal Move!\n");
}

printf("\n\nPlease enter Black's pieces on the chessboard\n\n");

printf("How many pieces do you have on the board? ");
scanf("%d", &black_number);

printf("Please enter the rank and position of your pieces: ");

z = 1;

repeat_b:
while (z <= white_number) {
scanf(" %c%d%c", &rank, &row, &col);

if (row < 9 && row > 0)
y = row - 1;
else{
printf("Not a valid row!\n");
goto repeat_b;
}

if (col < 'i' && col >= 'a')
x = col - 'a';
else{
printf("Not a valid column!\n");
goto repeat_b;
}

if (chess[y][x] == 0){
chess[y][x] = rank;
z++;
}
else
printf("Illegal Move!\n");
}

printf("\nThe total sum of the chess pieces relative value on the board is %d\n", evaluate_position(chess));

if (evaluate_position(chess) > 0)
else

}

int evaluate_position(char board[8][8]){

int sum = 0, sum_b = 0, j = 0;

for (int i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
switch(board[i][j]){
case 'K': sum += 0;
break;
case 'Q': sum += 9;
break;
case 'R': sum += 5;
break;
case 'B': sum += 3;
break;
case 'N': sum += 3;
break;
case 'P': sum += 1;
break;
case 'k': sum_b += 0;
break;
case 'q': sum_b += 9;
break;
case 'r': sum_b += 5;
break;
case 'b': sum_b += 3;
break;
case 'n': sum_b += 3;
break;
case 'p': sum_b += 1;
break;

}
}

return (sum - sum_b);

}```

2. No gotos.

And better structure.

If you find yourself doing ctrl-c,ctrl-v on large blocks of code, you should be thinking "make this a function!".

Code:
```void input ( int numPieces, char chess[8][8] ) {
int z = 1;
printf("Please enter the rank and position of your pieces: ");
while ( z <= numPieces ) {
scanf(" %c%d%c", &rank, &row, &col);
if (row < 9 && row > 0)
y = row - 1;
else {
printf("Not a valid row!\n");
continue;
}
if (col < 'i' && col >= 'a')
x = col - 'a';
else {
printf("Not a valid column!\n");
continue;
}

if (chess[y][x] == 0){
chess[y][x] = rank;
z++;
}
else
printf("Illegal Move!\n");
}
}

int main ( ) {
printf("Please enter White's pieces on the chessboard\n\n");
printf("How many pieces do you have on the board? ");
scanf("%d", &white_number);
input(white_number,chess);
printf("\n\nPlease enter Black's pieces on the chessboard\n\n");
printf("How many pieces do you have on the board? ");
scanf("%d", &black_number);
input(black_number,chess);
}```

3. Thank Salem! Appreciate this edit and rule-of-thumb a lot.

Popular pages Recent additions