Pls help, we're trying to code a game of checkers and for some reason , it's always white's turn pls help. I'ts C btw

/* Members: MCSPICIE
Lanz Ling
Diego Sevilla
Mark Villaluz
*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>

#define MAX_ROW 8
#define MAX_COL 8

void displayboard(char checker[MAX_ROW][MAX_COL]){
int i, j;

printf("     1   2   3   4   5   6   7   8   X \n");

for (i = 0; i < MAX_ROW; i++){
	printf (" %d", i + 1);
	printf(" | ");
	for (j = 0 ; j < MAX_COL ; j++) // The for loop here is to print where should be the player 1 and player 2 chess should be located
		printf("%c | ", checker[i][j]); 
		printf("\n");
}
printf(" Y\n\n");

}

void populatechecker(char checker[MAX_ROW][MAX_COL]){
int i, j;

for(i = 0; i < MAX_ROW; i++)
	for( j = 0; j < MAX_COL; j++)
		checker[i][j] = ' ';

for(i = 0; i < MAX_ROW; i++)
	for ( j = 0; j < MAX_COL; j++)
		if ( i <= 2 && j % 2 == 1 && i % 2 == 0 )
			checker[i][j] = 'W';
		else if ( i == 1 && j % 2 == 0)
			checker[i][j] = ' W';
		

for(i = 0; i < MAX_ROW; i++)
	for(j = 0; j < MAX_COL; j++)
		if(i >= 5 && i % 2 == 1 && j % 2 == 0)
			checker[i][j] = 'B';
		else if(i == 6 && j % 2 == 1)
			checker[i][j] = 'B';			 

}

int main(){

int i, j;
char checker[MAX_ROW][MAX_COL];
char temp;
int x1, y1, x2, y2;
int wpieces = 12, bpieces = 12;
int white = 1, black = 1;

populatechecker(checker);
displayboard(checker);

do {
if ( white = 1)
{
do{

printf("It is White's Turn please choose peace to move [X][Y]:");
scanf("%d %d", &y1, &x1);
printf("Where would you like to move it to? [X][Y]: ");
scanf("%d %d", &y2, &x2);
if (checker[x2 + 1][y2 + 1 ] == 'W' || checker[x2 - 1][ y2 + 1] == 'W')
	printf("The Poistion chosen is invalid\n");
} while ( !(checker[y1 - 1][x1 - 1] == 'W') ||  x1 < 1 || x1 > 8 || y1 < 1 || y1 > 8 || x2 < 1 || x2 > 8 || y2 < 1 || y2 > 8 || checker[x2-1][y2-1] == 'W' || x2 == x1 + 1 && x2 == x1 - 1 || y2 == y1 + 1 && y2 == y1 - 1); 


if( checker[x1-1][y1-1] == 'W')
{
checker[x1-1][y1-1] = ' ';
checker[x2-1][y2-1] = 'W';
}
else 
	printf("Invalid Input. Please Try Again!");

if (checker[x2-1][y2-1] == 'B')
	bpieces--;

system("CLS");

displayboard(checker);

white = 0;
black = 1;
}
else if (black = 1)
{
do{
printf("It is Black's' Turn please choose peace to move [X][Y]:");
scanf("%d %d", &y1, &x1);
printf("Where would you like to move it to? [X][Y]: ");
scanf("%d %d", &y2, &x2);
if (checker[x2 + 1][y2 + 1 ] == 'B' || checker[x2 - 1][ y2 + 1] == 'B')
	printf("The Poistion chosen is invalid\n");
} while ( !(checker[y1 - 1][x1 - 1] == 'B') ||  x1 < 1 || x1 > 8 || y1 < 1 || y1 > 8 || x2 < 1 || x2 > 8 || y2 < 1 || y2 > 8 || checker[x2-1][y2-1] == 'B' || x2 == x1 + 1 && x2 == x1 - 1 || y2 == y1 + 1 && y2 == y1 - 1); 


if( checker[x1-1][y1-1] == 'B')
{
checker[x1-1][y1-1] = ' ';
checker[x2-1][y2-1] = 'B';
}
else 
	printf("Invalid Input. Please Try Again!");

if (checker[x2-1][y2-1] == 'W')
	wpieces--;

system("CLS");

displayboard(checker);

black = 0;
white = 1;
}
} while (wpieces != 0 || bpieces != 0);


if (white == 0)
	printf("White Wins!");
else if (black == 0)
	printf("Black Wins!");
	

return 0;

}
type or paste code here

It’s going to be a headache to debug. There is a lot going on in your main function. Too much. You need to break the game up into many more smaller functions.

The code is also very hard to read and debug because the style is inconsistent.

3 Likes

quick glance:
Does it sound right to you that a turn can be both black and white?, and in C/C++ = is for assignment, and == is for comparison.

2 Likes

Totally off topic, but related…

Never write a do in such as way as it takes scolling to find the while. Use functional coding to abstact things away from the loop body. Keep do and while easily visible, together.

This goes to what the first response touched on, writing function code that breaks up the program so we can read and debug it.

1 Like