Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. #define N 5
  5.  
  6. void init(char dungeon[][80]){
  7. int row, col;
  8. for(row = 0; row < 21; row++){
  9. for(col = 0; col < 80; col++){
  10. if(row == 0 || row == 20){
  11. dungeon[row][col] = '-';
  12. } else if(col == 0 || col == 79) {
  13. dungeon[row][col] = '|';
  14. } else {
  15. dungeon[row][col] = ' ';
  16. }
  17. }
  18. }
  19.  
  20. }
  21. void printDungeon(char dungeon[][80]){
  22.  
  23. int row, col;
  24. for(row = 0; row < 21; row++)
  25. {
  26. for(col = 0; col < 80; col++)
  27. {
  28. printf("%c", dungeon[row][col]);
  29. }
  30. printf("\n");
  31. }
  32.  
  33. }
  34.  
  35. //Writes the rooms in the rooms array to the dungeon array as periods.
  36. void parseRooms(int rooms[][4], char dungeon[21][80]){
  37. int n,row,col;
  38. for (n=0; n<N; n++)
  39. {
  40. for(row = rooms[n][1]; row < rooms[n][1] + rooms[n][3]; row++){
  41. for(col = rooms[n][0]; row < rooms[n][0] + rooms[n][2]; col++){
  42. dungeon[row][col] = '.';
  43. }
  44. }
  45. }
  46. }
  47.  
  48. int checkRoom(int rooms[][4], int xPos, int yPos, int xSize, int ySize)
  49. {
  50. if (xPos + xSize > 79 || yPos + ySize > 20){
  51. return 0; }
  52.  
  53. int n, i, j;
  54. for (n = 0; n < N; n++)
  55. {
  56. for(i = rooms[n][0]; i < rooms[n][0] + rooms[n][2]; i++){
  57. for(j = rooms[n][1]; j < rooms[n][1] + rooms[n][3]; j++){
  58. if(i >= - 1 && i <= xPos + xSize && j >= yPos && j <= yPos +ySize){
  59. return 0;
  60. }
  61. }
  62. }
  63.  
  64. }
  65. return 1;
  66.  
  67. }
  68.  
  69.  
  70. void genRooms(int numRooms, int rooms[][4]){
  71. int cur = 0;
  72. srand(time(NULL));
  73. int xPos, yPos, xSize, ySize;
  74. while (cur < numRooms) {
  75. xPos = (rand() % 78) + 1;
  76. yPos = (rand() % 19) + 1;
  77. xSize = 5 + (rand() % 10);
  78. ySize = 3 + (rand() % 5);
  79. }
  80. if (checkRoom(rooms, xPos, yPos, xSize, ySize)) {
  81. rooms[cur][0] = xPos;
  82. rooms[cur][1] = yPos;
  83. rooms[cur][2] = xSize;
  84. rooms[cur][3] = ySize;
  85. cur++;
  86. }
  87. }
  88.  
  89. int main()
  90. {
  91.  
  92. char dungeon[21][80];
  93. int rooms[N][4]; // Stores Xpos, Ypos, Xsize, Ysize
  94. init(dungeon);
  95. genRooms(N, rooms);
  96. parseRooms(rooms, dungeon);
  97. printDungeon(dungeon);
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement