Problem with drafts game

This is a discussion on Problem with drafts game within the C Programming forums, part of the General Programming Boards category; Hey y'all. I've decided to try and code a drafts game. However, I'm getting some rather odd output. Now, I ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    18

    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;
    }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    18
    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?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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;
    }
    [edit]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;
             }
    Last edited by Dave_Sinkula; 12-21-2005 at 10:03 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    18
    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

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    Well Im a noob but

    forget it

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    18
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. platform game logic, problem
    By Akkernight in forum Game Programming
    Replies: 7
    Last Post: 02-23-2009, 09:49 AM
  2. Simple memory game problem
    By Eamusuta in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2005, 07:59 AM
  3. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 08:42 PM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM
  5. Problem with a game im making
    By TheGr8one in forum Game Programming
    Replies: 2
    Last Post: 10-19-2001, 07:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21