Thread: Select and move in 2d array.Help!

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

    Select and move in 2d array.Help!

    I am designing a 8x8 2d array checkers game. How can I select a character to print the hyphen '-' on the top and bottom of the character and by moving the hyphen to select the character I want. Let's say the character 'x' is selected, I can move the hyphen '-' to select the 'x' that I want.
    But my code is not working, I really need help. I had attached the sample below.

    Code:
    int gameboard(char board[8][8])
    {
    
    
     int x, y;
     
      for(x=0; x<8; x++)
      {
       for(y=0; y<8; y++)
        {
        printf("=---=");
        }
        printf("\n\n");
        
        for(y=0;y<8;y++)
        {
        printf("| %c |",board[x][y]);
        }
        printf("\n\n");
      }
      
      
      for(x=0;x<8;x++)
      {
      printf("=---=");
      }
      
    
    
    }
    void character(char board[8][8]){
     int x,y;
     
     for(x=0;x<8;x++){
       for(y=0;y<8;y++){
        if(x<3){
            
           if(x%2 == 0){
                  if(x%2 == 0){
                  board[x][y] = 'O';
              
                  }
                  if(y%2==1){
                  board[x][y]= ' ';
                 
                  }
           }
           if(x%2 == 1){
                  if(y%2 == 0){
                  board[x][y] = ' ';
                   }
                  if(y%2 ==1){
                 board[x][y]= 'O';
                  
                  }
           }
    
    
        }
        
        if((x==3) || (x==4)){
        board[x][y] = ' ';
        } 
    
    
        if(x>4)
        {
               
               if(x%2 == 0){
                      if(y%2 == 0){
                      board[x][y] = 'X';
                      
                      }
                      if(y%2 ==1){
                      board[x][y]= ' ';
                      
                      }
               }
    
    
               if(x%2 == 1){
                      if(y%2 == 0){
                      board[x][y] = ' ';
                      
                       }
                      if(y%2 ==1){
                      board[x][y]= 'X';
                      
                      }
               }
               
        
        for (x = 0; x < 8; x++) {
            for (y = 0; y < 8; y++) {
                if (x == 5 && y == 1) {
                    board[x-1][y] = '-';
                    board[x+1][y] = '-';
                }
            }
        }          
               
        }
    
    
        }
       }
    
    }
    This is what is want.[sample]
    Select and move in 2d array.Help!-sdd-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I would first suggest you split the function up into more manageable tasks.

    - drawXs
    - drawOs
    This may be combined into a single function where 'X' and 'O' are a parameter, if the code for one is a copy/paste of the other one with a few edits.

    - drawCursor
    This draws your '-' either side of a given position.

    Your indentation could be more consistent as well.
    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. Move A character in 2D array
    By thedardwhie in forum C Programming
    Replies: 4
    Last Post: 01-27-2015, 08:03 AM
  2. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  3. Move struct into array of structs?
    By Dest in forum C Programming
    Replies: 4
    Last Post: 05-05-2010, 10:55 AM
  4. Help using Cursor to move an array into a new table
    By cjohnman in forum C Programming
    Replies: 3
    Last Post: 04-17-2008, 11:34 AM
  5. Replies: 12
    Last Post: 12-06-2005, 08:30 PM

Tags for this Thread