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.

161 lines
3.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year 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 c_x;
  113. unsigned short c_y;
  114. if (x == len_x-1 && y == len_y-1){
  115. field[x][y] = SOLUTION;
  116. return 0;
  117. }
  118. do {
  119. c_x = x;
  120. c_y = y;
  121. if (lab_can_move(field, c_x, c_y, direction, len_x, len_y) == 0){
  122. lab_move(&c_x, &c_y, direction);
  123. printf("%d - %d\n", c_x, c_y);
  124. field[c_x][c_y] = SOLUTION;
  125. if (calculate_lab_way(field, len_x, len_y, c_x, c_y) == 0){
  126. return 0;
  127. }
  128. field[c_x][c_y] = WAY;
  129. }
  130. turn_direction_right(&direction);
  131. }
  132. while (direction != N);
  133. return 1;
  134. }