Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #define N 5
- void init(char dungeon[][80]){
- int row, col;
- for(row = 0; row < 21; row++){
- for(col = 0; col < 80; col++){
- if(row == 0 || row == 20){
- dungeon[row][col] = '-';
- } else if(col == 0 || col == 79) {
- dungeon[row][col] = '|';
- } else {
- dungeon[row][col] = ' ';
- }
- }
- }
- }
- void printDungeon(char dungeon[][80]){
- int row, col;
- for(row = 0; row < 21; row++)
- {
- for(col = 0; col < 80; col++)
- {
- printf("%c", dungeon[row][col]);
- }
- printf("\n");
- }
- }
- //Writes the rooms in the rooms array to the dungeon array as periods.
- void parseRooms(int rooms[][4], char dungeon[21][80]){
- int n,row,col;
- for (n=0; n<N; n++)
- {
- for(row = rooms[n][1]; row < rooms[n][1] + rooms[n][3]; row++){
- for(col = rooms[n][0]; row < rooms[n][0] + rooms[n][2]; col++){
- dungeon[row][col] = '.';
- }
- }
- }
- }
- int checkRoom(int rooms[][4], int xPos, int yPos, int xSize, int ySize)
- {
- if (xPos + xSize > 79 || yPos + ySize > 20){
- return 0; }
- int n, i, j;
- for (n = 0; n < N; n++)
- {
- for(i = rooms[n][0]; i < rooms[n][0] + rooms[n][2]; i++){
- for(j = rooms[n][1]; j < rooms[n][1] + rooms[n][3]; j++){
- if(i >= - 1 && i <= xPos + xSize && j >= yPos && j <= yPos +ySize){
- return 0;
- }
- }
- }
- }
- return 1;
- }
- void genRooms(int numRooms, int rooms[][4]){
- int cur = 0;
- srand(time(NULL));
- int xPos, yPos, xSize, ySize;
- while (cur < numRooms) {
- xPos = (rand() % 78) + 1;
- yPos = (rand() % 19) + 1;
- xSize = 5 + (rand() % 10);
- ySize = 3 + (rand() % 5);
- }
- if (checkRoom(rooms, xPos, yPos, xSize, ySize)) {
- rooms[cur][0] = xPos;
- rooms[cur][1] = yPos;
- rooms[cur][2] = xSize;
- rooms[cur][3] = ySize;
- cur++;
- }
- }
- int main()
- {
- char dungeon[21][80];
- int rooms[N][4]; // Stores Xpos, Ypos, Xsize, Ysize
- init(dungeon);
- genRooms(N, rooms);
- parseRooms(rooms, dungeon);
- printDungeon(dungeon);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement