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'},
- {'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
- {'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
- {'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
- {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'},
- {'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'},
- {'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'},
- {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} };
- typedef struct location{
- int i;
- int j;
- int entr;
- } t_location ;
- t_location entrance_location();
- void maze_solver(t_location);
- void find_entrance();
- void find_entrance_updown();
- void print_maze();
- int finding_point(int, int, 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()
- {
- print_maze();
- find_entrance();
- t_location current = entrance_location();
- printf("%d, %d, %d\n", current.entr, current.i, current.j);
- maze_solver(current);
- };
- void print_maze()
- {
- int i, j;
- for(i = 0; i < maxRow; ++i){
- for(j = 0; j < maxCol; ++j){
- printf("%c\t", labyrinth[i][j]);
- }
- printf("\n\n");
- }
- }
- void find_entrance ()
- {
- int i, j;
- int flag = 0;
- for(j = 0; j < maxCol; j+=maxCol - 1){
- for(i = 0; i < maxRow; i++){
- finding_point(flag, i, j);
- }
- }
- 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)
- int i, j;
- for(i = 0; i < maxRow; i+=11){
- for(j = 0; j < maxCol; j++){
- finding_point(flag, i, j);
- }
- }
- }
- int finding_point (int flag, int i, int j)
- {
- 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);
- }
- int x;
- while(x = 0, x < 20){ //la vera condizione è labyrinth[current.i][current.j] != 'c', questa era solo per vedere se funzionava
- evaluate_situation(current);
- x++;
- }
- }
- t_location entrance_location ()
- {
- t_location current;
- int i, j;
- 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", current.i ,current.j);
- if(labyrinth[0][j] == 'X'){ //sopra
- current.entr = 1;
- }
- else if (labyrinth[11][j] == 'X'){ //sotto
- current.entr = 2;
- }
- else if( labyrinth[i][0] == 'X'){ //sinistra
- current.entr = 3;
- }
- else if(labyrinth[i][11] == 'X'){ //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
- {
- int i = current.i;
- int 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
- {
- int i = current.i;
- int 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
- {
- int i = current.i;
- int 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
- {
- int i = current.i;
- int 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)
- {
- int i = current.i;
- int 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