Advertisement
valiamaximova1

dd

May 31st, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. // Online C compiler to run C program online
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <math.h>
  6.  
  7. enum TERRAIN_TYPE {
  8. GREEN,
  9. WASTELAND,
  10. WATER,
  11. ROAD,
  12. BUILDING
  13. };
  14.  
  15. typedef struct terrain {
  16. float height;
  17. int type;
  18. float attendance;
  19. } Terrain;
  20.  
  21. // Function prototypes
  22. int generateRandomInt(const int from, const int to);
  23. float generateDiff();
  24. int generateSign();
  25. float generateMultiplier();
  26. float generateFirstHeight();
  27. Terrain** generateEmptyMap(const int height, const int width);
  28. void DeleteMap(Terrain **map, const int height);
  29. Terrain **fillMapHeight(Terrain **map, const int height, const int width);
  30. Terrain **fillMapType(Terrain **map, const int height, const int width);
  31. void PrintMapByTerrainType(Terrain **map, const int height, const int width);
  32. void PrintMapByTerrainHeight(Terrain **map, const int height, const int width);
  33. void BuildRoad(Terrain **map, const int height, const int width);
  34. void ExportMap(Terrain **map, const int height, const int width, const char *file_name);
  35. Terrain** ImportMap(const char *file_name, int *height, int *width);
  36. float calculateIncline(float h1, float h2);
  37. int checkIncline(float h1, float h2, float h3, float h4, float h5);
  38. void PrintJumpingRoutes(Terrain **map, const int height, const int width);
  39.  
  40. // Generate random int between 'from' and 'to'
  41. int generateRandomInt(const int from, const int to) {
  42. return rand() % (to - from) + from;
  43. }
  44.  
  45. // Generate random floating point number between 0.0 and 1.0
  46. float generateDiff() {
  47. return ((float)generateRandomInt(1, 101)) / 100;
  48. }
  49.  
  50. // Flip a coin to pick a sign (+ or -). Returns +1 or -1 as a result
  51. int generateSign() {
  52. if (generateRandomInt(1, 1001) % 2) {
  53. return -1;
  54. }
  55. return 1;
  56. }
  57.  
  58. // Generate random floating point number between 0.5 and 1.5
  59. float generateMultiplier() {
  60. return ((float)generateRandomInt(20, 150)) / 100;
  61. }
  62.  
  63. // Generate a random floating point number between -1.0 & 1.0
  64. float generateFirstHeight() {
  65. return ((float)generateRandomInt(1, 20001)) / 10000.0 - 1.0;
  66. }
  67.  
  68. // Generate an empty map
  69. Terrain** generateEmptyMap(const int height, const int width) {
  70. if (height < 1 || width < 1) {
  71. fprintf(stderr, "Invalid size!\n");
  72. return NULL;
  73. }
  74.  
  75. Terrain **emptyMap = malloc(height * sizeof(Terrain *));
  76. for (int i = 0; i < height; ++i) {
  77. emptyMap[i] = malloc(width * sizeof(Terrain));
  78. }
  79. return emptyMap;
  80. }
  81.  
  82. void DeleteMap(Terrain **map, const int height) {
  83. for (int row = 0; row < height; ++row) {
  84. free(map[row]);
  85. }
  86. free(map);
  87. }
  88.  
  89. Terrain **fillMapHeight(Terrain **map, const int height, const int width) {
  90. for (int col = 0; col < width; ++col) {
  91. for (int row = 0; row < height; ++row) {
  92. if (row == 0) {
  93. map[row][col].height = generateFirstHeight();
  94. continue; // Skip to next iteration
  95. }
  96. // Current height = previousHeight + (random float between 0.0 and +1.0) * (multiplier between 0.5 and 1.5) * sign
  97. map[row][col].height = (map[row - 1][col].height + generateDiff()) * generateMultiplier() * generateSign();
  98. }
  99. }
  100.  
  101. return map;
  102. }
  103.  
  104. Terrain **fillMapType(Terrain **map, const int height, const int width) {
  105. for (int row = 0; row < height; ++row) {
  106. for (int col = 0; col < width; ++col) {
  107. map[row][col].type = generateRandomInt(0, 5);
  108. }
  109. }
  110.  
  111. return map;
  112. }
  113.  
  114. void PrintMapByTerrainType(Terrain **map, const int height, const int width) {
  115. if (!map) {
  116. fprintf(stderr, "Invalid Pointer\n");
  117. return;
  118. }
  119. if (height < 1 || width < 1) {
  120. fprintf(stderr, "Incorrect map size!\n");
  121. }
  122.  
  123. for (int row = 0; row < height; ++row) {
  124. for (int col = 0; col < width; ++col) {
  125. printf("%d ", map[row][col].type);
  126. }
  127. printf("\n");
  128. }
  129.  
  130. return;
  131. }
  132.  
  133. void PrintMapByTerrainHeight(Terrain **map, const int height, const int width) {
  134. if (!map) {
  135. fprintf(stderr, "Invalid Pointer\n");
  136. return;
  137. }
  138. if (height < 1 || width < 1) {
  139. fprintf(stderr, "Incorrect map size!\n");
  140. }
  141.  
  142. for (int row = 0; row < height; ++row) {
  143. for (int col = 0; col < width; ++col) {
  144. printf("%5.2f ", map[row][col].height);
  145. }
  146. printf("\n");
  147. }
  148.  
  149. return;
  150. }
  151.  
  152. void BuildRoad(Terrain **map, const int height, const int width) {
  153. printf("To exit, enter -1.\n");
  154. printf("Otherwise, enter coordinates of the field you'd like to transform to road.\n");
  155.  
  156. int row, col, previousRow, previousCol, roadLength = 0;
  157. do {
  158. scanf("%d", &row);
  159. if (row == -1) {
  160. return;
  161. }
  162.  
  163. scanf("%d", &col);
  164. if (col == -1) {
  165. return;
  166. }
  167.  
  168. if (row < 0 || row >= height || col < 0 || col >= width) {
  169. printf("Coordinates are out of the map. Please input valid coordinates\n");
  170. continue;
  171. }
  172.  
  173. if (roadLength > 0) {
  174. if (abs(previousRow - row) + abs(previousCol - col) > 1) {
  175. printf("Road builders can only move 1 square away each time.\n");
  176. continue;
  177. }
  178. }
  179.  
  180.  
  181. map[row][col].type = ROAD;
  182. previousRow = row;
  183. previousCol = col;
  184. roadLength++;
  185. } while (1);
  186. }
  187.  
  188. void ExportMap(Terrain **map, const int height, const int width, const char *file_name) {
  189. if (!file_name || !map) {
  190. fprintf(stderr, "Invalid pointer!\n");
  191. return;
  192. }
  193.  
  194. for (int i = 0; i < height; ++i) {
  195. if (!map[i]) {
  196. fprintf(stderr, "Invalid pointer!\n");
  197. return;
  198. }
  199. }
  200.  
  201. if (height < 1 || width < 1) {
  202. fprintf(stderr, "Invalid map size!\n");
  203. return;
  204. }
  205.  
  206. FILE *export = fopen(file_name, "wb");
  207. if (!export) {
  208. fprintf(stderr, "File failed to open!\n");
  209. }
  210.  
  211. fwrite(&width, sizeof(int), 1, export);
  212. fwrite(&height, sizeof(int), 1, export);
  213.  
  214. for(int row = 0; row < height; ++row) {
  215. fwrite(map[row], sizeof(Terrain), width, export);
  216. }
  217.  
  218. fclose(export);
  219.  
  220. return;
  221. }
  222.  
  223. Terrain** ImportMap(const char *file_name, int *height, int *width) {
  224. if (!file_name) {
  225. fprintf(stderr, "Invalid pointer!\n");
  226. return NULL;
  227. }
  228.  
  229. FILE *import = fopen(file_name, "rb");
  230. if (!import) {
  231. fprintf(stderr, "File failed to open!\n");
  232. return NULL;
  233. }
  234.  
  235. fread(width, sizeof(int), 1, import);
  236. fread(height, sizeof(int), 1, import);
  237.  
  238. Terrain **result = malloc(*height * sizeof(Terrain *));
  239. for (int i = 0; i < *height; ++i) {
  240. result[i] = malloc(*width * sizeof(Terrain));
  241. }
  242.  
  243. for (int row = 0; row < *height; ++row) {
  244. fread(result[row], sizeof(Terrain), *width, import);
  245. }
  246.  
  247. fclose(import);
  248.  
  249. return result;
  250. }
  251.  
  252.  
  253. float calculateIncline(float h1, float h2) {
  254. float a, b, c, A, B, C, R, s, pi, area;
  255. pi = acos(-1);
  256.  
  257. a = 1.0;
  258. b = abs(h2 - h1);
  259. c = sqrt(a*a + b*b);
  260.  
  261. s = (a + b + c) / 2;
  262. area = sqrt(s * (s - a) * (s - b) * (s - c));
  263. R = (a * b * c) / (4 * area);
  264.  
  265. A = (180 / pi) * asin(a / (2 * R));
  266. B = (180 / pi) * asin(b / (2 * R));
  267. C = (180 / pi) * asin(c / (2 * R));
  268.  
  269. return B;
  270. }
  271.  
  272. int checkIncline(float h1, float h2, float h3, float h4, float h5) {
  273. if (h1 < h2 && h2 < h3 && h3 > h4 && h4 > h5) {
  274. if(calculateIncline(h1, h2) < 45.0 && calculateIncline(h2, h3) < 45.0 &&
  275. calculateIncline(h3, h4) < 80.0 && calculateIncline(h4, h5) < 80.0) {
  276. return 1;
  277. }
  278. }
  279.  
  280. return 0;
  281. }
  282.  
  283. void PrintJumpingRoutes(Terrain **map, const int height, const int width) {
  284. for(int col = 4; col < width; ++col) {
  285. for (int row = 0; row < height; ++row) {
  286. if (map[row][col].type == WASTELAND && map[row][col-1].type == WASTELAND &&
  287. map[row][col-2].type == WASTELAND && map[row][col-3].type == WASTELAND &&
  288. map[row][col-4].type == WASTELAND) {
  289. if (checkIncline(map[row][col].height, map[row][col-1].height,
  290. map[row][col-2].height, map[row][col-3].height, map[row][col-4].height)) {
  291. printf("[%d|%d]<-[%d|%d]<-[%d|%d]<-[%d|%d]<-[%d|%d]\n",
  292. row, col - 4, row, col - 3, row, col - 2, row, col - 1, row, col);
  293. }
  294. }
  295. }
  296. }
  297. }
  298.  
  299. int main() {
  300. srand(time(NULL));
  301. Terrain** map = NULL;
  302. int choice = 0;
  303. int mapWidth, mapHeight;
  304.  
  305. do {
  306. printf("1. Generate Map\n");
  307. printf("2. Print Map By Terrain Height\n");
  308. printf("3. Print Map By Terrain Type\n");
  309. printf("4. Build Road\n");
  310. printf("5. Print Bikers Jumping Routes\n");
  311. printf("6. Export to File\n");
  312. printf("7. Import From file\n");
  313. printf("0. EXIT\n");
  314.  
  315. printf("Your Choice: ");
  316. scanf("%d", &choice);
  317. switch(choice) {
  318. case 1:
  319. if(map != NULL) {
  320. DeleteMap(map, mapWidth);
  321. mapWidth = -1;
  322. mapHeight = -1;
  323. }
  324. printf("Please input: MAP_WIDTH MAP_HEIGHT\n");
  325. scanf("%d %d", &mapWidth, &mapHeight);
  326.  
  327. map = generateEmptyMap(mapHeight, mapWidth);
  328. fillMapHeight(map, mapHeight, mapWidth);
  329. fillMapType(map, mapHeight, mapWidth);
  330. break;
  331. case 2:
  332. PrintMapByTerrainHeight(map, mapHeight, mapWidth);
  333. break;
  334. case 3:
  335. PrintMapByTerrainType(map, mapHeight, mapWidth);
  336. break;
  337. case 4:
  338. BuildRoad(map, mapHeight, mapWidth);
  339. break;
  340. case 5:
  341. ExportMap(map, mapHeight, mapWidth, "map.bin");
  342. break;
  343. case 6:
  344. if (map) {
  345. DeleteMap(map, mapWidth);
  346. mapWidth = -1;
  347. mapHeight = -1;
  348. }
  349. map = ImportMap("map.bin", &mapHeight, &mapWidth);
  350. break;
  351. case 0:
  352. break;
  353. default: break;
  354. }
  355. } while (choice != 0);
  356.  
  357. return 0;
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement