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
161 lines
3.8 KiB
#include "labyrinth.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "stdio.h"
|
|
#include "stdlib.h"
|
|
|
|
void show_solution(Field_State** field, unsigned short len_x, unsigned short len_y){
|
|
for (int c_x = 0; c_x < len_x; c_x++){
|
|
for (int c_y = 0; c_y < len_y; c_y++){
|
|
printf("%hi", field[c_x][c_y]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
unsigned short get_natural_number(char text[]){
|
|
unsigned short result;
|
|
printf("%s", text);
|
|
scanf("%hu", &result);
|
|
return result;
|
|
}
|
|
|
|
void ask_lab_dimensions(unsigned short *len_x, unsigned short *len_y){
|
|
*len_x = get_natural_number("Bitte gib die x-Laenge des Labyrinthes an:\n");
|
|
*len_y = get_natural_number("Bitte gib die y-Laenge des Labyrinthes an:\n");
|
|
}
|
|
|
|
short get_wall_input(unsigned short *x, unsigned short *y, unsigned short len_x, unsigned short len_y){
|
|
char answer;
|
|
printf("Moechten Sie die Koordinaten einer weiteren Labyrinthwand angeben? (y/n)\n");
|
|
scanf(" %c", &answer);
|
|
|
|
if (answer == 'n'){
|
|
return 1;
|
|
}
|
|
|
|
printf("Bitte geben Sie die Koordinaten von Labyrinthwaenden im Format x, y ein.\n");
|
|
scanf("%hu, %hu", x, y);
|
|
|
|
if (*x >= len_x || *y >= len_y){
|
|
printf("Die eingegebenen Koordinaten sind zu gross.\n");
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void turn_direction_right(Direction *direction){
|
|
switch (*direction) {
|
|
case N:
|
|
*direction = E;
|
|
break;
|
|
case E:
|
|
*direction = S;
|
|
break;
|
|
case S:
|
|
*direction = W;
|
|
break;
|
|
case W:
|
|
*direction = N;
|
|
break;
|
|
}
|
|
}
|
|
|
|
void lab_move(unsigned short *x, unsigned short *y, Direction direction){
|
|
|
|
if (direction == N){
|
|
*x = *x - 1;
|
|
return;
|
|
}
|
|
if (direction == E){
|
|
*y = *y + 1;
|
|
return;
|
|
}
|
|
if (direction == S){
|
|
*x = *x + 1;
|
|
return;
|
|
}
|
|
if (direction == W){
|
|
*y = *y - 1;
|
|
return;
|
|
}
|
|
|
|
}
|
|
|
|
void set_wall(Field_State** field, unsigned short x, unsigned short y) {
|
|
field[x][y] = WALL;
|
|
}
|
|
|
|
void init_field(Field_State** field, unsigned short len_x, unsigned short len_y){
|
|
for (int c_x = 0; c_x < len_x; c_x++){
|
|
for (int c_y = 0; c_y < len_y; c_y++){
|
|
field[c_x][c_y] = WAY;
|
|
}
|
|
}
|
|
}
|
|
|
|
short lab_can_move(Field_State** field, unsigned short x, unsigned short y, Direction direction, unsigned short len_x, unsigned short len_y){
|
|
|
|
lab_move(&x, &y, direction);
|
|
if (x >= len_x){
|
|
return 1;
|
|
}
|
|
if (y >= len_y){
|
|
return 1;
|
|
}
|
|
if (field[x][y] == WALL){
|
|
return 1;
|
|
}
|
|
if (field[x][y] == SOLUTION){
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void ask_lab_walls(Field_State** field, unsigned short len_x, unsigned short len_y){
|
|
unsigned short x, y;
|
|
short wall_input_continue;
|
|
|
|
init_field(field, len_x, len_y);
|
|
|
|
do {
|
|
wall_input_continue = get_wall_input(&x, &y, len_x, len_y);
|
|
if (wall_input_continue == 0){
|
|
set_wall(field, x, y);
|
|
show_solution(field, len_x, len_y);
|
|
}
|
|
} while (wall_input_continue != 1);
|
|
}
|
|
|
|
short calculate_lab_way(Field_State** field, unsigned short len_x, unsigned short len_y, unsigned short x, unsigned short y){
|
|
Direction direction = N;
|
|
|
|
unsigned short c_x;
|
|
unsigned short c_y;
|
|
|
|
if (x == len_x-1 && y == len_y-1){
|
|
field[x][y] = SOLUTION;
|
|
return 0;
|
|
}
|
|
|
|
do {
|
|
c_x = x;
|
|
c_y = y;
|
|
if (lab_can_move(field, c_x, c_y, direction, len_x, len_y) == 0){
|
|
lab_move(&c_x, &c_y, direction);
|
|
printf("%d - %d\n", c_x, c_y);
|
|
field[c_x][c_y] = SOLUTION;
|
|
if (calculate_lab_way(field, len_x, len_y, c_x, c_y) == 0){
|
|
return 0;
|
|
}
|
|
field[c_x][c_y] = WAY;
|
|
}
|
|
turn_direction_right(&direction);
|
|
}
|
|
while (direction != N);
|
|
return 1;
|
|
}
|
|
|