// Generate a maze (recursively) // Generates a maze using the Recursive Backtracker method: // https://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_backtracker void generate_maze(int x, int y) { maze[x][y] = false; // clear the spot in current position while(can_go(x, y - 2) || can_go(x, y + 2) || can_go(x - 2, y) || can_go(x + 2, y)) { // While there exists a new position you can go to switch(rand()%4) { // Randomly choose a direction to go to case 0: // Try going up if(can_go(x, y - 2)) { // If you can go up maze[x][y-1] = false; // remove wall above current position generate_maze(x, y-2); // move up two spaces } break; case 1: // Try going down if(can_go(x, y + 2)) { // If you can go down maze[x][y+1] = false; // remove wall below current position generate_maze(x, y + 2); // move down two spaces } break; case 2: // Try going left if(can_go(x - 2, y)) { // If you can go left maze[x-1][y] = false; // remove wall left of current position generate_maze(x-2, y); // move left two spaces } break; case 3: // Try going right if(can_go(x + 2, y)) { // If you can go right maze[x+1][y] = false; // remove wall right of current position generate_maze(x+2, y); // move right two spaces } break; } } }
void down_single_click_handler(ClickRecognizerRef recognizer, void *context) { // Generate new maze but don't solve it clear_solution(); clear_maze(); generate_maze(start_x, start_y); layer_mark_dirty(maze_layer); layer_mark_dirty(solution_layer); }
//buat maze void generate_maze(char **maze, int input, int x, int y){ int direction = rand()%4;//random arah int trial = 1; //banyak uji coba pengecekan jalan maze[x][y] = PATH;//jadikan baris x kolom y sebagai jalan system("cls"); view_maze(maze, input); getch(); while(trial<=4){ //coba sampai 4 kali if(trial<=4 && direction==UP){ //cek posisi atas trial++; if(x-2>=1 && maze[x-2][y] == WALL){ maze[x-1][y] = PATH; generate_maze(maze, input, x-2, y); } direction++; } if(trial<=4 && direction==DOWN){ //cek posisi bawah trial++; if(x+2<input && maze[x+2][y] == WALL){ maze[x+1][y] = PATH; generate_maze(maze, input, x+2, y); } direction++; } if(trial<=4 && direction==LEFT){//cek posisi kiri trial++; if(y-2>=1 && maze[x][y-2] == WALL){ maze[x][y-1] = PATH; generate_maze(maze, input, x, y-2); } direction++; } if(trial<=4 && direction==RIGHT){//cek posisi kanan trial++; if(y+2<input && maze[x][y+2] == WALL){ maze[x][y+1] = PATH; generate_maze(maze, input, x, y+2); } direction++; } direction = UP; } }
void select_single_click_handler(ClickRecognizerRef recognizer, void *context) { // Generate and Solve a new maze clear_solution(); clear_maze(); generate_maze(start_x, start_y); solve_maze(start_x, start_y); layer_mark_dirty(maze_layer); layer_mark_dirty(solution_layer); }
//membuat maze void generate_maze(char **maze, int size, int x, int y){ int trial = 1, direction = rand()%4; maze[x][y] = PATH; while(trial<=4){ if(trial<=4 && direction==0/*UP*/){ trial++; if(x-2>=1 && maze[x-2][y] != PATH){ maze[x-1][y] = PATH; generate_maze(maze, size, x-2, y); } direction++; } if(trial<=4 && direction==1/*DOWN*/){ trial++; if(x+2<size && maze[x+2][y] != PATH){ maze[x+1][y] = PATH; generate_maze(maze, size, x+2, y); } direction++; } if(trial<=4 && direction==2/*LEFT*/){ trial++; if(y-2>=1 && maze[x][y-2] != PATH){ maze[x][y-1] = PATH; generate_maze(maze, size, x, y-2); } direction++; } if(trial<=4 && direction==3/*RIGHT*/){ trial++; if(y+2<size && maze[x][y+2] != PATH){ maze[x][y+1] = PATH; generate_maze(maze, size, x, y+2); } direction++; } direction = 0/*UP*/; } }
int main(){ char **maze; int n, input, i, j; srand((unsigned) time(NULL)); //Masukkan ukuran maze do{ system("cls"); printf("MAZE CREATOR\n\nInput Size (0 ~ 30): "); fflush(stdin); scanf("%d", &n); }while(n<0 || n>30); if(n==0){ printf("#"); getch(); return 0; } input = 2*n+1; //buat maze kosong input x input maze = (char**)malloc(sizeof(char*) * input); //Jadikan semuanya cell tembok for(i=0; i<input; i++){ maze[i] = (char*)malloc(sizeof(char) * input); for(j=0; j<input; j++) maze[i][j] = WALL; } //random start i = 2*(rand()%n)+1; j = 2*(rand()%n)+1; //buat maze generate_maze(maze, input, i, j); view_maze(maze, input); for(i=input-1; i>=0; i--){ free(maze[i]); } free(maze); getch(); return 0; }
int main () { struct mazegame game; struct game_display *display; /* char *map_file_name = "mazes/mazeFile1.txt"; */ struct maze *maze; int game_running = 1; enum command command; srand ( time ( NULL ) ); /* maze = read_map ( map_file_name ); */ maze = generate_maze ( 20, 70, 50, 30 ); printf ( "escaped the read_map\n" ); game = new_mazegame ( maze ); printf ( "initializing display" ); display = init_display ( game.maze->size, MAINGAME ); while ( game_running ) { update_display ( display, &game ); get_command ( &command ); if ( command == QUIT || DR_equal_pos ( game.player.p, game.maze->goal_position ) || game.player.battery < -5 ) { game_running = 0; } else if ( RUN == command ) { update_game( &game, command ); update_display( display, &game ); update_game( &game, command ); update_display( display, &game ); update_game( &game, command ); } else { update_game ( &game, command ); } } destroy_display ( display ); destroy_mazegame ( &game ); return 0; }
int main(int argc, char const *argv[]) { maze_t *maze; if (argc != 3) { printf("\033[31;01mUsage: ./a.out nbCol nbLine\033[00m\n"); return (0); } maze = newMaze(strtol(argv[1], NULL, 10), strtol(argv[2], NULL, 10)); generate_maze(maze); // printCharArrayMaze(maze->array, maze->m, maze->n); printSDLCharArrayMaze(maze->array, maze->m, maze->n); destroyMaze(maze); return (0); }
int main() { Set_Console_FullScreen(); Set_Console_Buffer_Sizes(maze_width,maze_height+1); while(1) { generate_maze(); //solve_maze() getch(); } return 0; }
int main(){ char **maze; int **posX, **posY; int n, size, i, j; system("color f0"); srand((unsigned)time(0)); printf("Input Dungeon Level [lvl <= 0 to EXIT]: "); fflush(stdin); scanf("%d", &n); if(n<=0) return 0; //level+2 n+=2; size = 2*n+1; maze = (char**)malloc(sizeof(char*) * size); for(i=0; i<size; i++){ maze[i] = (char*)malloc(sizeof(char) * (size + 1)); for(j=0; j<size; j++) { if(i==0 || j==0 || i==size-1 || j==size-1 || rand()%100>25) maze[i][j] = WALL; else maze[i][j] = EX_WALL; } maze[i][size]='\0'; } //random posisi awal random i = 2*(rand()%n)+1; j = 2*(rand()%n)+1; generate_maze(maze, size, i, j); posX = (int**)malloc(sizeof(int*) * 2); posY = (int**)malloc(sizeof(int*) * 2); //masing-masing player diberi army sebanyak level yang telah //ditambah 2 for(i=0; i<2; i++){ posX[i] = (int*)malloc(sizeof(int)*n); posY[i] = (int*)malloc(sizeof(int)*n); for(j=0; j<n; j++){ do{ posX[i][j] = 2*(rand()%n)+1; posY[i][j] = 2*(rand()%n)+1; }while(maze[posX[i][j]][posY[i][j]] != PATH); maze[posX[i][j]][posY[i][j]] = i+1; } } //mulai permainan play_game(maze, size, posX, posY, n); getch(); for(i=1; i>=0; i--){ free(posY[i]); free(posX[i]); } free(posY); free(posX); for(i=size-1; i>=0; i--){ free(maze[i]); } free(maze); return 0; }
int main(){ setup_maze(16,LOC(8,8)); generate_maze(1234, 6); solve_maze(); }