扫雷小游戏

test.c #define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//选择菜单

void menu()

{

printf("***********************\n");

printf("***** 1. game1 *****\n");

printf("***** 2. game2 *****\n");

printf("***** 3. game3 *****\n");

printf("***** 0. exit *****\n");

printf("***********************\n");

}

//完成扫雷游戏

void game1()

{

char mine1[ROWS1][COLS1] = { 0 };//mine数组中存放布置好的雷的信息

char show1[ROWS1][COLS1] = { 0 };//show数组中存放排查出的雷的信息

InitBoard1(mine1, ROWS1, COLS1, '0');//数组全部初始化为'0'

InitBoard1(show1, ROWS1, COLS1, '');//数组全部初始化为''

SetMine1(mine1, ROW1, COL1);//布置雷,就9*9的棋盘上随机布置10个雷

DisplayBoard1(show1, ROW1, COL1);//打印棋盘

FindMine1(mine1, show1, ROW1, COL1);//排查雷

}

void game2()

{

char mine2[ROWS2][COLS2] = { 0 };

char show2[ROWS2][COLS2] = { 0 };

InitBoard2(mine2, ROWS2, COLS2, '0');

InitBoard2(show2, ROWS2, COLS2, '*');

SetMine2(mine2, ROW2, COL2);//布置雷,就16*16的棋盘上随机布置40个雷

DisplayBoard2(show2, ROW2, COL2);

FindMine2(mine2, show2, ROW2, COL2);

}

void game3()

{

char mine3[ROWS3][COLS3] = { 0 };

char show3[ROWS3][COLS3] = { 0 };

InitBoard3(mine3, ROWS3, COLS3, '0');

InitBoard3(show3, ROWS3, COLS3, '*');

SetMine3(mine3, ROW3, COL3);//布置雷,就30*16的棋盘上随机布置99个雷

DisplayBoard3(show3, ROW3, COL3);

FindMine3(mine3, show3, ROW3, COL3);

}

//开始游戏测试

void test()

{

int input = 0;

srand((unsigned int)time(NULL));

do

{

menu();

printf("请选择:>");

scanf("%d", &input);//1 0 x

switch (input)

{

case 1:

printf("***** 简单 *****\n");

game1();

break;

case 2:

printf("***** 中等 *****\n");

game2();

break;

case 3:

printf("***** 困难 *****\n");

game3();

break;

case 0:

printf("游戏结束,退出游戏\n");

break;

default:

printf("选择错误,重新选择\n");

break;

}

} while (input);

}

//主函数

int main()

{

test();

return 0;

}

game1.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//棋盘初始化的函数

void InitBoard1(char arr[ROWS1][COLS1], 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 DisplayBoard1(char arr[ROW1][COLS1], int row, int col)

{

int i = 0;

printf("------扫雷游戏------\n");

for (i = 0; i <= col; i++)

{

printf(" %2d ", i);

}

printf("\n");

for (i = 1; i <= row; i++)

{

int j = 0;

printf(" %2d ", i);

for (j = 1; j <= col; j++)

{

printf(" %2c ", arr[i][j]);

}

printf("\n");

}

}

//布置雷

void SetMine1(char arr[ROWS1][COLS1], 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--;

}

}

}

//计算输入坐标周围雷的个数

static int GetMineCount1(char mine1[ROWS1][COLS1], int x, int y)

{

int i = 0;

int count = 0;

for (i = x - 1; i <= x + 1; i++)

{

int j = 0;

for (j = y - 1; j <= y + 1; j++)

{

count += (mine1[i][j] - '0');

}

}

return count;

}

//排查雷

void FindMine1(char mine1[ROWS1][COLS1], char show1[ROWS1][COLS1], int row, int col)

{

int x = 0;

int y = 0;

int win = 0;

while (win < row * col - EASY_COUNT)

{

printf("请输入要排查的坐标:");

scanf("%d %d", &x, &y);

if (x >= 1 && x <= row && y >= 1 && y <= col)

{

if (show1[x][y] == '*')

{

if (mine1[x][y] == '1')

{

 printf("很遗憾,你被炸死了\n");

 DisplayBoard1(mine1, ROW1, COL1);//打印雷

 break;

}

else

{

 int count = GetMineCount1(mine1, x, y);

 show1[x][y] = count + '0';

 DisplayBoard1(show1, ROW1, COL1);

 win++;

}

}

else

{

printf("该坐标已经被排查了,重新输入坐标\n");

}

}

else

{

printf("坐标非法,请重新输入\n");

}

}

if (win == row * col - EASY_COUNT)

{

printf("恭喜你,排雷成功\n");

DisplayBoard1(mine1, ROW1, COL1);

}

}

game2.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//棋盘初始化的函数

