思想:
创建棋盘
放雷
排查雷
头文件
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void Displayboard(char board[ROW][COL], int row, int col);
void Setmine(char board[ROWS][COLS], int rows, int cols);
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
测试
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("*************************\n");
printf("**** 1.play 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, ROW, COL);
Displayboard(show, ROW, COL);
//布置雷
Setmine(mine, ROWS, COLS);
//Displayboard(mine, ROW, COL);
//排查雷
Findmine(mine, show, ROW, COL);
}
int main()
{
menu();
srand ((unsigned int)time(NULL));
printf("请输入数字");
int input;
do {
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("退出游戏");
default:
printf("请重新输入");
break;
}
} while (input);
return 0;
}
game
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i;
int j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
board[i][j] = set;
}
}
}
void Displayboard(char board[ROWS][COLS], int row, int col)
{
int i;
int j;
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 ", board[i][j]);
}
printf("\n");
}
printf("扫雷游戏\n");
}
void Setmine(char board[ROWS][COLS], int rows, int cols)
{
int count = 10;
while (count) {
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (board[x][y] == '0') {
board[x][y] = '1';
count--;
}
}
//Displayboard(board, ROW, COL);
}
int get_mine(char mine[ROWS][COLS], int x, int y)
{
int ret;
ret= (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');
return ret;
}
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y=0;
int ret;
while (1) {
printf("请输入坐标\n");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= 9 && y >= 1 && y <= 9) {
if (mine[x][y] == '1') {
printf("你被炸死了\n");
Displayboard(mine, ROW, COL);
break;
}
else {
ret = get_mine(mine, x, y);
show[x][y] = ret + '0';
Displayboard(show, ROW, COL);
}
}
else {
printf("坐标不合法请重新输入");
}
}
}
这个函数也可以这个样
int get_mine(char mine[ROWS][COLS], int x, int y)
{
/*int ret;
ret= (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');
return ret;*/
int i;
int j;
int count = 0;
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
if (mine[x + i][y + j] == '*')
count++;
}
}
return count;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)