random number generator and 2d array...

This is a discussion on random number generator and 2d array... within the C Programming forums, part of the General Programming Boards category; i have a declared 2d array... lets say array[15][15]. then inside the array, i want to print something on [0][7],[0][8],[0][9],[1][7],[1][8],[1][9],[2][7],[2][8] ...

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

    random number generator and 2d array...

    i have a declared 2d array... lets say array[15][15]. then inside the array, i want to print something on [0][7],[0][8],[0][9],[1][7],[1][8],[1][9],[2][7],[2][8] and [2][9].
    what i wan to print is either '(empty)' or a 'X' and i want it to be random. so using a random number generator, limit it to 0 and 1. if its 0, then print ' ', if 1 then print 'X'. this is for a tetris game. but i dont really know how to put it into codes. any ideas?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    You're going to have to show some amount of code / effort first sorry.
    One can always try.
    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"

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by Alexius Lim View Post
    i have a declared 2d array... lets say array[15][15]. then inside the array, i want to print something on [0][7],[0][8],[0][9],[1][7],[1][8],[1][9],[2][7],[2][8] and [2][9].
    what i wan to print is either '(empty)' or a 'X' and i want it to be random. so using a random number generator, limit it to 0 and 1. if its 0, then print ' ', if 1 then print 'X'. this is for a tetris game. but i dont really know how to put it into codes. any ideas?
    How many threads do we need yet to complete your tetris game? As I can see, you already know how to iterate through an array. Learning how to generate random numbers shouldn't be hard then.
    I never put signature, but I decided to make an exception.

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    59
    Well I've tried and it wont work, otherwise i wont be posting this thread. The thing is i don't know how to make it to print inside the box that i have made. im quite new to programming c thats why I have a lot that i dont know.

    what i have done so far.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define ROW 15
    #define COLUMN 15
    
    
    void gameboard(char b[ROW][COLUMN])
    {
        int r, c;
     
         
        for(r = -1; r <= ROW; r++)
        {
            for(c = -1; c  <= COLUMN; c++)
            {
                if(r == -1 || r == ROW || c == -1 || c == COLUMN)
                    putchar('*');
                else
                    putchar(b[r][c]);
            }
     
            putchar('\n');
        }
    }
    	
    
    
    void assignblocks(char b[ROW][COLUMN])
    {
    	int rol,col;
    	srand( (unsigned) time(NULL));
    	
    	for (rol = 7;rol<10;rol++)
    	{
    		b[rol][0] = rand()%2;
    		if (b[rol][0] == 1)
    		{
    		b[rol][0]= 'X';
    		printf("%c",b[rol][0]);
    		}
    	
    	}
    	printf("\n");		
    	
    	for (rol = 7;rol<10;rol++)
    	{
    		b[rol][1] = rand()%2;
    		if (b[rol][1] == 1)
    		{
    		b[rol][1]= 'X';
    		printf("%c",b[rol][1]);
    		}
    	
    	}
    	printf("\n");		
    		
    	for (rol = 7;rol<10;rol++)
    	{
    		b[rol][2] = rand()%2;
    		if (b[rol][2] == 1)
    		{
    		b[rol][2]= 'X';
    		printf("%c",b[rol][2]);
    		}
    	}
    }
    
    
    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] = ' ';
    		}
    	}
    	
    	gameboard(box);
    	
    	assignblocks(box);
    	
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    If I understand your problem, you first want to print an empty box like:
    Code:
    ******
    *    *
    *    *
    *    *
    ******
    and later fill that box with some X's like
    Code:
    ******
    *  X *
    *  X *
    * XX *
    ******
    In that case I suggest you reread Matticus' post from a few days ago. The basic idea is to use a 2D-array for the representation of the board and modify it accordingly. Then you would write a display function which prints the current state whenever you need to show the whole board.

    For me it looks like you want to print the changes directly to the screen but that doesn't work with standard functions like printf(). You would need to use a terminal control library like curses.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number generator array
    By werty2480 in forum C Programming
    Replies: 7
    Last Post: 04-08-2013, 08:53 AM
  2. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  3. random number generator
    By begginer in forum C Programming
    Replies: 5
    Last Post: 03-07-2011, 11:12 PM
  4. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  5. Random number generator
    By Labmouse in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2007, 02:23 PM

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