/* mine.c: 뢰 (TUI) : win32 console program : C : : 2010 12 24 1 료: 2010 12 29*/ #include<stdio.h> #include<stdlib.h> #include<time.h> #include<conio.h> #include<windows.h> #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;i<size.Y;i++){ map[i]=(CELL*)malloc(sizeof(CELL)*size.X);//2동 for(j=0;j<size.X;j++) initialize_cell(&map[i][j]);// } if(argc==4) iNumOfMine=atoi(argv[3]);////argument 뢰 else{ printf("뢰 를 : "); scanf("%d",&iNumOfMine); } cPosOfMine=make_mine_map(map,size,iNumOfMine); iNumOfLeftCell=size.X*size.Y; printf(" 데 따른 맵 및 데 료됐.\n3 됩.\n"); count_time(3); //system("pause"); /* 루 */ time(&tStartTime);// do{ print_map(map,size,iNumOfMine,iCurrentFindedMine); printf(" \na: 르\ts: 뢰 \td: 는 뭐뭔 모르\tq: 료"); cSelect=getch(); fflush(stdin); cSelect=tolower(cSelect); switch(cSelect){ case 'a': iIsAlive=click_cell(map,size,&iNumOfLeftCell); if(iIsAlive==FALSE){ printf("뢰 \n"); system("pause");// 문 } break; case 's': one_right_click_cell(map,size,cPosOfMine,iNumOfMine,&iFindedRealMine); iCurrentFindedMine++; break; case 'd': double_right_click_cell(map,size); break; case 'q': for(i=0;i<size.Y;i++) free(map[i]);//2 동 료 free(map); free(cPosOfMine); printf(" 료.\n: 리마 \n"); system("pause"); exit(0); break; default: printf("못 ."); } }while(iNumOfLeftCell>iNumOfMine && 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;i<size.Y;i++){ for(j=0;j<size.X;j++) print_one_cell(map,j,i,TRUE); free(map[i]);//2 동 Sleep(500); printf("\n"); } free(map);//1 동 printf(": %ldsec.\n",tEndTime-tStartTime);// printf("2010 리마 만듦\n"); system("pause"); return 0; } COORD* make_mine_map(CELL **map,COORD size,int iNumOfMine) { int i,j; static COORD *pos_data; //FILE *txtForDebug=fopen("mine_pos.txt","w"); srand(time(NULL)); pos_data=(COORD*)malloc(sizeof(COORD)*iNumOfMine);//뢰 만 동 /*뢰 */ for(i=0;i<iNumOfMine;){ pos_data[i].X=rand()%size.X; pos_data[i].Y=rand()%size.Y; /*복 */ for(j=0;j<i;j++) if(pos_data[i].X==pos_data[j].X && pos_data[i].Y==pos_data[j].Y) continue;//복 면 i++; } /*맵 뢰 및 디 */ for(i=0;i<iNumOfMine;i++){ //printf("%d: %d %d\n",i,pos_data[i].X,pos_data[i].Y); map[pos_data[i].Y][pos_data[i].X].iIsMine=TRUE; //fprintf(txtForDebug,"%d: %d %d\n",i,(int)pos_data[i].X,(int)pos_data[i].Y); } //system("pause"); return pos_data; } void print_map(CELL **map,COORD size,int iNumOfMine,int iCurrentFindedMine) { int xIndex,yIndex; system("cls"); for(yIndex=-1;yIndex<size.Y;yIndex++){ for(xIndex=-1;xIndex<size.X;xIndex++){ if(yIndex==-1){ if(xIndex==-1) printf(" "); else printf("%d ",xIndex); } else if(xIndex==-1) printf("%d",yIndex); else{ print_one_cell(map,xIndex,yIndex,FALSE); } } printf("\n"); } printf("남 뢰 : %d\n",iNumOfMine-iCurrentFindedMine); } int click_cell(CELL** map,COORD size,int *iNumOfLeftCell) { COORD input=coord_input(size); if(map[input.Y][input.X].iIsRevealed==TRUE) return TRUE;//미 눌 else if(map[input.Y][input.X].iIsMine==TRUE) return FALSE;//뢰를 눌 else find_mine(map,size,input,&(*iNumOfLeftCell)); return TRUE; } void one_right_click_cell(CELL **map,COORD size,COORD *cPosOfMine,int iNumOfMine,int *iFindedRealMine) { COORD input=coord_input(size); if(map[input.Y][input.X].iIsRevealed==TRUE) return;//미 눌 else{ if(map[input.Y][input.X].iIsFined==TRUE) map[input.Y][input.X].iIsFined=FALSE; else map[input.Y][input.X].iIsFined=TRUE; } if(search_mine(iNumOfMine,cPosOfMine,input)==TRUE) (*iFindedRealMine)++; } void double_right_click_cell(CELL **map,COORD size) { COORD input=coord_input(size); if(map[input.Y][input.X].iIsRevealed==TRUE) return;//미 눌 else{ if(map[input.Y][input.X].iIsUnknown==FALSE) map[input.Y][input.X].iIsUnknown=TRUE; else map[input.Y][input.X].iIsUnknown=FALSE; } } void find_mine(CELL **map,COORD size,COORD pos,int *iNumOfLeftCell) { int iNumOfMine=0,coordX,coordY; COORD temp_pos=pos; if(map[pos.Y][pos.X].iIsMine==TRUE) return;//만 뢰면 료 map[pos.Y][pos.X].iIsRevealed=TRUE;// (*iNumOfLeftCell)--; for(coordY=-1;coordY<=1;coordY++){ for(coordX=-1;coordX<=1;coordX++){ temp_pos=pos; if(coordX==0 && coordY==0) continue;// 는 데를 temp_pos.X=pos.X+coordX; temp_pos.Y=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;//범를 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<iNumOfMine;i++) if(real_mine_cell[i].X==target_cell.X && real_mine_cell[i].Y==target_cell.Y) return TRUE; return FALSE; }
리마 모 뢰를 봤.
램 봤, 딩 대 .
램 봤, 딩 대 .
데 는