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.

127 lines
3.2 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <stdbool.h>
  6. #include "labyrinth.h"
  7. #include "userinput.h"
  8. int printlabyrinth(lab laby, int hoehe, int breite){
  9. for(int i = 0; i < hoehe; i++){
  10. for(int j = 0; j < breite; j++){
  11. if(laby[i][j] == '1'){
  12. printf("");
  13. }
  14. else if(laby[i][j] == WEG){
  15. printf(" ");
  16. }
  17. else{
  18. printf("%c ", laby[i][j]);
  19. }
  20. }
  21. printf("\n");
  22. }
  23. printf("\n");
  24. printf("How many steps do you need to reach your goal?\n");
  25. printf("\n");
  26. return 0;
  27. }
  28. void wegsuchen(lab laby, bool* done, int y, int x, int ziely, int zielx){
  29. laby[y][x] = MARKIERT;
  30. if(x == zielx && y == ziely){
  31. *done = true;
  32. }
  33. else{
  34. if (!*done && y + 1 <= ziely && laby[y+1][x] == WEG){
  35. wegsuchen(laby, done, y + 1, x, ziely, zielx);
  36. }
  37. if (!*done && x + 1 <= zielx && laby[y][x+1] == WEG){
  38. wegsuchen(laby, done, y, x + 1, ziely, zielx);
  39. }
  40. if (!*done && y - 1 >= 0 && laby[y-1][x] == WEG){ // oben
  41. wegsuchen(laby, done, y - 1, x, ziely, zielx);
  42. }
  43. if (!*done && x - 1 >= 0 && laby[y][x-1] == WEG){ // links
  44. wegsuchen(laby, done, y, x - 1, ziely, zielx);
  45. }
  46. if (!*done){
  47. laby[y][x] = WEG;
  48. }
  49. }
  50. }
  51. void labyrinthschritte(lab laby, int hoehe, int breite, int schritte, int versuche){
  52. int antwort = 0;
  53. antwort = userInput();
  54. if(antwort == schritte){
  55. printf("Correct you need %d steps.\n", schritte);
  56. for(int i = 0; i < hoehe; i++){
  57. for(int j = 0; j < breite; j++){
  58. printf("%c ", laby[i][j]);
  59. }
  60. printf("\n");
  61. }
  62. printf("\n");
  63. }
  64. else{
  65. if(versuche != 3){
  66. printf("Your answer is wrong. Try again.\n");
  67. versuche = versuche + 1;
  68. labyrinthschritte(laby, hoehe, breite, schritte, versuche); //if schleife für 3 versuche
  69. }
  70. else{
  71. printf("You lost.\n");
  72. }
  73. }
  74. }
  75. void labyrinthauswahl(int auswahl){
  76. printf("Bitte wählen Sie ein Labyrinth aus\n");
  77. switch (auswahl){
  78. case 1:
  79. lab laby = {
  80. {'0', '1', '0', '0', '0', '0'},
  81. {'0', '1', '0', '1', '1', '0'},
  82. {'0', '0', '0', '0', '1', '0'},
  83. {'0', '1', '1', '0', '1', '0'},
  84. {'0', '1', '0', '0', '1', '0'},
  85. };
  86. int hoehe = 5;
  87. int breite = 6;
  88. printlabyrinth(laby, hoehe, breite);
  89. break;
  90. case 2:
  91. lab laby2 = {
  92. {'0', '0', '0', '0', '1', '0', '1', '1', '0'},
  93. {'1', '0', '1', '0', '0', '1', '1', '1', '0'},
  94. {'1', '0', '1', '1', '0', '0', '1', '1', '0'},
  95. {'0', '0', '0', '1', '1', '0', '1', '0', '1'},
  96. {'0', '1', '0', '1', '0', '0', '1', '0', '1'},
  97. {'0', '1', '0', '1', '0', '1', '0', '0', '0'},
  98. {'0', '1', '0', '1', '0', '0', '0', '1', '0'},
  99. };
  100. hoehe = 7;
  101. breite = 9;
  102. printlabyrinth(laby2, hoehe, breite);
  103. break;
  104. default:
  105. break;
  106. }
  107. }