"BWIGLEY" <bwigley@ihug.co.nzwrote in message
news:eflam2$1mo$1@lust.ihug.co.nz...
Quote:
"jmcgill" <jmcgill@email.arizona.eduwrote in message
news:rAnTg.509$rS.150@fed1read05...
<snip>
Quote:
Thanks, I've got it working perfectly now. How should I print the
array to the screen?
BTW, here's the source so far:
/*Program outline
Make 5 rooms, randomly placed
Try to select all squares that touch the first square (and select ones
near them etc.)
If all not all squares are selected make 2 more rooms
retry selecting (only ones that don't have selected squares all around
them)
repeat room creation, selection trying etc.
If at any time it's close to having all squares selected delete all
unselected squares
Convert selected squares to floor (1)
*/
#include <stdio.h//input + output
#include <math.h//for floor
#include <stdlib.h//Random #'s
#include <time.h//Random #'s
int room_square[25][25],
selected = 0,
squares = 0,
first_square_x = -1,
first_square_y = 0;
//Make a room
void scr_make_room(void) {
int top_x, roomx, top_y, roomy, xplus, yplus;
//printf("%f\n",(rand()/(RAND_MAX + 1)) * 21);
top_x = rand() / (RAND_MAX / 21 + 1) +1; //floor(random(21))+1
roomx = rand() / (RAND_MAX / 8 + 1) +1; //floor(random(8))+1
top_y = rand() / (RAND_MAX / 21 + 1) +1; //floor(random(16))+1
roomy = rand() / (RAND_MAX / 8 + 1) +1; //floor(random(8))+1
//Turn all the wall squares within the room to floor
for (xplus = 0; xplus < roomx; xplus++) {
for (yplus = 0; yplus < roomy; yplus++) {
// make sure it's in the room
if (top_x + xplus < 24 && top_y + yplus < 24) {
if (room_square[top_x + xplus][top_y + yplus] == 0) {
room_square[top_x + xplus][top_y + yplus] = 1;
//make it floor
squares++; //increase the number of squares made
//record the first square made(ever)
if (first_square_x == -1) {
first_square_x = top_x + xplus;
first_square_y = top_y + yplus;
} //endi first square
} //endi wall
} //endi in room
} //end for y
} //end for x
} //end scr_make_room()
void scr_select_squares(int square_x, int square_y) {
int amnt = 0;
if ((room_square[square_x-1][square_y] == -1 ||
room_square[square_x-1][square_y] == -2) && //if all 'round it's
selected be done
(room_square[square_x+1][square_y] == -1 ||
room_square[square_x+1][square_y] == -2) &&
(room_square[square_x][square_y-1] == -1 ||
room_square[square_x][square_y-1] == -2) &&
(room_square[square_x][square_y+1] == -1 ||
room_square[square_x][square_y+1] == -2)) {
room_square[square_x][square_y] = -2;
}
else { //if they're not all selected, try to do it
//select to the left
if (square_x-1 0) { //in the room
if (room_square[square_x-1][square_y] == 1) { //if it's
floor
room_square[square_x-1][square_y] = -1; //Select it
selected++; //increase no. selected
scr_select_squares(square_x-1, square_y); //select
squares around it
}
}
//right
if (square_x+1 < 24) { //in the room
if (room_square[square_x+1][square_y] == 1) { //if it's
floor
room_square[square_x+1][square_y] = -1; //Select it
selected++; //increase no. selected
scr_select_squares(square_x+1, square_y); //select
squares around it
}
}
//Up
if (square_y - 1 0) { //in the room
if (room_square[square_x][square_y-1] == 1) { //if it's
floor
room_square[square_x][square_y-1] = -1; //Select it
selected++; //increase no. selected
scr_select_squares(square_x, square_y-1); //select
squares around it
}
}
//Down
if (square_y + 1 < 24) { //in the room
if (room_square[square_x][square_y+1] == 1) { //if it's
floor
room_square[square_x][square_y+1] = -1; //Select it
selected++; //increase no. selected
scr_select_squares(square_x, square_y+1); //select
squares around it
}
}
}
}
int main(int argc, char** argv) {
int i, j;
srand((unsigned)time(NULL)); //Init rng
for (i = 0; i < 25; i++) {
for (j = 0; j < 25; j++) {
room_square[i][j] = 0;
}
}
for (i = 0; i < 5; i++) {
scr_make_room();
}
//select the first square
room_square[first_square_x][first_square_y] = -1;
selected++;
//select all squares around it
scr_select_squares(first_square_x, first_square_y);
//***ROOM GENERATION LOOP!!!
while (selected != squares) {
//select squares
for (i=1; i<24; i++) {
for (j=1; j<24; j++) {
if (room_square[i][j] == -1) {
scr_select_squares(i,j);
}
}
}
//lets try printing it:
for (i=0; i<25; i++) {
printf("\n");
for (j=0; j<25; j+=1) {
switch (room_square[i][j]){
case 0:{
printf("0");
};break;
case 1:{
printf(".");
};break;
case -1:{
printf("s");
};break;
case -2:{
printf("-");
};break;
}
//printf("%d", room_square[i][j]);
}
}
printf("\n");
getch();
//if most squares are selected delete the unselected ones
if (squares < selected+5 && selected!=squares) {
for (i=1; i<24; i++) {
for (j=1; j<24; j++) {
if (room_square[i][j] == 1) {
room_square[i][j] = 0;
squares--;
}
}
}
}
//make some rooms if theres squares still unselected squares
if (selected != squares) {
scr_make_room();
scr_make_room();
}
}//end while
//***Now we have one contigous space of floor!
//make selected spaces floor
for (i=1; i<24; i++) {
for (j=1; j<24; j++) {
if (room_square[i][j] == -1 || room_square[i][j] == -2) {
room_square[i][j] = 1;
}
}
}
//lets try printing it:
for (i=0; i<25; i++) {
printf("\n");
for (j=0; j<25; j+=1) {
switch (room_square[i][j]){
case 0:{
printf("0");
};break;
case 1:{
printf(".");
};break;
}
//printf("%d", room_square[i][j]);
}
}
printf("\nsquares: %d", squares);
printf("\nselected: %d", selected);
getch(); //just so it doesn't exit on windoze
return 0;
}