头文件
#include#include #define _CRT_SECURE_NO_WARNINGS #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 void InitBoard(char mine[ROWS][COLS], int rows, int cols, char ret); void DisplayBoard(char mine[ROWS][COLS], int rows, int cols); void SetMine(char mine[ROWS][COLS], int rows, int cols); void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols); int get_mine_count(char mine[ROWS][COLS], int x, int y);
函数定义在game.c中
#include "game.h" #define _CRT_SECURE_NO_WARNINGS void InitBoard(char mine[ROWS][COLS], int rows, int cols,char ret) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { mine[i][j] = ret; } } } void DisplayBoard(char mine[ROWS][COLS], int rows, int cols) { for (int x = 0; x < cols; x++) { printf("%d ", x); } printf("n"); for (int i = 1; i < rows; i++) { printf("%d ", i); for (int j = 1; j < cols; j++) { printf("%c ", mine[i][j]); } printf("n"); } } void SetMine(char mine[ROWS][COLS], int rows, int cols) { int count = EASY_COUNT; while (count) { int x = rand() % ROW + 1; int y = rand() % COL + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } int get_mine_count(char mine[ROWS][COLS], int x, int y) { return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8*'0'; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols) { int x = 0; int y = 0; int win = 0; while (win"); scanf_s("%d%d",&x,&y); if (x >= 1 && x <= rows && y >= 1 && y <= cols) { if (mine[x][y] == '1') { printf("你被炸死了!n"); DisplayBoard(mine, ROWS-1, COLS-1); break; } else { int count = get_mine_count(mine,x,y); show[x][y] = count + '0'; DisplayBoard(show, ROWS - 1, COLS - 1); win++; } } else { printf("输入的坐标不合法!n"); } } if (win == EASY_COUNT) { printf("恭喜你完成游戏!n"); DisplayBoard(mine, ROWS - 1, COLS - 1); } }
主程序定义在test.c中
#include "game.h" #include#define _CRT_SECURE_NO_WARNINGS void menu() { printf("******************n"); printf("**** 1.play ****n"); printf("**** 0.exit ****n"); printf("******************n"); } void game() { char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; InitBoard(mine, ROWS, COLS,'0'); InitBoard(show, ROWS, COLS, '*'); //DisplayBoard(mine,ROWS-1,COLS-1); DisplayBoard(show,ROWS-1,COLS-1); SetMine(mine, ROWS, COLS); DisplayBoard(mine, ROWS - 1, COLS - 1); FindMine(mine, show, ROW, COL); } void test() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请选择:>n"); scanf_s("%d",&input); switch (input) { case 1: game(); break; case 0: printf("游戏已退出!n"); break; default: printf("请重新输入!n"); break; } } while (input); } int main() { test(); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)