Tips for drawing a grid

This is a discussion on Tips for drawing a grid within the C Programming forums, part of the General Programming Boards category; Hello, I am new to C programming and I am trying to draw a grid for a game of checkers. ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    19

    Tips for drawing a grid

    Hello, I am new to C programming and I am trying to draw a grid for a game of checkers. Each square in the grid will contain a member of a two-dimensional array.

    Every attempt at displaying a nice-looking grid has failed miserably. I am sure there are some members here who have done similar things and I was wondering if there are any tips or tricks for making a nice looking grid in the output.

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Code:
    for (j = 0; j < NUM_ROWS; j++)
    {
       for (k = 0; k < NUM_COLS; k++)
       {
          printf("&#37;2c", grid[j][k]);
       }
       printf("\n");
    }
    I've found, on my display, that setting a width of two, like indicated in red above, seems to correspond pretty well to a newline in terms of neat spacing. Is this what you're looking for?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How are you drawing the grid? On what platform? Generally, a grid is simply lines going horizontally and vertically to form a nice set of squares. If the grid is square (like for checkers), you can do this by drawing lines both horizontally and vertically at the same time. Something like this:
    Code:
    const int cellsize = 20;   // 20 pixels wide/high cells. 
    void grid(int gridsize)
    {
        int i;
        for(i = 0; i < size; i++)
        {
           DrawLine(0, i * cellsize, size * cellsize, i * cellsize); 
           DrawLine(i * cellsize, 0, i * cellsize, size * cellsize);
         }
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Thanks for the help. Is DrawLine a built in function? What do the parameters mean?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by countchocula View Post
    Thanks for the help. Is DrawLine a built in function? What do the parameters mean?
    DrawLine would be some graphics function that you probably have to write yourself - the parameters are X and Y coordinates for the start and end points of a line. Of course, this assumes you are in a graphical environment.

    For example, in Windows, you'd use MoveTo(x1, y1); LineTo(x2, y2);

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Quote Originally Posted by JDGATX View Post
    Code:
    for (j = 0; j < NUM_ROWS; j++)
    {
       for (k = 0; k < NUM_COLS; k++)
       {
          printf("%2c", grid[j][k]);
       }
       printf("\n");
    }
    I've found, on my display, that setting a width of two, like indicated in red above, seems to correspond pretty well to a newline in terms of neat spacing. Is this what you're looking for?
    This works great for simply displaying the contents of the array in a square, however I would like to be able to do this with the gridlines around each unit.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Quote Originally Posted by matsp View Post
    DrawLine would be some graphics function that you probably have to write yourself - the parameters are X and Y coordinates for the start and end points of a line. Of course, this assumes you are in a graphical environment.

    For example, in Windows, you'd use MoveTo(x1, y1); LineTo(x2, y2);

    --
    Mats
    Unfortunately I am just trying to make a console app. I don't have any experience with graphics. (Maybe this is what is causing my frustrations!)

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    This works great for simply displaying the contents of the array in a square, however I would like to be able to do this with the gridlines around each unit.
    I think I know what you mean, but I dont think you could do it as you want it. You could use "|" to seperate the cols and "--" to seperate the rows but the rows would be abit far from each other.

    What I did when I needed to draw a board (for a game like checkers) is used "[" and "]".
    That way modify the code JDGATX gave to you to be:

    Code:
      for (j = 0; j < NUM_ROWS; j++)
        {
          for (k = 0; k < NUM_COLS; k++)
            printf("[%c]", grid[j][k]);
          printf("\n");
        }
      return (0);

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Quote Originally Posted by +Azazel+ View Post
    I think I know what you mean, but I dont think you could do it as you want it. You could use "|" to seperate the cols and "--" to seperate the rows but the rows would be abit far from each other.

    What I did when I needed to draw a board (for a game like checkers) is used "[" and "]".
    That way modify the code JDGATX gave to you to be:

    Code:
      for (j = 0; j < NUM_ROWS; j++)
        {
          for (k = 0; k < NUM_COLS; k++)
            printf("[%c]", grid[j][k]);
          printf("\n");
        }
      return (0);
    Thanks, that is what I was trying to do before but I kept getting ugly outputs.

    I think I will simply use brackets. Thanks for the help guys.

  10. #10
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Heres some code I wrote for drawing a chessboard a while back. It uses non-standard ASCII characters though so may not display correctly on all machines:
    Code:
    void DrawBoard()
    {
        for(int y=0; y<18; y++)
            for(int x=0; x<26; x++)
                if(y==0)
                    if(x==25)       printf(" \n");
                    else if(x%3==2) putchar(65+(x/3));   
                    else            putchar(' ');
                else if (y==1)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(218);
                    else if(x==25)  printf("%c\n", 191);
                    else if(x%3==1) putchar(194); 
                    else            putchar(196);
                else if(y==17)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(192);
                    else if(x==25)  printf("%c\n", 217);
                    else if(x%3==1) putchar(193); 
                    else            putchar(196);
                else if(y%2==1)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(195);
                    else if(x==25)  printf("%c\n", 180);
                    else if(x%3==1) putchar(197); 
                    else            putchar(196);
                else  
                    if (x==0)       putchar(48+(y/2));
                    else if(x==25)  printf("%c\n", 179);
                    else if(x%3==1) putchar(179); 
                    else if(x%3==2) printf("  ");;// <- Cell content goes here
    }

  11. #11
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    When I made my first game, it was tic-tac-toe. I did a really ugly way of doing it which was something like this:

    Code:
    void drawGrid()
    {
        cout << grid[0][0] << "|" << grid[0][1] << "|" << grid[0][3] << endl;
        cout << "_____" << endl;
        cout << grid[1][0] << "|" << grid[1][1] << "|" << grid[1][3] << endl;
    
         ...
    }
    Sometimes it's neat going to code you wrote when you were first starting out ^_^
    What is C++?

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Quote Originally Posted by mike_g View Post
    Heres some code I wrote for drawing a chessboard a while back. It uses non-standard ASCII characters though so may not display correctly on all machines:
    Code:
    void DrawBoard()
    {
        for(int y=0; y<18; y++)
            for(int x=0; x<26; x++)
                if(y==0)
                    if(x==25)       printf(" \n");
                    else if(x%3==2) putchar(65+(x/3));   
                    else            putchar(' ');
                else if (y==1)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(218);
                    else if(x==25)  printf("%c\n", 191);
                    else if(x%3==1) putchar(194); 
                    else            putchar(196);
                else if(y==17)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(192);
                    else if(x==25)  printf("%c\n", 217);
                    else if(x%3==1) putchar(193); 
                    else            putchar(196);
                else if(y%2==1)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(195);
                    else if(x==25)  printf("%c\n", 180);
                    else if(x%3==1) putchar(197); 
                    else            putchar(196);
                else  
                    if (x==0)       putchar(48+(y/2));
                    else if(x==25)  printf("%c\n", 179);
                    else if(x%3==1) putchar(179); 
                    else if(x%3==2) printf("  ");;// <- Cell content goes here
    }
    That looks like the kind of thing that would give a nice result, but it has a disturbing lack of open and close braces.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, I guess that would be one of my bad habits. At least I wasent fitting several lines of code into one using the comma operator to avoid braces. Something I have done occasionally in the past.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with grid problem (dynamic programming)
    By scp89 in forum C Programming
    Replies: 15
    Last Post: 05-07-2009, 12:16 PM
  2. More Windows Trouble
    By samGwilliam in forum Windows Programming
    Replies: 9
    Last Post: 04-12-2005, 10:02 AM
  3. Cleaning up a Grid of Memory
    By Epo in forum C++ Programming
    Replies: 9
    Last Post: 02-25-2005, 09:23 AM
  4. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 04:38 PM
  5. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM

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