做了如下修改,求技术指正。。。
程序代码:
void displaynum(char field[], int num, int row, int col) { if(num==0) { field[row*MAX_PADDED_COL+col]=SPACE; //centre position if(field[row*MAX_PADDED_COL+col-1] == STAR) field[row*MAX_PADDED_COL+col-1]=SPACE; //left if(field[row*MAX_PADDED_COL+col+1] == STAR) field[row*MAX_PADDED_COL+col+1]=SPACE; //right if(field[row*MAX_PADDED_COL+col-MAX_PADDED_COL] == STAR) field[row*MAX_PADDED_COL+col-MAX_PADDED_COL]=SPACE;//top if(field[row*MAX_PADDED_COL+col+MAX_PADDED_COL] == STAR) field[row*MAX_PADDED_COL+col+MAX_PADDED_COL]=SPACE; //bottom if(field[row*MAX_PADDED_COL+col-MAX_PADDED_COL-1] == STAR) field[row*MAX_PADDED_COL+col-MAX_PADDED_COL-1]=SPACE; //left top if(field[row*MAX_PADDED_COL+col-MAX_PADDED_COL+1] == STAR) field[row*MAX_PADDED_COL+col-MAX_PADDED_COL+1]=SPACE; //right top if(field[row*MAX_PADDED_COL+col+MAX_PADDED_COL+1] == STAR) field[row*MAX_PADDED_COL+col+MAX_PADDED_COL+1]=SPACE; //right bottom if(field[row*MAX_PADDED_COL+col+MAX_PADDED_COL-1]==STAR) field[row*MAX_PADDED_COL+col+MAX_PADDED_COL-1]=SPACE; //left bottom } else if(num==1) { field[row*MAX_PADDED_COL+col]='1'; } else if(num==2) { field[row*MAX_PADDED_COL+col]='2'; } else if(num==3) { field[row*MAX_PADDED_COL+col]='3'; } else if(num==4) { field[row*MAX_PADDED_COL+col]='4'; } else if(num==5) { field[row*MAX_PADDED_COL+col]='5'; } else if(num==6) { field[row*MAX_PADDED_COL+col]='6'; } else if(num==7) { field[row*MAX_PADDED_COL+col]='7'; } else { field[row*MAX_PADDED_COL+col]='8'; } } void start_game(void) { char field[MAX_PADDED_ROW * MAX_PADDED_COL]; char mine[MAX_PADDED_ROW * MAX_PADDED_COL] = {0}; int row, col, a, i, j,b,checkstar; initgrid(field, mine, MAX_PADDED_ROW, MAX_PADDED_COL); mineposition(mine, MAX_PADDED_ROW, MAX_PADDED_COL); row=0; col=0; checkstar = 0; for(;;) { printgrid(field,MAX_PADDED_ROW,MAX_PADDED_COL); if(mine[row*MAX_PADDED_COL+col] == MINE) { printf("You hit a mine!! Game over...\n"); break; } if(checkstar==MAX_MINES) { printf("Congratulations! You win!\n"); break; } // begin: to be deleted: for testing purpose for (i=0;i<MAX_PADDED_ROW;i++) { for (j = 0; j < MAX_PADDED_COL; j++) { if (mine[i * MAX_PADDED_COL + j] == MINE) { printf("[%d][%d]\t", /*(i * MAX_PADDED_COL + j) / MAX_PADDED_COL*/i, /*(i * MAX_PADDED_COL + j) % MAX_PADDED_COL)*/j); } } } // end: to be deleted: for testing purpose printf("\nEnter <-1 -1> to exit game...\n\n"); printf("row and column <[row] [col]>':\n"); scanf("%d %d",&row,&col); if (row == -1 && col == -1) { printf("\nExiting from game...\n"); break; } else if (((row < 1 )||( row > ROW)) && ((col<1)||(col >COL))) { printf("\nInvalid tile position! Please enter row and column again...\n\n"); } else { if(mine[row*MAX_PADDED_COL+col] != MINE) { a = checknearbymines(mine, row,col); displaynum(field, a, row, col); } else { for (i=0;i<MAX_PADDED_ROW;i++) { for (j = 0; j < MAX_PADDED_COL; j++) { if (mine[i * MAX_PADDED_COL + j] == MINE) { field[i * MAX_PADDED_COL + j]= 'Y'; } } } field[row*MAX_PADDED_COL+col] = 'X'; } } checkstar=0; for(b=0;b<MAX_PADDED_ROW*MAX_PADDED_COL;b++) { if(field[b]==STAR) { checkstar++; } } system("cls"); } }