You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
4.5 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  1. #include "labyrinth.h"
  2. #include "global.h"
  3. #include "stdio.h"
  4. #include "stdlib.h"
  5. void show_solution(Field_State** field, unsigned short len_x, unsigned short len_y){
  6. for (int c_x = 0; c_x < len_x; c_x++){
  7. for (int c_y = 0; c_y < len_y; c_y++){
  8. printf("%hi", field[c_x][c_y]);
  9. }
  10. printf("\n");
  11. }
  12. }
  13. unsigned short get_natural_number(char text[]){
  14. unsigned short result;
  15. printf("%s", text);
  16. scanf("%hu", &result);
  17. return result;
  18. }
  19. void ask_lab_dimensions(unsigned short *len_x, unsigned short *len_y){
  20. *len_x = get_natural_number("Bitte gib die x-Laenge des Labyrinthes an:\n");
  21. *len_y = get_natural_number("Bitte gib die y-Laenge des Labyrinthes an:\n");
  22. }
  23. short get_wall_input(unsigned short *x, unsigned short *y, unsigned short len_x, unsigned short len_y){
  24. char answer;
  25. printf("Moechten Sie die Koordinaten einer weiteren Labyrinthwand angeben? (y/n)\n");
  26. scanf(" %c", &answer);
  27. if (answer == 'n'){
  28. return 1;
  29. }
  30. printf("Bitte geben Sie die Koordinaten von Labyrinthwaenden im Format x, y ein.\n");
  31. scanf("%hu, %hu", x, y);
  32. if (*x >= len_x || *y >= len_y){
  33. printf("Die eingegebenen Koordinaten sind zu gross.\n");
  34. return -1;
  35. }
  36. return 0;
  37. }
  38. void turn_direction_right(Direction *direction){
  39. switch (*direction) {
  40. case N:
  41. *direction = E;
  42. break;
  43. case E:
  44. *direction = S;
  45. break;
  46. case S:
  47. *direction = W;
  48. break;
  49. case W:
  50. *direction = N;
  51. break;
  52. }
  53. }
  54. void lab_move(unsigned short *x, unsigned short *y, Direction direction){
  55. if (direction == N){
  56. *x = *x - 1;
  57. return;
  58. }
  59. if (direction == E){
  60. *y = *y + 1;
  61. return;
  62. }
  63. if (direction == S){
  64. *x = *x + 1;
  65. return;
  66. }
  67. if (direction == W){
  68. *y = *y - 1;
  69. return;
  70. }
  71. }
  72. void set_wall(Field_State** field, unsigned short x, unsigned short y) {
  73. field[x][y] = WALL;
  74. }
  75. void init_field(Field_State** field, unsigned short len_x, unsigned short len_y){
  76. for (int c_x = 0; c_x < len_x; c_x++){
  77. for (int c_y = 0; c_y < len_y; c_y++){
  78. field[c_x][c_y] = WAY;
  79. }
  80. }
  81. }
  82. short lab_can_move(Field_State** field, unsigned short x, unsigned short y, Direction direction, unsigned short len_x, unsigned short len_y){
  83. lab_move(&x, &y, direction);
  84. if (x >= len_x){
  85. return 1;
  86. }
  87. if (y >= len_y){
  88. return 1;
  89. }
  90. if (field[x][y] == WALL){
  91. return 1;
  92. }
  93. if (field[x][y] == SOLUTION){
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. void ask_lab_walls(Field_State** field, unsigned short len_x, unsigned short len_y){
  99. unsigned short x, y;
  100. short wall_input_continue;
  101. init_field(field, len_x, len_y);
  102. do {
  103. wall_input_continue = get_wall_input(&x, &y, len_x, len_y);
  104. if (wall_input_continue == 0){
  105. set_wall(field, x, y);
  106. show_solution(field, len_x, len_y);
  107. }
  108. } while (wall_input_continue != 1);
  109. }
  110. short calculate_lab_way(Field_State** field, unsigned short len_x, unsigned short len_y, unsigned short x, unsigned short y){
  111. Direction direction = N;
  112. unsigned short current_x;
  113. unsigned short current_y;
  114. if (x == len_x-1 && y == len_y-1){
  115. field[x][y] = SOLUTION;
  116. return 0;
  117. }
  118. do {
  119. current_x = x;
  120. current_y = y;
  121. if (lab_can_move(field, current_x, current_y, direction, len_x, len_y) == 0){
  122. lab_move(&current_x, &current_y, direction);
  123. printf("%d - %d\n", current_x, current_y);
  124. field[current_x][current_y] = SOLUTION;
  125. if (calculate_lab_way(field, len_x, len_y, current_x, current_y) == 0){
  126. return 0;
  127. }
  128. field[current_x][current_y] = WAY;
  129. }
  130. turn_direction_right(&direction);
  131. }
  132. while (direction != N);
  133. return 1;
  134. }
  135. void start_labyrinth_game(){
  136. unsigned short len_x, len_y;
  137. Field_State **field;
  138. ask_lab_dimensions(&len_x, &len_y);
  139. field = malloc(len_x * sizeof *field);
  140. for (int c_index = 0; c_index < len_x; c_index++){
  141. field[c_index] = malloc(len_y * sizeof field[c_index]);
  142. }
  143. ask_lab_walls(field, len_x, len_y);
  144. if (calculate_lab_way(field, len_x, len_y, 0, 0) == 1){
  145. printf("Keine Loesung moeglich!\n");
  146. }
  147. show_solution(field, len_x, len_y);
  148. for (int c_index = 0; c_index < len_x; c_index++)
  149. {
  150. free(field[c_index]);
  151. }
  152. free(field);
  153. }