# Thread: Help in checkers game!

1. ## Help in checkers game!

Please help me i have this due soon,also here is my source code for the game, does anybody know what i should do to make the pieces move? thank you.
Code:
```#include <stdio.h>
#include <Windows.h>  //include the windows API to print unicode
//allows print unicode

//Sets-Up Checker Pieces by giving a value in a multidimensional array.
void initializeBoard(int board[8][8])
{
int i, j, k;

for(i = 1; i < 9; i++)
{
for(j = 1; j < 9; j++)
{
if(((i == 6)&&(j%2 == 1))||((i == 7)&&(j%2 == 0))||((i == 8)&&(j%2 == 1))) //Player 1 Pieces; valid square value is changed to 1
{
board[i][j] = 1;
}
else if(((i == 1)&&(j%2 == 0))||((i == 2)&&(j%2 == 1))||((i == 3)&&(j%2 == 0))) //Player 2 Pieces; valid square value is changed to 2
{
board[i][j] = 2;
}
else if((i + j)%2 == 1) //Checks all Valid Squares and value is set to 3
{
board[i][j] = 3;
}
else
{
board[i][j] = 0;
}
}
}
}
void printBoard(int board[8][8])
{
int i, j, k;
int a = 0; //piece number for black
int b = 13; //piece number for white
//print column number

for(k = 1; k != 9; k++)
{
printf("  %d ",k);
}
//print board
printf("\n+---+---+---+---+---+---+---+---+\n"); //top row divider
for(i = 0; i <9; i++) //prints row with i being the number of rows
{
for(j = 0; j <9; j++)//prints column with j being the number of rows
{
if(board[i][j] == 1)
{
//spacing issues
a += 1;
if(a < 10) //if a single digit print piece with space
{
printf("|D%d ", a);
}
else //if not single digit print piece without space
{
printf("|D%d", a);
}
}
else if(board[i][j] == 2)
{
//spacing issues
b -= 1;
if(b < 10) //if a single digit print piece with space
{
printf("|L%d ", b);
}
else //if not single digit print piece without space
{
printf("|L%d", b);
}
}
else if(board[i][j] == 3)
{
printf("| â–  ");
}
else
{
printf("|   ");//print a column
}
}
printf("| %d", i); //outer right side border with row numbers
printf("\n"); //new line to signify new row
printf("+---+---+---+---+---+---+---+---+\n"); //row divider
}
}
int main()
{
SetConsoleOutputCP(CP_UTF8);
int board[8][8]; //board
initializeBoard(board);
printBoard(board);
return 0;
}```

2. > for(i = 1; i <9; i++)
Array subscripts start at 0, not 1
So you're wandering off the ends of the board array.

You can avoid all the if/else to determine the field width by just doing this.
Code:
`printf("|D%2d ", a);`
Initialise the board.
Code:
`int board[8][8] = { { 0 } }; //board`
But you actually need to initialise the top two ranks with white pieces, and the bottom two ranks with black pieces, as per the layout of a checkers game.

> does anybody know what i should do to make the pieces move? thank you.
Well the first thing to do would be to prompt "which piece do you want to move" followed by "where do you want to move it to".

3. Originally Posted by Salem
> for(i = 1; i <9; i++)
Array subscripts start at 0, not 1
So you're wandering off the ends of the board array.

You can avoid all the if/else to determine the field width by just doing this.
Code:
`printf("|D%2d ", a);`
Initialise the board.
Code:
`int board[8][8] = { { 0 } }; //board`
But you actually need to initialise the top two ranks with white pieces, and the bottom two ranks with black pieces, as per the layout of a checkers game.

> does anybody know what i should do to make the pieces move? thank you.
Well the first thing to do would be to prompt "which piece do you want to move" followed by "where do you want to move it to".
Thank you for that, i have updated the code. please check for the corrections needed.

4. > Thank you for that, i have updated the code. please check for the corrections needed.

All of them should be
for ( i = 0 ; i < 8 ; i++ )
or
for ( j = 0 ; j < 8 ; j++ )

Also, post the latest code.
Don't keep editing the original post.