void InitBoard2(char arr[ROWS2][COLS2], 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 DisplayBoard2(char arr[ROW2][COLS2], int row, int col)

{

int i = 0;

printf("------扫雷游戏------\n");

for (i = 0; i <= col; i++)

{

printf(" %2d ", i);

}

printf("\n");

for (i = 1; i <= row; i++)

{

int j = 0;

printf(" %2d ", i);

for (j = 1; j <= col; j++)

{

printf(" %2c ", arr[i][j]);

}

printf("\n");

}

}

//布置雷

void SetMine2(char arr[ROWS2][COLS2], int row, int col)

{

int count = MIDDING_COUNT;

while (count)

{

int x = rand() % row + 1;

int y = rand() % col + 1;

if (arr[x][y] == '0')

{

arr[x][y] = '1';

count--;

}

}

}

//计算输入坐标周围雷的个数

static int GetMineCount2(char mine2[ROWS2][COLS2], int x, int y)

{

int i = 0;

int count = 0;

for (i = x - 1; i <= x + 1; i++)

{

int j = 0;

for (j = y - 1; j <= y + 1; j++)

{

count += (mine2[i][j] - '0');

}

}

return count;

}

//排查雷

void FindMine2(char mine2[ROWS2][COLS2], char show2[ROWS2][COLS2], int row, int col)

{

int x = 0;

int y = 0;

int win = 0;

while (win < row * col - MIDDING_COUNT)

{

printf("请输入要排查的坐标:");

scanf("%d %d", &x, &y);

if (x >= 1 && x <= row && y >= 1 && y <= col)

{

if (show2[x][y] == '*')

{

if (mine2[x][y] == '1')

{

 printf("很遗憾,你被炸死了\n");

 DisplayBoard2(mine2, ROW2, COL2);//打印雷

 break;

}

else

{

 int count = GetMineCount2(mine2, x, y);

 show2[x][y] = count + '0';

 DisplayBoard2(show2, ROW2, COL2);

 win++;

}

}

else

{

printf("该坐标已经被排查了,重新输入坐标\n");

}

}

else

{

printf("坐标非法,请重新输入\n");

}

}

if (win == row * col - MIDDING_COUNT)

{

printf("恭喜你,排雷成功\n");

DisplayBoard2(mine2, ROW2, COL2);

}

}

game3.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//棋盘初始化的函数

void InitBoard3(char arr[ROWS3][COLS3], 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 DisplayBoard3(char arr[ROW3][COLS3], int row, int col)

{

int i = 0;

printf("------扫雷游戏------\n");

for (i = 0; i <= col; i++)

{

printf(" %2d ", i);

}

printf("\n");

for (i = 1; i <= row; i++)

{

int j = 0;

printf(" %2d ", i);

for (j = 1; j <= col; j++)

{

printf(" %2c ", arr[i][j]);

}

printf("\n");

}

}

//布置雷

void SetMine3(char arr[ROWS3][COLS3], int row, int col)

{

int count = HARD_COUNT;

while (count)

{

int x = rand() % row + 1;

int y = rand() % col + 1;

if (arr[x][y] == '0')

{

arr[x][y] = '1';

count--;

}

}

}

//计算输入坐标周围雷的个数

static int GetMineCount3(char mine3[ROWS3][COLS3], int x, int y)

{

int i = 0;

int count = 0;

for (i = x - 1; i <= x + 1; i++)

{

int j = 0;

for (j = y - 1; j <= y + 1; j++)

{

count += (mine3[i][j] - '0');

}

}

return count;

}

//排查雷

void FindMine3(char mine3[ROWS3][COLS3], char show3[ROWS3][COLS3], int row, int col)

{

int x = 0;

int y = 0;

int win = 0;

while (win < row * col - HARD_COUNT)

{

printf("请输入要排查的坐标:");

scanf("%d %d", &x, &y);

if (x >= 1 && x <= row && y >= 1 && y <= col)

{

if (show3[x][y] == '*')

{

if (mine3[x][y] == '1')

{

 printf("很遗憾,你被炸死了\n");

 DisplayBoard3(mine3, ROW3, COL3);//打印雷

 break;

}

else

{

 int count = GetMineCount3(mine3, x, y);

 show3[x][y] = count + '0';

 DisplayBoard3(show3, ROW3, COL3);

 win++;

}

}

else

{

printf("该坐标已经被排查了,重新输入坐标\n");

}

}

else

{

printf("坐标非法,请重新输入\n");

}

}

if (win == row * col - HARD_COUNT)

{

printf("恭喜你,排雷成功\n");

DisplayBoard3(mine3, ROW3, COL3);

}

}

game.h

#define _CRT_SECURE_NO_WARNINGS 1

#pragma once

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define ROW1 9

#define COL1 9

#define ROWS1 ROW1+2

#define COLS1 COL1+2

#define ROW2 16

#define COL2 18

#define ROWS2 ROW2+2

#define COLS2 COL2+2

#define ROW3 30

#define COL3 18

#define ROWS3 ROW3+2

#define COLS3 COL3+2

#define EASY_COUNT 10

#define MIDDING_COUNT 40

#define HARD_COUNT 99

//声明函数

//棋盘初始化的函数

//game1

void InitBoard1(char arr[ROWS1][COLS1], int rows, int cols, char set);

//打印棋盘

void DisplayBoard1(char arr[ROWS1][COLS1], int row, int col);

//布置雷

void SetMine1(char arr[ROWS1][COLS1], int row, int col);

//排查雷

void FindMine1(char mine[ROWS1][COLS1], char show[ROWS1][COLS1], int row, int col);

//game2

void InitBoard2(char arr[ROWS2][COLS2], int rows, int cols, char set);

//打印棋盘

void DisplayBoard2(char arr[ROWS2][COLS2], int row, int col);

//布置雷

void SetMine2(char arr[ROWS2][COLS2], int row, int col);

//排查雷

void FindMine2(char mine[ROWS2][COLS2], char show[ROWS2][COLS2], int row, int col);

//game3

void InitBoard3(char arr[ROWS3][COLS3], int rows, int cols, char set);

//打印棋盘

void DisplayBoard3(char arr[ROWS3][COLS3], int row, int col);

//布置雷

void SetMine3(char arr[ROWS3][COLS3], int row, int col);

//排查雷

void FindMine3(char mine[ROWS3][COLS3], char show[ROWS3][COLS3], int row, int col);

alt

#C语言#
全部评论

相关推荐

03-17 19:21
门头沟学院 Java
面试官_我太想进步了:正常企查查显示的员工一般比设计的少
点赞 评论 收藏
分享
高斯林的信徒:问你有没有保底,好人啊,就差把这是kpi面告诉你了
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务