XxY XxY - 1 month ago 15
C Question

Assigning characters into 2d array[Checkers]

I'm working on a checkers game in which I want to print out the character 'x' and 'o' into the 2d array. But my code doesn't work, it printed out different characters instead. I need help.

Here's my code :

#include <stdio.h>


void message()
{

char name[20],name2[20];

printf("Please enter the name for the first player : ");
scanf("%s", &name);
printf("Please enter the name for the second player : ");
scanf("%s", &name2);

printf("\nGood day %s and %s, let's start the game....all the best!!!\n\n", name, name2);

}

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

int x, y;

for(x=0; x<8; x++)
{
for(y=0; y<8; y++)
{
printf("|%c ");
}
printf("|\n\n");
}



}

void character(char board[8][8])
{
int i,j;

for(i=0;i<8;i++){
for(j=0;j<8;j++){
if(i<3){

if(i%2 == 0){
if(j%2 == 0){
board[i][j] = ' ';

}
if(j%2==1){
board[i][j]= 'O';

}
}
if(i%2 == 1){
if(j%2 == 0){
board[i][j] = 'O';
}
if(j%2 ==1){
board[i][j]= ' ';

}
}

}
if((i==3) || (i==4))
{board[i][j] = ' ';}
if(i>4)
{

if(i%2 == 0){
if(j%2 == 0){
board[i][j] = ' ';

}
if(j%2 ==1){
board[i][j]= 'X';

}
}

if(i%2 == 1){
if(j%2 == 0){
board[i][j] = 'X';

}
if(j%2 ==1){
board[i][j]= ' ';

}
}
}

}
}

}


int main()
{
int x, y;
char bx[8][8];

message();

gameboard(bx);

character(bx);


return 0;

}

Answer

The order of character(bx) and gameboard(bx) need to be changed, so that it may set the values in the array before print it.

And modify your gameboard function as followed to show the value for the specific element:

int gameboard(char board[8][8])                                                                                                                                                                              
{
            ...

            printf("|%c  ",board[x][y]);

            ...
}
Comments