Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stddef.h>
- #define maxRow 12
- #define maxCol 12
- //DEVO FARLO CON GLI IF NESTATI DIO CANEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE, CIAO DARIO DI DOMANI
- char labyrinth[maxRow][maxCol] = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
- {'#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'},
- {'.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'},
- {'#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'},
- {'#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', 'c'},
- {'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
- {'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
- {'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
- {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'},
- {'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'},
- {'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'},
- {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} };
- int i;
- int j;
- typedef struct location{
- int i;
- int j;
- int entr;
- } t_location ;
- t_location entrance_location(t_location);
- void maze_solver(t_location);
- void find_entrance();
- void find_entrance_updown();
- void print_maze();
- int finding_point(int);
- t_location move_up (t_location);
- t_location move_down(t_location);
- t_location move_left(t_location);
- t_location move_right(t_location);
- t_location evaluate_situation(t_location);
- int main()
- {
- t_location current = {0, 0, 0};
- print_maze();
- find_entrance();
- entrance_location(current);
- printf("%d, %d, %d", current.i, current.j, current.entr);
- };
- void print_maze()
- {
- for(i = 0; i < maxRow; ++i){
- for(j = 0; j < maxCol; ++j){
- printf("%c\t", labyrinth[i][j]);
- }
- printf("\n");
- }
- }
- void find_entrance ()
- {
- int flag = 0;
- for(j = 0; j < maxCol; j+=maxCol - 1){
- for(i = 0; i < maxRow; i++){
- finding_point(flag);
- }
- }
- if(flag == 0) {
- find_entrance_updown();
- }
- }
- void find_entrance_updown (int flag) // still to implement case in which there's no entrance
- { // just need to make findentranceupdown output flag0 and create an if(no entrance)
- for(i = 0; i < maxRow; i+=11){
- for(j = 0; j < maxCol; j++){
- finding_point(flag);
- }
- }
- }
- int finding_point (int flag)
- {
- if(labyrinth[i][j] == '.') {
- printf("Found the entrance\n");
- labyrinth[i][j] = 'X';
- flag++;
- print_maze();
- }
- return flag;
- }
- void maze_solver(t_location current)
- {
- if(current.entr == 1){
- move_down(current);
- }
- else if(current.entr == 2){
- move_up(current);
- }
- else if(current.entr == 3){
- move_right(current);
- }
- else if(current.entr == 4){
- move_left(current);
- }
- while(labyrinth[current.i][current.j] != 'c'){
- evaluate_situation(current);
- }
- }
- t_location entrance_location (t_location current)
- {
- for( i = 0; i < maxRow; i++) {
- for( j = 0; j < maxCol; j++){
- if(labyrinth[i][j] == 'X'){
- current.i = i;
- current.j = j;
- //questo printf mi assicura che current è up to date: printf("%d, %d, %d", current.i, current.j, current.entr);
- printf("Entrance coord:%d, %d\n", i ,j);
- if(labyrinth[0][j]){ //sopra
- current.entr = 1;
- }
- else if (labyrinth[11][j]){ //sotto
- current.entr = 2;
- }
- else if( labyrinth[i][0]){ //sinistra
- current.entr = 3;
- }
- else if(labyrinth[i][11]){ //destra
- current.entr = 4;
- }
- }
- }
- }
- return current;
- }
- t_location move_up(t_location current) //devo ricordarmi di settare gli indici all'entrata prima di lanciare move_up
- {
- i = current.i;
- j = current.j;
- if(labyrinth[i+1][j] == '.'){
- labyrinth[i+1][j] = 'X';
- print_maze();
- current.i = i++;
- }
- return current;
- }
- t_location move_down(t_location current) //devo ricordarmi di settare gli indici all'entrata prima di lanciare move_up
- {
- i = current.i;
- j = current.j;
- if(labyrinth[i-1][j] == '.'){
- labyrinth[i-1][j] = 'X';
- print_maze();
- current.i = i--;
- }
- return current;
- }
- t_location move_right(t_location current) //devo ricordarmi di settare gli indici all'entrata prima di lanciare move_up
- {
- i = current.i;
- j = current.j;
- if(labyrinth[i][j+1] == '.'){
- labyrinth[i][j+1] = 'X';
- print_maze();
- current.j = j++;
- } //adesso deve controllare che sia possibile il movimento a destra
- return current;
- }
- t_location move_left(t_location current) //devo ricordarmi di settare gli indici all'entrata prima di lanciare move_up
- {
- i = current.i;
- j = current.j;
- if(labyrinth[i][j-1] == '.'){
- labyrinth[i][j-1] = 'X';
- print_maze();
- current.j = j--;
- } //adesso deve controllare che sia possibile il movimento a destra
- return current;
- }
- t_location evaluate_situation(t_location current)
- {
- i = current.i;
- j = current.j;
- if((labyrinth[i+1][j] == 'X') && (labyrinth[i-1][j] == '.') && (labyrinth[i-1][j+1] == '.')){ //ricordati il caso in cui trova C
- move_up(current);
- move_right(current);
- }
- else if((labyrinth[i+1][j] == 'X') && (labyrinth[i-1][j] == '.') && ( labyrinth[i-1][j-1] == '.') && (labyrinth[i-1][j+1] != '.')){
- move_up(current);
- move_left(current);
- }
- else if((labyrinth[i+1][j] == 'X') && (labyrinth[i-1][j-1] == '#') && ( labyrinth[i-1][j+1] == '#')){
- move_up(current);
- }
- //down movements
- else if((labyrinth[i-1][j] == 'X') && (labyrinth[i+1][j] == '.') && (labyrinth[i+1][j-1] == '.')){
- move_down(current);
- move_left(current);
- }
- else if((labyrinth[i-1][j] == 'X') && (labyrinth[i+1][j] == '.') && ( labyrinth[i+1][j+1] == '.') && (labyrinth[i+1][j-1] != '.')){
- move_down(current);
- move_right(current);
- }
- else if((labyrinth[i-1][j] == 'X') && (labyrinth[i+1][j-1] == '#') && ( labyrinth[i+1][j+1] == '#')){
- move_down(current);
- }
- //right movements
- else if((labyrinth[i][j-1] == 'X') && (labyrinth[i][j+1] == '.') && (labyrinth[i+1][j+1] == '.')){
- move_right(current);
- move_down(current);
- }
- else if((labyrinth[i][j-1] == 'X') && (labyrinth[i][j+1] == '.') && ( labyrinth[i-1][j+1] == '.') && (labyrinth[i+1][j+1] == '#')){
- move_right(current);
- move_up(current);
- }
- else if((labyrinth[i][j-1] == 'X') && (labyrinth[i-1][j+1] == '#') && ( labyrinth[i+1][j+1] == '#')){
- move_right(current);
- }
- //left movements
- else if((labyrinth[i][j+1] == 'X') && (labyrinth[i][j-1] == '.') && (labyrinth[i-1][j-1] == '.')){
- move_left(current);
- move_up(current);
- }
- else if((labyrinth[i][j+1] == 'X') && (labyrinth[i][j-1] == '.') && (labyrinth[i+1][j-1] == '.') && (labyrinth[i-1][j-1] == '#')){
- move_left(current);
- move_down(current);
- }
- else if((labyrinth[i][j+1] == 'X') && (labyrinth[i-1][j-1] == '#') && (labyrinth[i+1][j-1] == '#')){
- move_left(current);
- }
- return current;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement