Like Tree2Likes

Draw a grid using the following characters?

This is a discussion on Draw a grid using the following characters? within the C Programming forums, part of the General Programming Boards category; Hey there! I'm trying to create a grid using the following characters: !, @, #, $, &. It's an 8 ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    16

    Question Draw a grid using the following characters?

    Hey there! I'm trying to create a grid using the following characters: !, @, #, $, &.

    It's an 8 x 8 grid, and should contain the characters in no particular order (trying to create a Bejeweled-like game).

    I already defined the width and length to be 8, so:
    #define WIDTH 8
    #define LENGTH 8

    Code:
    void makegrid(char grid[]) 
     { 
           int j;    
           int k; 
       
          for ( j = -1; j < LENGTH; j++ )         
                 {             
                      for ( k = -1; k < WIDTH; k++ ) 
                      {                     
                               /* I don't know what to type here anymore  :( */                    
                      }            
                 }    
    }
    Last edited by programmingnewb; 08-20-2013 at 02:46 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,079
    You should consider using a multidimensional array (have a read).

    Why are you starting the variables at -1 in your loops?
    Adak likes this.

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Assuming the -1 in the loops is meant to be 0, you probably want something like

    Code:
    grid[j * LENGTH + k] = random_grid_char();
    I leave the implementation of random_grid_char() as an exercise.

    Edit: Some reading Row-major order - Wikipedia, the free encyclopedia
    Last edited by SirPrattlepod; 08-20-2013 at 07:41 PM.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Um, hello! is random_grid_char a function? Sorry, not really good with C yet.

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by programmingnewb View Post
    Um, hello! is random_grid_char a function? Sorry, not really good with C yet.
    Nope. You need to write it yourself.

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    I meant, I'd need to write it as a function?

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by programmingnewb View Post
    I meant, I'd need to write it as a function?
    If you like. It would be a one line function so you could just as easily do it without.

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Oh, okay, thank you sir!

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Matticus View Post
    You should consider using a multidimensional array (have a read).

    Why are you starting the variables at -1 in your loops?
    ^^^^ Good advice there.

    This is not code for a program. This is code to nudge you in the right direction, only. Fresh off the cuff, and won't compile as is, but it should get you started OK.

    Code:
    #include <time.h>
    
    
    #define ROWS 8
    #define COLS 8
    
    
    srand(time());
    
    char grid[ROWS][COLs], temp;
    int r, c, r1,c1,i;
    
    //initialize the board grid
    for(r=0;r<ROWS;r++) {
       for(c=0;c<COLS;c++) {
          if(r<2 )
              grid[r][c]='@';
          else if(r<4)
              grid[r][c]='#';
          else if(r<6)
              grid[r][c]='*';
          else
              grid[r][c]='!';
       }
    }
    
    //now randomize the grid by making a lot of random swaps
    for(i=0;i<100;i++) {
        r=rand() % ROWS;
        c=rand() % COLS;
        r1=rand() % ROWS;
        c1=rand() % COLS;
        temp = grid[r][c];
        grid[r][c]= grid[r1][c1];
        grid[r1][c1]=temp;
    }

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Hi sir/maam! What are the r < 2, r < 4, for?

  11. #11
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Adak View Post
    ^^^^ Good advice there.

    This is not code for a program. This is code to nudge you in the right direction, only. Fresh off the cuff, and won't compile as is, but it should get you started OK.

    Code:
    #include <time.h>
    
    
    #define ROWS 8
    #define COLS 8
    
    
    srand(time());
    
    char grid[ROWS][COLs], temp;
    int r, c, r1,c1,i;
    
    //initialize the board grid
    for(r=0;r<ROWS;r++) {
       for(c=0;c<COLS;c++) {
          if(r<2 )
              grid[r][c]='@';
          else if(r<4)
              grid[r][c]='#';
          else if(r<6)
              grid[r][c]='*';
          else
              grid[r][c]='!';
       }
    }
    
    //now randomize the grid by making a lot of random swaps
    for(i=0;i<100;i++) {
        r=rand() % ROWS;
        c=rand() % COLS;
        r1=rand() % ROWS;
        c1=rand() % COLS;
        temp = grid[r][c];
        grid[r][c]= grid[r1][c1];
        grid[r1][c1]=temp;
    }
    I guess you could do it that way. It's a complicated way, though.

  12. #12
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Do you have any idea what the r < 2, r < 4 are supposed to be?

  13. #13
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by programmingnewb View Post
    Do you have any idea what the r < 2, r < 4 are supposed to be?
    Not really, sorry :-(

    I sent Adak a PM showing him how I'd do it, but I don't want to be seen as giving the answer to you :-(

    It really is a simple as using a 1-dimensional array and "addressing" it yourself using either row-major or column-major arithmetic (it doesn't matter which). Personally I wouldn't use a 2-dimensional array for this, but *shrug*.

  14. #14
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Oh,it's okay! But everyone I've asked (programmers from League of Legends I befriended) said I should use a 2D array I'll just try to figure it out XD

  15. #15
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I did write an example. It can be done better, but it'll work.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define LENGTH 8
    #define WIDTH  8
    
    static const char grid_chars[] = {'!', '@', '#', '$', '&'};
    static const int num_grid_chars = sizeof grid_chars / sizeof grid_chars[0];
    
    char random_grid_char(void)
    {
        return grid_chars[rand() % num_grid_chars];
    }
    
    void makegrid(char *grid)
    {
        int j;
        int k;
    
        for (j = 0; j < LENGTH; j++)
        {
            for (k = 0; k < WIDTH; k++)
            {            
                grid[j * LENGTH + k] = random_grid_char();
            }
        }
    }
    
    void printgrid(char *grid)
    {
        int j;
        int k;
    
        for (j = 0; j < LENGTH; j++)
        {
            for (k = 0; k < WIDTH; k++)
            {
                printf("%c", grid[j * LENGTH + k]);
            }
            printf("\n");
        }
    }
    
    int main(void)
    {
        char grid[WIDTH * LENGTH];
        makegrid(grid);
        printgrid(grid);
        return 0;
    }

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a grid
    By karimoftos in forum C Programming
    Replies: 6
    Last Post: 04-19-2011, 11:32 AM
  2. Grid
    By mortalc in forum C Programming
    Replies: 7
    Last Post: 03-12-2011, 05:23 AM
  3. Plz, teach me how to draw a grid!
    By Messiah in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2010, 11:16 AM

Tags for this Thread


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