Thread: printing a box using a 2d array...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    59

    printing a box using a 2d array...

    i need some help on how to print a box using 2d arrrays. i have to create a tetris game which a im stuck at this stage... so far i have tired i can only come out with this..

    my coding done so far is as follows:
    Code:
    #include <stdio.h>
    #define ROW 15
    #define COLUMN 15
    
    
    void disp_box (char b[ROW][COLUMN])
    {    
    
    
        int r,c;
    
    
        for(r=0; r<14;r++)
        {
            for(c=0; c<15;c++)
            {
                printf("_ ");
            
            }
            
        printf("\n");
        }            
    }
    int main(void)
    {
    
    
        char box[ROW][COLUMN];
        char name[20] ="AAA" ;
        int x,y;
        
        printf("*************Tetris Game***********\n");
        printf("Please Enter Player Name: ");
        scanf("%s", &name);
        printf("Player Name:%s\n", name);
        
        
        
        for(x=0; x<15; x++)
        {
            for(y=0; y<15; y++)
            {
                box[x][y] = '_';
            }
        }
        
        disp_box(box);
        
        return 0;
    }

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    if you mean print the contents of your array within the function, then where you've got printf("_ ") you can either use

    Code:
    putchar(b[r][c]); 
    // or 
    printf("%c", b[r][c]);
    if you mean add a border around the playfield, then use a couple of loops. Keep in mind that you'll need to adjust your playfield accordingly. Since you've declared an array sized on [ROW][COLUMN] you might as well use those symbolic constants within your loops.

    Code:
    // add a top / bottom border
    
    for(col=0; col < COLUMN; ++col)
    {
        b[0][col] = '-';
        b[ROW - 1][col] = '-';
    }
    
    // add left / right sides
    
    for(row = 1; row < ROW - 2; ++row)
    {
        b[row][0] = '|';
        b[row][COLUMN - 1] = '|';
    }
    I *think*... it's late and my mind wanders more than usual.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    59
    but the problem now is how to print them into a box... where should i add the printf inside the loops?
    Last edited by Alexius Lim; 05-09-2013 at 05:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a 2d array?
    By ChickenChowMinh in forum C Programming
    Replies: 2
    Last Post: 02-11-2013, 10:04 PM
  2. Printing an array of strings until array == NULL
    By mikemhz in forum C Programming
    Replies: 10
    Last Post: 11-04-2011, 01:09 PM
  3. Printing array
    By taurus in forum C Programming
    Replies: 6
    Last Post: 09-04-2008, 06:35 AM
  4. Printing Array help
    By saahmed in forum C Programming
    Replies: 18
    Last Post: 02-22-2006, 10:11 PM
  5. array printing help
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-10-2002, 01:10 PM

Tags for this Thread