在9X9的雷盘中随机分布有10个雷(雷盘的大小和雷的个数可自己设置),选择雷盘上的坐标,若是雷,游戏结束;若不是雷,则在此坐标上显示周围雷的个数(以此坐标为中心,最近的八个位置),一直游戏,直到雷盘上只剩10个雷所在的坐标未被选择时即胜利。
建立两个数组,数组mine:用来存放雷,‘0’代表不是雷,‘1’代表雷,这个数组在玩游戏时不显示,左图;数组show:玩游戏时所显示的数组,右图;游戏开始时全为‘*’,选择坐标后显示周围雷的个数。
分析:如果用9X9的数组来存放雷,即mine[9][9],会出现一个问题:每条边上的元素周围并没有八个元素,这不方便后面统计周围雷的个数,因此把雷盘设置为11X11,mine[11][11]这样方便统计,但雷存储在mine[1][1]到mine[9][9]之间。同样show也设置为[11][11],但显示时只显示show[1][1]到show[9][9]。
同三子棋一样。
int main() { srand(time(NULL)); int input = 0; do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出\n"); break; default: printf("请重新选择:>"); break; } } while (input); }建立两个数组mine和show,在开始时对他们初始化,mine全为‘0’;show全为‘*’;这里用Inic函数来实现。
void Inic(char board[][COLS], int rows, int cols, char p) { /*int i = 0; int j = 0; for (i = 0; i <= rows; i++) { for (j = 0; j <= cols; j++) { board[i][j] = p; } }*/ memset(&board[0][0], p, sizeof(board[0][0])*rows*cols); }随机生成count = 10 个雷。注:雷用‘1’来表示。
void Set(char board[][COLS], int row, int col, int n) { int x = 0; int y = 0; while(n) { x = 1 + rand() % row; y = 1 + rand() % col; if (board[x][y] == '0') { board[x][y] = '1'; n--; } } }选择坐标,如果是雷:失败,游戏结束,显示结果,即打印存放雷的数组mine;如果不是雷:统计周围雷的个数,GetMine(mine, x, y)返回一个整型,同时win计数加1,当win = 棋盘总坐标数 — 雷的个数时,说明剩下的坐标全是雷,胜利。通过循环即可实现游戏,胜利,失败时跳出循环。
void Cheek(char mine[][COLS], char show[][COLS], int row, int col)//定义为mine 和show 方便 { int x = 0; int y = 0; int win = 0; while (1) { printf("请输入坐标:>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row&&y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("雷,gg\n"); Print(mine, row, col); break; } else { int number = GetMine(mine, x, y); show[x][y] = number + '0'; Print(show, row, col); win++; if (win == col*row - count) { printf("胜利!\n"); Print(mine, row, col); break; } } } else printf("坐标错误,请重新输入\n"); } } int GetMine(char board[][COLS], int x, int y) { return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + board[x][y - 1] + board[x][y + 1] + board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0'; }game.h
#define _CRT_SECURE_NO_WARNINGS 2 #include<stdlib.h> #include<stdio.h> #include<time.h> #include<string.h> #define ROW 9//控制雷盘大小 #define COL 9 #define ROWS ROW+2//实际数组大小 #define COLS COL+2 #define count 10//设置雷的个数 void Inic(char board[][COLS], int rows, int cols, char p);//初始化 void Set(char board[][COLS], int row, int col, int n);//放雷 void Print(char board[][COLS], int row, int col); void Cheek(char board[][COLS], char Board[][COLS], int x, int y); int GetMine(char board[][COLS], int x, int y);test.c
#include"game.h" 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 };//显示的棋盘 Inic(mine, ROWS, COLS, '0'); Inic(show, ROWS, COLS, '*'); Set(mine, ROW, COL, count);//生成雷 //Print(mine, ROW, COL);//正常游戏时不会显示 Print(show, ROW, COL);//只打印row行,col列 Cheek(mine, show, ROW, COL); } int main() { srand(time(NULL)); int input = 0; do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出\n"); break; default: printf("请重新选择:>"); break; } } while (input); }game.c
#include<stdlib.h> #include<stdio.h> #include<time.h> #include<string.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define count 10 void Inic(char board[][COLS], int rows, int cols, char p);//初始化 void Set(char board[][COLS], int row, int col, int n);//放雷 void Print(char board[][COLS], int row, int col); void Cheek(char board[][COLS], char Board[][COLS], int x, int y); int GetMine(char board[][COLS], int x, int y);