扫雷
分模块,test.c,game.h,game.c
test.c
创建界面,让可以一直玩,do while,循环
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu() { printf("******************n"); printf("**** 1.paly ****n"); printf("**** 0.exit ****n"); printf("******************n"); } int main() { int input = 0; srand((unsigned int)time(NULL));//创建随机数 do { menu();//打印游戏界面 printf("请选择:>n"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏,游戏结束n"); break; default: printf("输入错误,请重新输入:>n"); break; } } while (input); return 0; }
game .h 介绍每个函数的作用
#pragma once #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 //定义雷的数量 #define LEIGESHU 10 #include#include #include void game(); void init_board(char arr[ROWS][COLS], int rows, int cols,char set);//初始化棋盘 void display_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, 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; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { arr[i][j] = set; } } }//初始化棋盘,用二维数组 void display_board(char arr[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("--------扫雷--------n"); for (i = 0; i <= col; i++) { printf("%d ", i); } printf("n"); for (i = 1; i <= row; i++) { printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ",arr[i][j]); } printf("n"); } printf("--------扫雷--------n"); }//打印棋盘,并且与其他的分隔开 void set_mine(char mine_board[ROWS][COLS], int row, int col) { int m = LEIGESHU; while (1) { int i = rand() % row + 1; int j = rand() % col + 1; if (mine_board[i][j] == '0') { mine_board[i][j] = '1'; m--; } if (0 == m) { break; } } }//放置雷,用一个定义的LEIGESHU方便修改雷的数量 int count_mine(char mine[ROWS][COLS],int x, int y) { return mine[x - 1][y] + 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] - 8 * '0'; }//求返回的雷 void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int win = 0; while (1) { printf("请输入排查雷的坐标:>n"); scanf("%d %d", &x, &y); if (x >=1 && x <= row && y >=1 && y <= col) { if (mine[x][y] == '1') { printf("很遗憾,你被炸死了n"); display_board(mine, row, col); break; } else { int ret = count_mine(mine, x, y); show[x][y] = '0' + ret; display_board(show, row, col); win++; } if (win == row * col - LEIGESHU) { printf("恭喜你,你赢了n"); break; } } else { printf("输入越界请重新输入:>n"); } } } void game() { char show_board[ROWS][COLS] = { 0 }; char mine_board[ROWS][COLS] = { 0 }; init_board(show_board, ROWS, COLS, '*'); init_board(mine_board, ROWS, COLS, '0'); display_board(show_board, ROW, COL); set_mine(mine_board,ROW,ROW); find_mine(mine_board, show_board, ROW, COL); }//按照函数依次进行
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)