Thread: I really need help in 2d array c. Please help.

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    5

    I really need help in 2d array c. Please help.

    Hi guys, I am creating a checkers board game for my assignment which is due soon. The problem is the char 'X' doesn't go into the array. Any idea? Have been searching for error for almost an hour but still no clue.
    I really need help please.

    Code:
     void gameboard(char board[16][8])
     {
    
    
      int x, y;
     
      for(x=0; x<16; x++)
      {
        for(y=0;y<8;y++)
        {
        if(x % 2 == 0)
        {
        printf(" %c ",board[x][y]);
        }
        else{
        printf("| %c |",board[x][y]);
          }
        }
        printf("\n");
      }
    
    
      }
    
    
    
    void character(char board[16][8])
     {
     int x,y;
     
     for(x=0;x<16;x++){
       for(y=0;y<8;y++){
       
        if(x<6){
            
           if(x%2 == 0){
                  if(x%2 == 0){
                  board[x][y] = ' ';
              
                  }
                 }
    
    
             
          
           if(x%2 == 1){
                  if(y%2 == 0){
                  board[x][y] = 'O';
                   }
                  if(y%2 ==1){
                 board[x][y]= ' ';
                  
                  }
           }
    
    
          if(x==3){
             if(y%2 == 1){
             board[x][y] = 'O';
             }
             if(y%2 == 0){
             board[x][y] = ' ';
             }
            } 
        }
        
        if((x==6)||(x==7)||(x==8)||(x==9)){
        board[x][y] = ' ';
        } 
    
    
            
        if(x>9)
        {  
        
           if(x%2 == 0){
                  if(x%2 == 0){
                  board[x][y] = 'X';
              
                  }
                  if(y%2 == 0){
                  board[x][y] = ' ';
                 }
         
           }  
        
          if(x%2 == 1){
                  if(y%2 == 1){
                  board[x][y] = ' ';
                   }
                  
           }
            if(x==12){
             if(y%2 == 0){
             board[x][y] = 'X';
             }
             if(y%2 == 1){
             board[x][y] = ' ';
             }
    
    
           }  
       
        }
    
    
        }
       }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,436
    Well aside from the poor indentation, it seems fine.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void gameboard(char board[16][8])
    {
      int x, y;
    
      for (x = 0; x < 16; x++) {
        printf("%02d=",x);  //!! added for debug
        for (y = 0; y < 8; y++) {
          if (x % 2 == 0) {
            //!! it's not helpful when different rows have different
            //!! numbers of chars printed
            printf(" %c ", board[x][y]);
          } else {
            printf("| %c |", board[x][y]);
          }
        }
        printf("\n");
      }
    }
    
    void character(char board[16][8])
    {
      int x, y;
    
      for (x = 0; x < 16; x++) {
        for (y = 0; y < 8; y++) {
          if (x < 6) {
            if (x % 2 == 0) {
              if (x % 2 == 0) {
                board[x][y] = ' ';
              }
            }
            if (x % 2 == 1) {
              if (y % 2 == 0) {
                board[x][y] = 'O';
              }
              if (y % 2 == 1) {
                board[x][y] = ' ';
              }
            }
            if (x == 3) {
              if (y % 2 == 1) {
                board[x][y] = 'O';
              }
              if (y % 2 == 0) {
                board[x][y] = ' ';
              }
            }
          }
    
          if ((x == 6) || (x == 7) || (x == 8) || (x == 9)) {
            board[x][y] = ' ';
          }
    
          if (x > 9) {
            if (x % 2 == 0) {
              if (x % 2 == 0) {
                board[x][y] = 'X';
              }
              if (y % 2 == 0) {
                board[x][y] = ' ';
              }
            }
            if (x % 2 == 1) {
              if (y % 2 == 1) {
                board[x][y] = ' ';
              }
            }
            if (x == 12) {
              if (y % 2 == 0) {
                board[x][y] = 'X';
              }
              if (y % 2 == 1) {
                board[x][y] = ' ';
              }
            }
          }
        }
      }
    }
    
    int main()
    {
      char board[16][8] = { { 0 } };
      // any dots printed later mean character() didn't do it's job properly
      memset(board,'.',sizeof(board));
      character(board);
      gameboard(board);
      return 0;
    }
    
    My output
    $ ./a.out 
    00=                        
    01=| O ||   || O ||   || O ||   || O ||   |
    02=                        
    03=|   || O ||   || O ||   || O ||   || O |
    04=                        
    05=| O ||   || O ||   || O ||   || O ||   |
    06=                        
    07=|   ||   ||   ||   ||   ||   ||   ||   |
    08=                        
    09=|   ||   ||   ||   ||   ||   ||   ||   |
    10=    X     X     X     X 
    11=| . ||   || . ||   || . ||   || . ||   |
    12= X     X     X     X    
    13=| . ||   || . ||   || . ||   || . ||   |
    14=    X     X     X     X 
    15=| . ||   || . ||   || . ||   || . ||   |
    But then again, it's not clear what you would call success.

    Why do you need a 16x8 board for checkers?
    You're just polluting a lot of code with a lot of x%2 nonsense, where the extra spacing only really matters in the display routine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Your functions are not well-named. How about
    * init_board (instead of "character")
    * print_board (instead of "gameboard")

    You don't need "board" to be 16x8 just to have a blank line printed between the rows of the board. It should be 8x8 and just hold the pertinent data. The blank lines can be added when it's printed. If you need to add a marker in the blank rows to indicate a particular piece, the position of that marker can be saved in two ints (e.g., markrow, markcol) and passed to the print function.

    Your board initialization function can be simplified (even assuming an 8x8 board). Fist note that, according to Salem's run of your program, not only are the X's misplaced, but the O's are on the wrong squares too. The top left square should be blank (it's a "light square").

    To simplify the process, note that indexing the rows and columns by row % 2 and col % 2 shows that the "dark squares" (the ones that the pieces play on) occur when row % 2 != col % 2. The light squares occur when they're equal.

    Code:
    `  0 1 0 1 0 1 0 1
    0    .   .   .   .
    1  .   .   .   .
    0    .   .   .   .
    1  .   .   .   .
    0    .   .   .   .
    1  .   .   .   .
    0    .   .   .   .
    1  .   .   .   .
    (This assumes numbering rows from top to bottom. If they're numbered from bottom to top then the comparisons are reversed.)

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    5
    Hi guys, thanks for the feedback. The reason why I used 16x8 board because I wanted to insert hyphen '_' on top and bottom of the character starting from the first top left 'X' and move the hyphen to select the character that I want. By the way, I am new to programming, I am here to learn.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,436
    But that hyphen cursor can be added by the display routine, which prints each row of the board on alternate lines of the display.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  2. Replies: 10
    Last Post: 09-09-2012, 02:29 PM
  3. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  4. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread