~cpp 노 가 다 뱁 새
~cpp CASE [1][2][3] [4][ ][5] [6][7][8]
~cpp #include <stdio.h> #include <stdlib.h> #include <time.h> struct mouse_xy { int x; int y; }mouse; int main() { int map[5][5]; int a; srand(time(0)); a = rand() % 8 + 1; mouse.x=1; mouse.y=1; /////파일 입출력방법을 김승욱 교수님이 /////안알려주셨으므로 무효! /////데이터 입력부분 map[0][0]=0; map[0][1]=1; map[0][2]=0; map[0][3]=1; map[0][4]=1; map[1][0]=1; map[1][1]=0; map[1][2]=1; map[1][3]=1; map[1][4]=1; map[2][0]=0 ; map[2][1]=0; map[2][2]=1; map[2][3]=0; map[2][4]=1; map[3][0]=0; map[3][1]=0 ; map[3][2]=0; map[3][3]=1; map[3][4]=0; map[4][0]=0; map[4][1]=1; map[4][2]=0; map[4][3]=1; map[4][4]=0; ////////////////////데이터 입력 끝 while(1) { if (mouse.x==5 && mouse.y== 5) {break; } ///////마지막 부분에 도달했다면 while문을 나갑니다. a = rand() % 8 + 1; ////변수 a는 1~8사이의 랜덤값을갖습니다. switch (a){ case 1: if (0<mouse.x-1 && mouse.x-1<6 && 0<mouse.y-1 && mouse.y-1<6) //다음 예상되는 경로가 벽이 아닐경우 이동합니다. { if (map[mouse.x-1-1][mouse.y-1-1]==0) { //각 좌표에 맞게 이동합니다. mouse.x -=1; mouse.y -=1; printf("(%d,%d)=>(%d,%d)로갓눼\n ",mouse.x+1,mouse.y+1,mouse.x,mouse.y); break; } } case 2: if (0<mouse.x && mouse.x<6 && 0<mouse.y-1 && mouse.y-1<6) { if (map[mouse.x-1][mouse.y-1-1]==0) { mouse.y -=1; printf("(%d,%d)=>(%d,%d)로갓눼\n ",mouse.x,mouse.y+1,mouse.x,mouse.y); break; } } case 3: if (0<mouse.x+1 && mouse.x+1<6 && 0<mouse.y-1 && mouse.y-1<6) { if (map[mouse.x+1-1][mouse.y-1-1]==0) {mouse.x +=1; mouse.y -=1; printf("(%d,%d)=>(%d,%d)로갓눼\n ",mouse.x-1,mouse.y+1,mouse.x,mouse.y); break; } } case 4: if (0<mouse.x-1 && mouse.x-1<6 && 0<mouse.y && mouse.y<6) { if (map[mouse.x-1-1][mouse.y-1]==0) {mouse.x -=1; printf("(%d,%d)=>(%d,%d)로갓눼\n ",mouse.x+1,mouse.y,mouse.x,mouse.y); break; } } case 5: if (0<mouse.x+1 && mouse.x+1<6 && 0<mouse.y && mouse.y<6) { if (map[mouse.x+1-1][mouse.y-1]==0) {mouse.x +=1; printf("(%d,%d)=>(%d,%d)로갓눼\n ",mouse.x-1,mouse.y,mouse.x,mouse.y); break; } } case 6: if (0<mouse.x-1 && mouse.x-1<6 && 0<mouse.y+1 && mouse.y+1<6) { if (map[mouse.x-1-1][mouse.y+1-1]==0) {mouse.x -=1; mouse.y +=1; printf("(%d,%d)=>(%d,%d)로갓눼\n ",mouse.x+1,mouse.y-1,mouse.x,mouse.y); break; } } case 7: if (0<mouse.x && mouse.x<6 && 0<mouse.y+1 && mouse.y+1<6) { if (map[mouse.x-1][mouse.y+1-1]==0) { mouse.y +=1; printf("(%d,%d)=>(%d,%d)로갓눼\n ",mouse.x,mouse.y-1,mouse.x,mouse.y); break; } } case 8: if (0<mouse.x+1 && mouse.x+1<6 && 0<mouse.y+1 && mouse.y+1<6) { if (map[mouse.x+1-1][mouse.y+1-1]==0) {mouse.x +=1; mouse.y +=1; printf("(%d,%d)=>(%d,%d)로갓눼\n ",mouse.x-1,mouse.y-1,mouse.x,mouse.y); break; } } // switch 문의 특징으로 각 case에서 if문안에 break를 넣으므로서 // 다음 경로가 벽일경우 그 아래의 case문을 실행되도록(순차) // 해놔씀 } } return 0; }