game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void init_board(char mine[ROWS][COLS], char set, int row, int col)//chushihua { memset(mine, '0', row*col*sizeof(mine[0][0])); } void set_mine(char mine[ROWS][COLS]) { int x, y; int count = 0; do{ x = rand() % 9 + 1; y = rand() % 9 + 1; if (mine[x][y] != '1') { mine[x][y] = '1'; count++; } } while (count<MAX); } void display(char board[ROWS][COLS], int row, int col)//打印棋盘 { int i = 0; int j = 0; printf(" "); //预留空位 for (i = 1; i <= ROW; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= ROW; i++) { printf("---"); } printf("\n"); for (i = 1; i<row - 1; i++) { printf("-| ", i); for (j = 1; j<col - 1; j++) { printf("%c ", board[i][j]); } printf("\n"); } } void finput(char mine[ROWS][COLS], int x, int y) { int a, b; if (mine[x][y] == '1')//如果第一次踩到雷 不炸 替换掉 { mine[x][y] = '0'; //printf("替换\n"); // display(mine,ROWS,COLS); while (1) { a = rand() % 9 + 1; b = rand() % 9 + 1; if (mine[a][b] != '1') { mine[a][b] = '1'; //printf("转移\n"); //display(mine,ROWS,COLS); break; } } } } void search_road(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y) { if (0 == mine_count(mine, x, y)) { show[x][y] = ' ';//如何实现调用自己 if ((x - 1)>0 && (y - 1)>0 && show[x - 1][y - 1] == '*') //x-1,y-1 search_road(mine, show, x - 1, y - 1); if ((y - 1)>0 && show[x][y - 1] == '*') //x,y-1 search_road(mine, show, x - 1, y - 1); if ((x + 1) <= ROW && (y - 1)>0 && show[x + 1][y - 1] == '*') //x+1,y-1 search_road(mine, show, x + 1, y - 1); if ((x + 1) <= ROW&&show[x + 1][y] == '*') //x+1,y search_road(mine, show, x + 1, y); if ((x + 1) <= ROW && (y + 1) <= COL&&show[x + 1][y + 1] == '*') //x+1,y+1 search_road(mine, show, x + 1, y + 1); if ((y + 1) <= COL&&show[x][y + 1] == '*') //x,y+1 search_road(mine, show, x, y + 1); if ((x - 1)>0 && (y + 1) <= COL&&show[x - 1][y + 1] == '*') //x-1,y+1 search_road(mine, show, x - 1, y + 1); if ((x - 1)>0 && show[x - 1][y] == '*') //x-1,y search_road(mine, show, x - 1, y); } else show[x][y] = mine_count(mine, x, y) + '0'; } int mine_count(char mine[ROWS][COLS], int x, int y) { int rcount; //rand mine count rcount = mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] + mine[x - 1][y] - 8 * 48; //返回值问题 //printf("%d\n",rcount); return rcount; } int check_win(char show[ROWS][COLS]) { int i, j, c_count = 0; for (i = 1; i <= ROW; i++) { for (j = 1; j <= COL; j++) { if (show[i][j] != '*') c_count++; } } return c_count; } #define _CRT_SECURE_NO_DEPRECATE 1 #include<stdio.h> #include<stdlib.h> #include"game.h" void menu() { printf("*******************************\n"); printf("*******************************\n"); printf("********1.PLAY 0.EXIT*********\n"); printf(" 请选择 \n"); } void game() { char mine[ROWS][COLS]; char show[ROWS][COLS]; int x, y; int icount = 0; //input count srand((unsigned int)time(NULL)); memset(mine, '0', ROWS*COLS*sizeof(mine[0][0])); memset(show, '*', ROWS*COLS*sizeof(show[0][0])); set_mine(mine); display(mine, ROWS, COLS); display(show, ROWS, COLS); while (1) { printf("请输入扫雷坐标:"); //输入坐标 scanf("%d%d", &x, &y); if (((x >= 1) && (x <= ROW)) && ((y >= 1) && (y <= COL)))//chongfushurukongbai { icount++; if (icount == 1) //第一次输入 遇到炸弹自动转移炸弹。再展开 { if (mine[x][y] == 1) { finput(mine, x, y); } show[x][y] = mine_count(mine, x, y) + '0'; search_road(mine, show, x, y); display(show, ROWS, COLS); } else { //不满足第一次输入 if (mine[x][y] == '1') //遇到炸弹 { printf("boom!!!\n"); display(mine, ROWS, COLS); break; } else { show[x][y] = mine_count(mine, x, y) + '0'; search_road(mine, show, x, y); display(show, ROWS, COLS); } } } else printf("坐标有误请重新输入\n"); if ((ROW*COL - MAX) == check_win(show)) { printf("恭喜你扫雷成功\n"); break; } } } enum { EXIT, PLAY };test.c #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> int main() { int input; do{ menu(); scanf("%d", &input); switch (input) { case 1:game(); break; case 0: break; default:printf("输入有误,请重新选择"); break; } } while (input != 0); system("pause"); return 0; }