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
188 lines
4.5 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 current_x;
|
|
unsigned short current_y;
|
|
|
|
if (x == len_x-1 && y == len_y-1){
|
|
field[x][y] = SOLUTION;
|
|
return 0;
|
|
}
|
|
|
|
do {
|
|
current_x = x;
|
|
current_y = y;
|
|
if (lab_can_move(field, current_x, current_y, direction, len_x, len_y) == 0){
|
|
lab_move(¤t_x, ¤t_y, direction);
|
|
printf("%d - %d\n", current_x, current_y);
|
|
field[current_x][current_y] = SOLUTION;
|
|
if (calculate_lab_way(field, len_x, len_y, current_x, current_y) == 0){
|
|
return 0;
|
|
}
|
|
field[current_x][current_y] = WAY;
|
|
}
|
|
turn_direction_right(&direction);
|
|
}
|
|
while (direction != N);
|
|
return 1;
|
|
}
|
|
|
|
void start_labyrinth_game(){
|
|
unsigned short len_x, len_y;
|
|
Field_State **field;
|
|
|
|
ask_lab_dimensions(&len_x, &len_y);
|
|
|
|
field = malloc(len_x * sizeof *field);
|
|
for (int c_index = 0; c_index < len_x; c_index++){
|
|
field[c_index] = malloc(len_y * sizeof field[c_index]);
|
|
}
|
|
|
|
ask_lab_walls(field, len_x, len_y);
|
|
|
|
if (calculate_lab_way(field, len_x, len_y, 0, 0) == 1){
|
|
printf("Keine Loesung moeglich!\n");
|
|
}
|
|
|
|
show_solution(field, len_x, len_y);
|
|
|
|
for (int c_index = 0; c_index < len_x; c_index++)
|
|
{
|
|
free(field[c_index]);
|
|
}
|
|
free(field);
|
|
|
|
}
|
|
|