admin管理员组文章数量:1794759
C语言——扫雷游戏
一.效果展示
✨ ✨ ✨ C语言版的扫雷游戏
在线扫雷游戏:点我即玩
二.代码实现
1.设置菜单
代码语言:javascript代码运行次数:0运行复制void Menu()
{
printf("*********************\n");
printf("******* 1. play *****\n");
printf("******* 0. exit *****\n");
printf("*********************\n");
}
void Test()
{
int input = 0;
do
{
Menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1: //选择1进入游戏
Game();
break;
case 0: //选择0退出游戏
printf("退出游戏\n");
break;
default: //输入的既不是0也不是1,重新输入
printf("选择错误,请重新选择:\n");
break;
}
} while (input);
}
2.雷盘初始化
代码语言:javascript代码运行次数:0运行复制void InitBoard(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;
}
}
}
3.雷盘打印
代码语言:javascript代码运行次数:0运行复制void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
printf("------扫雷游戏------\n");
for (int i = 0; i <= col; i++)
{
printf("%d ", i); //打印列号
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i); //打印行号
for (int j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
4.布置雷
代码语言:javascript代码运行次数:0运行复制void SetMine(char arr[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)// 循环次数>=10
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
5.排查雷
代码语言:javascript代码运行次数:0运行复制void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], 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 (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾你被炸死了\n");
DisplayBoard(mine, ROW, COL); //打印出雷的位置
break;
}
else
{
//该坐标不是雷就得统计坐标周围几个雷
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0'; //将数字转化为数字字符
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查了,重新输入坐标\n");
}
}
else
{
printf("坐标非法,请重新输入");
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你排雷成功");
DisplayBoard(mine, ROW, COL);
}
}
}
6.统计排查的坐标周围雷的个数
方法一
代码语言:javascript代码运行次数:0运行复制static int GetMineCount(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'; //将排查坐标的周围*个字符相加再减去8*‘0’得到的就是总共有几个雷
}
方法二
代码语言:javascript代码运行次数:0运行复制static int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
int count = 0;
int i = 0;
for (i = x - 1; i <= x + 1; i++)
{
int j = 0;
for (j = y - 1; j <= y + 1; j++)
{
count += mine[i][j] - '0';
}
}
return count;
}
三 .test.c
代码语言:javascript代码运行次数:0运行复制#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Menu()
{
printf("*********************\n");
printf("******* 1. play *****\n");
printf("******* 0. exit *****\n");
printf("*********************\n");
}
void Game()
{
//mine数组中存放布置好的雷的信息
//show数组中存放排查出的雷的信息
char mine[ROWS][COLS] = { 0 };//数组全部初始化为‘0’
char show[ROWS][COLS] = { 0 };//数组全部初始化为‘*’
//初始化函数
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//布置雷
//在9 * 9棋盘上随机布置10个雷
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//打印棋盘
//DisplayBoard(mine,ROW,COL);
DisplayBoard(show, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
void Test()
{
int input = 0;
rand((unsigned int)time(NULL));
do
{
Menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
Game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择:\n");
break;
}
} while (input);
}
int main()
{
Test();
return 0;
}
四. game.c
代码语言:javascript代码运行次数:0运行复制#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(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 DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
printf("------扫雷游戏------\n");
//打印列号
for (int i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);//打印行号
for (int j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
void SetMine(char arr[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)// 循环次数>=10
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
static int GetMineCount(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';*/ //方法一
int count = 0;
int i = 0;
for (i = x - 1; i <= x + 1; i++)
{
int j = 0;
for (j = y - 1; j <= y + 1; j++)
{
count += mine[i][j] - '0';
}
}
return count; //方法二
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], 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 (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
//该坐标不是雷就得统计坐标周围几个雷
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';//将数字转化为数字字符
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查了,重新输入坐标\n");
}
}
else
{
printf("坐标非法,请重新输入");
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你排雷成功");
DisplayBoard(mine, ROW, COL);
}
}
}
五. game.h
代码语言:javascript代码运行次数:0运行复制#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//声明函数
//棋盘初始化函数
void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void DisplayBoard(char arr[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char arr[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2024-10-21,如有侵权请联系 cloudcommunity@tencent 删除统计游戏int函数数组本文标签: c语言扫雷游戏
版权声明:本文标题:C语言——扫雷游戏 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1754686186a1705197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论