Nah lo! Sebetulnya, maze itu emang ada yang ga ada jalan keluarnya!
Pernah kan masuk maze? Sekarang kita bikin programnya OK?
Ketikkan (copy) kode dibawah ini:
//beginning of code
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void traverse(char[28][28]);
int main(){
char maze[28][28];
int panjang=0,lebar=0,x=30,y=30,tgtx,tgty,face=0;
printf("Berapa ukurannya (max 25)?\n");
while (panjang<1 || panjang>25){
printf("Panjang (kiri ke kanan): ");
scanf("%d",&panjang);
}
while (lebar<1 || lebar>25){
printf("Lebar (atas ke bawah): ");
scanf("%d",&lebar);
}
for(int i=0;i<lebar+2;i++){
for(int j=0;j<panjang+2;j++){
maze[i][j]=' ';
}
}
printf("Rancang maze. Gunakan:\n@ sebagai mulai\nX sebagai dinding\n* sebagai finish\nspasi, untuk ruang\n");
fflush(stdin);
for(int i=1;i<=lebar;i++){
gets(maze[i]);
for(int j=panjang-1;j>=0;j--){
maze[i][j+1]=maze[i][j];
}
for(int j=1;j<=panjang;j++){
if(maze[i][j]=='\0'){
maze[i][j+1]='\0';
maze[i][j]=' ';
}
}
}
for(int i=0;i<panjang+2;i++){
maze[i][0]='X';
maze[i][panjang+1]='X';
maze[0][i]='X';
maze[lebar+1][i]='X';
}
for(int i=1;i<=lebar;i++){
for(int j=1;j<=panjang;j++){
if(maze[i][j]=='@'){
x=j;
y=i;
}
if(maze[i][j]=='*'){
tgtx=j;
tgty=i;
}
}
}
if(x==30 && y==30){
printf("Mana starting point?");
getch();
return 0;
}
while(x!=tgtx || y!=tgty){
system("cls");
for(int i=0;i<lebar+2;i++){
for(int j=0;j<panjang+2;j++){
printf("%c",maze[i][j]);
if(j==panjang+1) printf("\n");
}
}
switch(face){
case 0 :
if(maze[y][x+1]==' ' || maze[y][x+1]=='*'){
maze[y][x]=' ';
maze[y][x+1]='@';
x=x+1;
face=3;
}
else if(maze[y+1][x]==' ' || maze[y+1][x]=='*'){
maze[y][x]=' ';
maze[y+1][x]='@';
y=y+1;
}
else if(maze[y][x-1]==' ' || maze[y][x-1]=='*'){
maze[y][x]=' ';
maze[y][x-1]='@';
x=x-1;
face=1;
}
else if(maze[y-1][x]==' ' || maze[y-1][x]=='*'){
maze[y][x]=' ';
maze[y-1][x]='@';
y=y-1;
face=2;
}
break;
case 1 :
if(maze[y+1][x]==' ' || maze[y+1][x]=='*'){
maze[y][x]=' ';
maze[y+1][x]='@';
y=y+1;
face=0;
}
else if(maze[y][x-1]==' ' || maze[y][x-1]=='*'){
maze[y][x]=' ';
maze[y][x-1]='@';
x=x-1;
}
else if(maze[y-1][x]==' ' || maze[y-1][x]=='*'){
maze[y][x]=' ';
maze[y-1][x]='@';
y=y-1;
face=2;
}
else if(maze[y][x+1]==' ' || maze[y][x+1]=='*'){
maze[y][x]=' ';
maze[y][x+1]='@';
x=x+1;
face=3;
}
break;
case 2 :
if(maze[y][x-1]==' ' || maze[y][x-1]=='*'){
maze[y][x]=' ';
maze[y][x-1]='@';
x=x-1;
face=1;
}
else if(maze[y-1][x]==' ' || maze[y-1][x]=='*'){
maze[y][x]=' ';
maze[y-1][x]='@';
y=y-1;
}
else if(maze[y][x+1]==' ' || maze[y][x+1]=='*'){
maze[y][x]=' ';
maze[y][x+1]='@';
x=x+1;
face=3;
}
else if(maze[y+1][x]==' ' || maze[y+1][x]=='*'){
maze[y][x]=' ';
maze[y+1][x]='@';
y=y+1;
face=0;
}
break;
case 3 :
if(maze[y-1][x]==' ' || maze[y-1][x]=='*'){
maze[y][x]=' ';
maze[y-1][x]='@';
y=y-1;
face=2;
}
else if(maze[y][x+1]==' ' || maze[y][x+1]=='*'){
maze[y][x]=' ';
maze[y][x+1]='@';
x=x+1;
}
else if(maze[y+1][x]==' ' || maze[y+1][x]=='*'){
maze[y][x]=' ';
maze[y+1][x]='@';
y=y+1;
face=0;
}
else if(maze[y][x-1]==' ' || maze[y][x-1]=='*'){
maze[y][x]=' ';
maze[y][x-1]='@';
x=x-1;
face=1;
}
break;
}
getch();
}
system("cls");
for(int i=0;i<lebar+2;i++){
for(int j=0;j<panjang+2;j++){
printf("%c",maze[i][j]);
if(j==panjang+1) printf("\n");
}
}
getch();
return 0;
}
//end of code
Nah, sekarang bikin rancangan mazenya di notepad. Gunain @ sebagai mulai, X buat dinding, * buat finish.
Misalnya ini untuk maze dengan panjang 9 dan lebar 10:
Jalanin program C-nya (pencet F9).
Isi panjangnya (misal 9)
Isi lebarnya (misal 10).
Copy notepad-mu, di jendela program klik kanan di title bar>paste.
Enjoy! Tahan key apa aja untuk fast-forward.
Hope life is as easy as maze traversing...
No comments:
Post a Comment