{{{ /* mine.c: 지뢰찾기 소스 파일(TUI) 종류: win32 console program 언어: C 제작자: 윤종하 제작 시작일: 2010년 12월 24일 1차 종료일: 2010년 12월 29일*/ #include #include #include #include #include #define TRUE 1 #define FALSE 0 //#define NO_STATE 3 typedef struct cell//1개 셀에 포함된 데이터의 구조체 { int iIsRevealed; int iIsMine; int iNumOfMine;//주변에 있는 지뢰의 개수 int iIsUnknown; int iIsFined; }CELL; COORD* make_mine_map(CELL** map,COORD size,int iNumOfMine); void print_map(CELL** map,COORD size,int iNumOfMine,int iCurrentFindedMine); int click_cell(CELL** map,COORD size,int *iNumOfLeftCell); void one_right_click_cell(CELL** map,COORD size,COORD *cPosOfMine,int iNumOfMine,int *iFindedRealMine); void double_right_click_cell(CELL** map,COORD size); void find_mine(CELL** map,COORD size,COORD pos,int *iNumOfLeftCell); void count_time(int iSecond); void print_one_cell(CELL** map,int xpos,int ypos,int mine); COORD coord_input(COORD size); void initialize_cell(CELL *input); int search_mine(int iNumOfMine,COORD* real_mine_cell,COORD target_cell); int main(int argc,char* argv[]) { CELL **map; COORD size; COORD *cPosOfMine; int iNumOfMine,iCurrentFindedMine=0,iNumOfLeftCell,iIsAlive=TRUE,tempX,tempY,iFindedRealMine=0,i,j; time_t tStartTime,tEndTime; char cSelect; /*걍 멋있으라고 한 시작화면*/ for(i=0;i<5;i++) printf("*****************************************************************************\n"); printf("********** **********\n"); printf("********** **********\n"); printf("********** **********\n"); printf("********** 지뢰찾기 **********\n"); printf("********** **********\n"); printf("********** **********\n"); printf("********** **********\n"); printf("********** **********\n"); printf("********** 크리스마스에 할 거 없어서 만듦*********\n"); for(i=0;i<5;i++) printf("\*****************************************************************************\n"); /*map 작성 시작*/ if(argc==4){//argument로의 맵사이즈 입력이 있을 경우 size.X=(short)atoi(argv[1]); size.Y=(short)atoi(argv[2]); } else{//argument로의 입력이 없을 경우 printf("맵의 사이즈를 입력하세요(가로 세로): "); scanf("%d %d",&tempX,&tempY); size.X=tempX; size.Y=tempY; } map=(CELL**)malloc(sizeof(CELL)*size.Y);//1차동적할당 for(i=0;iiNumOfMine && iIsAlive==TRUE && iFindedRealMine!=iNumOfMine); time(&tEndTime);//종료시간 저장 free(cPosOfMine); /*승패 알림 메시지 출력*/ system("cls"); if(iIsAlive==TRUE) printf("축하합니다! 당신이 승리했습니다.\a\a\a\n"); else if(iIsAlive==FALSE) printf("당신이 졌습니다.\a\a\n"); else printf("Unproteted error is occured!\a"); /*모든 지뢰의 위치 출력*/ for(i=0;isize.X-1 || temp_pos.X<0) || (temp_pos.Y>size.Y-1 || temp_pos.Y<0)) continue;//범위를 초과했을 경우 else if(map[temp_pos.Y][temp_pos.X].iIsRevealed==TRUE) continue;//이미 깐 것일 경우 else if(map[temp_pos.Y][temp_pos.X].iIsMine==TRUE) iNumOfMine++; } } map[pos.Y][pos.X].iNumOfMine=iNumOfMine;//주변 지뢰의 개수 저장 if(map[pos.Y][pos.X].iNumOfMine!=0) return;//지뢰의 개수가 0개가 아니면 끝 else{//주변의 데이터 탐색시작 for(coordY=-1;coordY<=1;coordY++){ for(coordX=-1;coordX<=1;coordX++){ temp_pos=pos; temp_pos.X+=coordX; temp_pos.Y+=coordY; if((temp_pos.X>size.X-1 || temp_pos.X<0) || (temp_pos.Y>size.Y-1 || temp_pos.Y<0)) continue;//범위를 초과했을 경우 if(coordX==0 && coordY==0) continue;//기존 위치를 참조했을 경우. 지우면 stack overflow의 향연 if(map[temp_pos.Y][temp_pos.X].iIsRevealed==TRUE) continue;//열려있을 경우는 검사할 필요가 없잖아 find_mine(map,size,temp_pos,&(*iNumOfLeftCell)); } } } } void count_time(int iSecond) { for(;iSecond>0;iSecond--){ printf("%d\a ",iSecond); Sleep(1000); } printf("%d\a\n",iSecond); } void print_one_cell(CELL **map,int xpos,int ypos,int mine) { if(map[ypos][xpos].iIsRevealed==TRUE){ if(mine==TRUE && map[ypos][xpos].iIsMine==TRUE) printf(" *"); else if(map[ypos][xpos].iNumOfMine==0) printf("□"); else printf(" %d",map[ypos][xpos].iNumOfMine); } else{ if(map[ypos][xpos].iIsFined==TRUE) printf(" m"); else if(map[ypos][xpos].iIsUnknown==TRUE) printf(" ?"); else if(mine==TRUE && map[ypos][xpos].iIsMine) printf(" *"); else printf("■"); } } COORD coord_input(COORD size) { COORD temp; int tempX,tempY; printf("좌표를 입력하세요(x y): "); scanf("%d %d",&tempX,&tempY); temp.X=(short)tempX; temp.Y=(short)tempY; while(temp.X<0 || temp.X>size.X-1 || temp.Y<0 || temp.Y>size.Y-1){ printf("좌표 입력이 잘못됐습니다. 다시 입력하세요.\n좌표를 입력하세요(x y): "); scanf("%d %d",&tempX,&tempY); temp.X=(short)tempX; temp.Y=(short)tempY; } return temp; } void initialize_cell(CELL *input) { input->iIsFined=FALSE; input->iIsMine=FALSE; input->iIsRevealed=FALSE; input->iIsUnknown=FALSE; } int search_mine(int iNumOfMine,COORD *real_mine_cell,COORD target_cell) { int i; for(i=0;i