5. updated code, so to validate the pieces do i have to make a function that validates the pieces?
Code:
```#include <stdio.h>
#include <Windows.h>  //include the windows API to print unicode
//allows print unicode

//Sets-Up Checker Pieces by giving a value in a multidimensional array.
void initializeBoard(int board[8][8])
{
int i, j, k;
/* If the value of board[i][j] is equal to 1, then that piece is Player 1's piece.
If the value of board[i][j] is equal to 2, then that piece is Player 2's piece.
board[i][j] is equal to 0 means that the location is not an outer bound area, (white squares)
board[i][j] is equal to 3 means that the location is a possible position but is empty */
//Player 1 Pieces
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
if(((i == 6)&&(j%2 == 1))||((i == 7)&&(j%2 == 0))||((i == 8)&&(j%2 == 1))) //Player 1 Pieces; valid square value is changed to 1
{
board[i][j] = 1;
}
else if(((i == 1)&&(j%2 == 0))||((i == 2)&&(j%2 == 1))||((i == 3)&&(j%2 == 0))) //Player 2 Pieces; valid square value is changed to 2
{
board[i][j] = 2;
}
else if((i + j)%2 == 1) //Checks all Valid Squares and value is set to 3
{
board[i][j] = 3;
}
else
{
board[i][j] = 0;
}
}
}
}
void printBoard(int board[8][8])
{
int i, j, k;
int a = 0; //piece number for black
int b = 13; //piece number for white
//print column number

for(k = 0; k != 9; k++)
{
printf("  %d ",k);
}
//print board
printf("\n+---+---+---+---+---+---+---+---+\n"); //top row divider
for(i = 0; i <8; i++) //prints row with i being the number of rows
{
for(j = 0; j <8; j++)//prints column with j being the number of rows
{
if(board[i][j] == 1)
{
//spacing issues
a += 1;
if(a < 10) //if a single digit print piece with space
{
printf("|D%d ", a);
}
else //if not single digit print piece without space
{
printf("|D%d", a);
}
}
else if(board[i][j] == 2)
{
//spacing issues
b -= 1;
if(b < 10) //if a single digit print piece with space
{
printf("|L%d ", b);
}
else //if not single digit print piece without space
{
printf("|L%d", b);
}
}
else if(board[i][j] == 3)
{
printf("| â–  ");
}
else
{
printf("|   ");//print a column
}
}
printf("| %d", i); //outer right side border with row numbers
printf("\n"); //new line to signify new row
printf("+---+---+---+---+---+---+---+---+\n"); //row divider
}
}
int main()
{
SetConsoleOutputCP(CP_UTF8);
int board[8][8]; //board
initializeBoard(board);
printBoard(board);
return 0;
}```

6. Code:
```\$ ./a.out
0   1   2   3   4   5   6   7   8
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   | 0
+---+---+---+---+---+---+---+---+
|L12|   |L11|   |L10|   |L9 |   | 1
+---+---+---+---+---+---+---+---+
|   |L8 |   |L7 |   |L6 |   |L5 | 2
+---+---+---+---+---+---+---+---+
|L4 |   |L3 |   |L2 |   |L1 |   | 3
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   | 4
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   | 5
+---+---+---+---+---+---+---+---+
|   |D1 |   |D2 |   |D3 |   |D4 | 6
+---+---+---+---+---+---+---+---+
|D5 |   |D6 |   |D7 |   |D8 |   | 7
+---+---+---+---+---+---+---+---+```
Why is there an 8 at the end of the top row?
Why are there 3 rows of L pieces?
Why don't the L pieces start in the first row?

/* If the value of board[i][j] is equal to 1, then that piece is Player 1's piece.
If the value of board[i][j] is equal to 2, then that piece is Player 2's piece.
board[i][j] is equal to 0 means that the location is not an outer bound area, (white squares)
board[i][j] is equal to 3 means that the location is a possible position but is empty */
Use an enum.
Code:
```enum {
EMPTY = 0,  // unused space
WHITE = 1,  // white piece
BLACK = 2,  // black piece
FREE = 3,  // valid, but unused position
};
...
if(((i == 6)&&(j%2 == 1))||((i == 7)&&(j%2 == 0))||((i == 8)&&(j%2 == 1))) //Player 1 Pieces; valid square value is changed to 1
{
board[i][j] = WHITE;
}
else if(((i == 1)&&(j%2 == 0))||((i == 2)&&(j%2 == 1))||((i == 3)&&(j%2 == 0))) //Player 2 Pieces; valid square value is changed to 2
{
board[i][j] = BLACK;
}
else if((i + j)%2 == 1) //Checks all Valid Squares and value is set to 3
{
board[i][j] = FREE;
}
else
{
board[i][j] = EMPTY;
}```