fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int clamp(int n,int min,int max){
  6. int rnv = n;
  7. if(n >= min){
  8. if(n <= max) rnv = n;
  9. else rnv = max;
  10. }
  11. else rnv = min;
  12. return rnv;
  13. }
  14.  
  15. void map_generator(int w, int h) {
  16. int world[h][w];
  17. for(int i = 0;i < h;i++) for(int j = 0;j < w;j++) world[i][j] == 0;
  18. int cx = w/2, cy = h/2;
  19. int cdir = rand() % 3;
  20. int cnt = 0;
  21.  
  22. while(cnt < 30) {
  23. cnt++;
  24. switch(cdir) {
  25. case 0: cy--; break;
  26. case 1: cx++; break;
  27. case 2: cy++; break;
  28. case 3: cx--; break;
  29. }
  30. cx = clamp(cx, 0, w-1);
  31. cy = clamp(cy, 0, h-1);
  32. printf("cx : %d cy : %d\n",cx,cy);
  33. world[cy][cx] = 1;
  34. for(int i = 0;i < h;i++){
  35. for(int j = 0;j < w;j++){
  36. if(world[i][j] == 1) printf("■");
  37. else printf("□");
  38. }
  39. printf("\n");
  40. }
  41. cdir = rand() % 3;
  42. }
  43.  
  44. for(int i = 0;i < h;i++){
  45. for(int j = 0;j < w;j++){
  46. if(world[i][j] == 1) printf("■");
  47. else printf("□");
  48. }
  49. printf("\n");
  50. }
  51. }
  52.  
  53. int main(void) {
  54. int width = 10, height = 8;
  55. srand(time(NULL));
  56. map_generator(width,height);
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
cx : 5  cy : 5
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□□□□□□
□□□□□□□□□□
cx : 5  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■□□□□
□□□□□□□□□□
cx : 6  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■□□□
□□□□□□□□□□
cx : 6  cy : 7
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■□□□
□□□□□□■□□□
cx : 6  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■□□□
□□□□□□■□□□
cx : 6  cy : 7
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■□□□
□□□□□□■□□□
cx : 6  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■□□□
□□□□□□■□□□
cx : 6  cy : 7
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■□□□
□□□□□□■□□□
cx : 7  cy : 7
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■□□□
□□□□□□■■□□
cx : 7  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■■□□
□□□□□□■■□□
cx : 8  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■■■□
□□□□□□■■□□
cx : 9  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■■■■
□□□□□□■■□□
cx : 9  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■■■■
□□□□□□■■□□
cx : 9  cy : 7
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□□
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 5
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 5
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 6
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 5
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□□
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 4
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□■
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 5
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□■
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 4
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□■
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 4
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□■
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 4
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□■
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 4
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□□
□□□□□□□□□■
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 3
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□□
□□■□□□□□□■
□□□□□□□□□■
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
cx : 9  cy : 2
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□■
□□■□□□□□□■
□□□□□□□□□■
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■
□□□□□□□□□□
□□□□□□□□□□
□□□□□□□□□■
□□■□□□□□□■
□□□□□□□□□■
□□□□□■□□□■
□□□□□■■■■■
□□□□□□■■□■