做了个扫雷,发现我最后要显示地雷位置的出现了问题,我觉得问题应该是出在最下面一个方程
我用的是VS,才学了两个多月,感觉有点力不从心。。整个代码花了半个月来写了。。。
问题是我在最下面的一个void start_game(void)的方程中。我做了一个踩到地雷就显示所以地雷位置,并且自动退出程序。但是放break来退出程序的话,我显示地雷的逻辑就完全没有用了,一定要拿掉才可以显示。。。
另外能不能帮我看看我判断胜利的逻辑有没有问题
感谢啊!!!!!
程序代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 18 #define COL 10 #define MAX_MINES ROW #define PADDED_TOP_ROW 1 #define PADDED_LEFT_COL 1 #define PADDED_BOTTOM_ROW PADDED_TOP_ROW #define PADDED_RIGHT_COL PADDED_LEFT_COL #define MAX_PADDED_ROW (PADDED_TOP_ROW + ROW + PADDED_BOTTOM_ROW) // top and bottom rows padded 20 #define MAX_PADDED_COL (PADDED_LEFT_COL + COL + PADDED_RIGHT_COL) // left and right columns padded 12 #define STAR '*' #define SPACE ' ' #define MINE '1' void initgrid(char grid[], int row_size, int col_size); void printgrid(char grid[ROW*COL], int row_size, int col_size); void start_game(void); void mineposition(int mine[], int row, int col); int checknearbymines(int row, int col); int leftstars(char grid, char mines, int row, int col); int main(void) { start_game(); system("pause"); return 0; } void initgrid(char grid[], char mine[], int row_size, int col_size) { int i,j; for (i = 0; i < row_size; i++) { for (j=0;j<col_size;j++) { if ((i == 0) || (i == row_size -1) || (j == 0) || (j == col_size -1)) grid[i*col_size+j] = SPACE; else grid[i*col_size+j] = STAR; mine[i*col_size+j] = SPACE; } } } void printgrid(char grid[], int row_size, int col_size) { int i,j; printf("\t 1 2 3 4 5 6 7 8 9 10\n"); printf("\t --------------------\n"); for(i=0;i<row_size;i++) { if ((i > 0) && (i < row_size - 1)) printf("%d|\t", i); for(j=0;j<col_size;j++) printf("%c ",grid[i*col_size + j]); printf("\n"); } } void mineposition(char mine[], int row_size, int col_size) { int i, j, random, found; static int init=0; int m[MAX_MINES] = {0}; int row, col; if (init == 0) { srand(time(NULL)); init = 1; } i = 0; for(;;) { random = rand() % (ROW * COL - 1); found = 0; for (j = 0; j < i; j++) { if (m[j] == 1) // 1 = mine, 0 = not a mine { found = 1; break; } } if (found == 0) { m[i] = random; i++; if (i >= MAX_MINES) break; } } for (i = 0; i < MAX_MINES; i++) { row = m[i] / COL + 1; col = m[i] % COL + 1; mine[row * col_size + col] = MINE; } } int checknearbymines(char board[], int row, int col) { int mines = 0; if(board[row*MAX_PADDED_COL+col-1] == MINE) //check left mines++; if(board[row*MAX_PADDED_COL+col+1] == MINE) //check right mines++; if(board[row*MAX_PADDED_COL+col-MAX_PADDED_COL] == MINE) //check up mines++; if(board[row*MAX_PADDED_COL+col+MAX_PADDED_COL] == MINE) //check down mines++; if(board[row*MAX_PADDED_COL+col-MAX_PADDED_COL-1] == MINE) //check left top mines++; if(board[row*MAX_PADDED_COL+col-MAX_PADDED_COL+1] == MINE) //check right top mines++; if(board[row*MAX_PADDED_COL+col+MAX_PADDED_COL-1] == MINE) //check left bottom mines++; if(board[row*MAX_PADDED_COL+col+MAX_PADDED_COL+1] == MINE) //check right bottom mines++; return mines; } int leftstars(char grid[]) { int i, j, left; for(i = 13; i < 227; i++) { if(grid[i] == STAR) left++; } return left; } 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; initgrid(field, mine, MAX_PADDED_ROW, MAX_PADDED_COL); mineposition(mine, MAX_PADDED_ROW, MAX_PADDED_COL); for(;;) { printgrid(field,MAX_PADDED_ROW,MAX_PADDED_COL); // 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 * MAX_PADDED_COL + j) % MAX_PADDED_COL); } } } // end: to be deleted: for testing purpose //mine[row * col_size + col] 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("\n\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) { // need to translate the row & col in the field array to the corresponding index in mine array a = checknearbymines(mine, row,col); if(a==0) { field[row*MAX_PADDED_COL+col]=' '; //centre position field[row*MAX_PADDED_COL+col-1]=' '; //left field[row*MAX_PADDED_COL+col+1]=' '; //right field[row*MAX_PADDED_COL+col-MAX_PADDED_COL]=' ';//top field[row*MAX_PADDED_COL+col+MAX_PADDED_COL]=' '; //bottom field[row*MAX_PADDED_COL+col-MAX_PADDED_COL-1]=' '; //left top field[row*MAX_PADDED_COL+col+MAX_PADDED_COL+1]=' '; //right bottom field[row*MAX_PADDED_COL+col-MAX_PADDED_COL+1]=' '; //right top field[row*MAX_PADDED_COL+col+MAX_PADDED_COL-1]=' '; //left bottom } else if(a==1) { field[row*MAX_PADDED_COL+col]='1'; } else if(a==2) { field[row*MAX_PADDED_COL+col]='2'; } else if(a==3) { field[row*MAX_PADDED_COL+col]='3'; } else if(a==4) { field[row*MAX_PADDED_COL+col]='4'; } else if(a==5) { field[row*MAX_PADDED_COL+col]='5'; } else if(a==6) { field[row*MAX_PADDED_COL+col]='6'; } else if(a==7) { field[row*MAX_PADDED_COL+col]='7'; } else { field[row*MAX_PADDED_COL+col]='8'; } //if (leftstars(field) == MAX_MINES) //{ // printf("Congratulations!! You won!!"); //} } 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]= 'X'; field[row*MAX_PADDED_COL+col] = 'Y'; } } } printf("You hit a mine!! Game over..."); system("cls"); system("pause"); } } system("cls"); } }
[ 本帖最后由 卷毛的企鹅 于 2013-8-7 23:55 编辑 ]