game.h文件
主要是声明
#pragma once #include#include #include #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 void init_board(char arr[ROWS][COLS],int rows,int cols, char set); void show_board(char arr[ROWS][COLS],int row,int col); void set_mine(char arr[ROWS][COLS], int row, int col); void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
game.c文件
功能主体
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void init_board(char arr[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++) { arr[i][j] = set; } } } void show_board(char arr[ROWS][COLS], int row, int col) { printf("--------------扫雷------------------n"); int i = 0; for (i = 0; i <= col; i++) { printf("%d ", i); } printf("n"); for (i = 1; i <= row; i++) { printf("%d ", i); int j = 0; for (j = 1; j <= col; j++) { printf("%c ", arr[i][j]); } printf("n"); } printf("--------------扫雷------------------n"); } void set_mine(char arr[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (arr[x][y] == '0') { arr[x][y] = '1'; count--; } } } int get_mine_count(char arr[ROWS][COLS], int x, int y) { int i = 0; int j = 0; int count = 0; for (i = -1; i < 2; i++) { for (j = -1; j < 2; j++) { if ((arr[x + i][y + j])== '1') { count++; } } } return count+48; } void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int count = 0; while (row* col-count) { show_board(show, ROW, COL); printf("请输入->"); scanf("%d %d", &x, &y); if ((x > 0 && x <= row) && (x > 0 && x <= col)) { if (mine[x][y] == '1') { break; } else { show[x][y] = get_mine_count(mine, x, y); count++; } } else { printf("非法输入n"); } } if (row * col - count != 0) { printf("你被炸死了!n"); } else { printf("恭喜排雷成功!n"); } }
test.c文件
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" 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 }; //初始化数组 init_board(mine, ROWS, COLS, '0'); init_board(show, ROWS, COLS, '*'); //显示棋盘 ; //布置雷 set_mine(mine, ROW, COL); //排雷 find_mine(mine, show, ROW, COL); } int main() { int input = 0; srand((unsigned)time(NULL)); do { menu(); printf("请输入->"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("推出游戏"); break; default: printf("输入错误,请重新输入!"); break; } } while (input); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)