# Problem with drafts game

• 12-21-2005
InvertedSaint
Problem with drafts game
Hey y'all. I've decided to try and code a drafts game. However, I'm getting some rather odd output. Now, I know I could be doing this more efficiently, but right now I'm just interested in working code :)

Anyway, the basic problem is that when I try and detect a piece, it will only detect on the first row.

So, if I try "row = 1, col = 1", the piece will be detected. However, if I try "row = 2, col = 2", the piece will not be detected. I don't understand why though :S

This is not for homework or such, just my own personal want to learn to code. Any help is greatly appreciated :)

Code:

```#include <stdio.h> void    populateboard(void); void    printboard(void); void    makemove(void); /* board[row][column] */ int    board[8][8] = {0}; int    turn = 1; int main(void) {     populateboard();     system("CLS");     printboard();     makemove();         printf("\n");     system("PAUSE");     return 0; } void populateboard(void) {     int i, j;         for (i = 0; i <= 2; i += 2) {         for (j = 0; j <= 7; j += 2) {             board[i][j] = 1;         }     }     for (j = 1; j <= 7; j += 2) {         board[1][j] = 1;     }     for (i = 5; i <= 7; i += 2) {         for (j = 0; j <= 7; j += 2) {             board[i][j] = 2;         }     }     for (j = 1; j <= 7; j += 2) {         board[6][j] = 2;     }     return; } void printboard(void) {     int i, j;         printf("    1  2  3  4  5  6  7  8");     printf("\n  ---------------------------------\n");     for (i = 0; i <= 7; ++i) {         for (j = 0; j <= 7; ++j) {             if (j == 0) {                 printf("%i ", i + 1);             }             printf("| %c ", board[i][j]);             if (j == 7) {                 printf("|\n  ---------------------------------\n");             }         }     }     return; } void makemove(void) {     int row, col;     start: ;     printf("\n%c's turn\n\nEnter row: ", turn);     scanf("%i", &row);     printf("Enter column: ");     scanf("%i", &col);         printf("\nrow: %i\ncol: %i\n", row, col);         if (board[row + 1][col + 1] == turn) {         printf("Piece exists");     } else {         printf("Piece does not exist");         goto start;     }     return; }```
• 12-21-2005
InvertedSaint
Code:

```    for (j = 1; j <= 7; j += 2) {         board[1][j] = 1;     }```
That is in there, which I think would populate row two. I do not think it is a problem with the population, however, as when I print the board it shows all the pieces in their places. Also, row three, which is populated in the same fashion as row one, is also undetectable.

EDIT: Was the above post (By another user) removed, or was I seeing things?
• 12-21-2005
Dave_Sinkula
No I deleted it because I realized I posted poorly. (Not that this one is great by any means.)

Some changes:
Code:

```#include <stdio.h> void    populateboard(void); void    printboard(void); void    makemove(void); int    board[8][8] = {0}; int    turn = 1; int main(void) {   populateboard();   printboard();   makemove();   return 0; } void populateboard(void) {   int i, j;   for ( i = 0; i < 8; ++i )   {       for ( j = 0; j < 8; ++j )       {         if ( ((i == 0 || i == 2) && j % 2 == 0) || (i == 1 && j % 2 == 1) )         {             board[i][j] = 1;         }         if ( ((i == 5 || i == 7) && j % 2 == 0) || (i == 6 && j % 2 == 1) )         {             board[i][j] = 2;         }       }   } } void printboard(void) {   int i, j;   printf("    1  2  3  4  5  6  7  8");   printf("\n  ---------------------------------\n");   for ( i = 0; i < 8; ++i )   {       for ( j = 0; j < 8; ++j )       {         if ( j == 0 )         {             printf("%i ", i + 1);         }         printf("| %c ", board[i][j]);         if ( j == 7 )         {             printf("|\n  ---------------------------------\n");         }       }   }   return; } void makemove(void) {   int row, col;   start: ;   printf("\n%c's turn\n\nEnter row: ", turn);   scanf("%i", &row);   printf("Enter column: ");   scanf("%i", &col);   printf("\nrow: %i\ncol: %i\n", row, col);   if ( board[row - 1][col - 1] == turn )   {       printf("Piece exists");   }   else   {       printf("Piece does not exist: turn = %d\n", turn);       goto start;   }   return; }```
Shouldn't the setup be different?
http://en.wikipedia.org/wiki/Checkers
Code:

```        if ( ((i == 0 || i == 2) && j % 2 == 0) || (i == 1 && j % 2 == 0) )         {             board[i][j] = 1;         }         if ( ((i == 5 || i == 7) && j % 2 == 1) || (i == 6 && j % 2 == 1) )         {             board[i][j] = 2;         }```
[edit=Jeez]
Code:

```        if ( ((i == 0 || i == 2) && j % 2 == 0) || (i == 1 && j % 2 == 1) )         {             board[i][j] = 1;         }         if ( ((i == 5 || i == 7) && j % 2 == 1) || (i == 6 && j % 2 == 0) )         {             board[i][j] = 2;         }```
• 12-21-2005
InvertedSaint
Ahh! Thank you! An extremely efficient way to populate the board (I forgot about modulus :S ) and the final problem with my code.

So, row one would detect because you would type '1', which the computer would add one to, making it a 2 and row three would be row 2 in the array's scheme of things, right?

Thanks for setting me on the right track, it all makes sense now :)
• 12-21-2005
Dave_Sinkula
Quote:

Originally Posted by InvertedSaint
So, row one would detect because you would type '1', which the computer would add one to, making it a 2 and row three would be row 2 in the array's scheme of things, right?

In C arrays are zero-based. So when you enter element 1,1 you of course mean array location 0,0.
[Whoo-hoo! Post #2000.]
• 12-21-2005
Mikecore
Well Im a noob but
forget it
• 12-21-2005
InvertedSaint
Quote:

Originally Posted by Dave_Sinkula
In C arrays are zero-based. So when you enter element 1,1 you of course mean array location 0,0.
[Whoo-hoo! Post #2000.]

Yeah, I got that. That's why I had the '+ 1' in there. Of course, I was an idiot and did it the wrong way :S

And yeah, you're right about the other side being setup differently. I probably would've run into that further down the track.