# Thread: random number generator and 2d array...

1. ## 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. You're going to have to show some amount of code / effort first sorry.
One can always try.

3. Originally Posted by Alexius Lim
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.

4. 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");
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. 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