C语言-扫雷游戏

本文将对此游戏做一个大致的概述,此代码适合初学者,编写软件使用了vs2017。

该代码可以实现如下功能:

1.用户可以选择3个难度,分别布置不同个数的雷。

2.随机数设置雷的位置。

3.输入坐标进行排雷(周围没有雷可以展开一片,用0表示)。

4.输入的坐标为雷时,被炸死游戏结束。

5.排除所有的雷后,游戏结束,显示所用时间。

下面展示源代码:

头文件game.h

引用了几个必要的头文件,宏定义了行和列以及雷的个数,方便后续的更改。

 #define _CRT_SECURE_NO_WARNINGS 1

 #include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define ROW 9
#define COL 9 #define ROWS ROW+2
#define COLS COL+2 #define EASY_COUNT 10 void Initboard(char board[ROWS][COLS],int rows, int cols,char set);
void displayboard(char board[ROWS][COLS],int row,int col);
void setmine(char mine[ROWS][COLS], int row, int col,int count);
void findmine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col,int count);

测试模块 text.c

本模块主要针对于控制台窗口的颜色与大小,以及函数的调用。

#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()
{
int input = 0;
printf("请选择难度:\n1.简单\n2.困难\n3.炼狱\n");
scanf("%d", &input);
int count = input * EASY_COUNT;
//sprintf(stCmd, "mode con cols=%d lines=%d", a,b);
//system(stCmd);
char mine[ROWS][COLS];//存放雷的信息
char show[ROWS][COLS];//存放排查出的雷的信息
Initboard(mine, ROWS, COLS,'0'); //'0'
Initboard(show, ROWS, COLS,'*');//'*'
//displayboard(mine, ROW, COL);
setmine(mine, ROW, COL,count);
//displayboard(mine, ROW, COL);
displayboard(show, ROW, COL);
findmine(mine, show, ROW, COL,count);
}
void test()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("\n请选择:\n");
scanf("%d", &input);
switch (input) {
case 1:game(); break;
case 0:printf("退出游戏\n"); break;
default:printf("输入有误,请重新输入\n"); break;
}
} while (input); } int main()
{
int a = 35; int b = 16;
char stCmd[128];
system("color 4A");
system("mode con: cols=30 lines=12");
printf("\n\n\n-----欢迎进入扫雷游戏-----\n\n\n");
printf("----------请稍后----------\n\n\n");
Sleep(1000);
system("cls");
system("color 1A");
sprintf(stCmd, "mode con cols=%d lines=%d", a, b);
system(stCmd);
test();
return 0;
}

  游戏模块 game.c

此模块为各个函数的实现。

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void Initboard(char board[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++)
{
board[i][j] = set;
} }
} void displayboard(char show[ROWS][COLS], int row, int col)
{ int i = 0;
int j = 0;
printf("\n ");
for(i=0;i<=row;i++)
{
printf("%d ", i);
}
printf("\n");
printf(" ===========================\n");
for (i = 1; i <=row; i++)
{
printf("%d ||", i);
for (j = 1; j <= col; j++)
{
printf("%c ",show[i][j]);
}
printf("||");
printf("\n");
}
printf(" ============================\n"); }
void setmine(char mine[ROWS][COLS], int row, int col,int count)
{ system("cls");
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '0'+1;
count--;
} } }
static int judgemine(char mine[ROWS][COLS], int x,int y,int* count1)
{
(*count1)--;
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 Recursion(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int *count1)
{
int res = judgemine(mine, x, y, count1);
if (res == 0 && show[x][y] == '*')
{
show[x][y] = '0';
int arr[8][2] = { {-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1} };
for (int i = 0; i < 8; ++i)
{
if (x + arr[i][0] >= 1 && x + arr[i][0] <= ROW && y + arr[i][1] >= 1 && y + arr[i][1] <= COL
&& show[x + arr[i][0]][y + arr[i][1]] == '*')
Recursion(mine, show, x + arr[i][0], y + arr[i][1], count1);
}
}
else
show[x][y] = res + '0';
} void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int count)
{
int x = 0;
int y = 0;
int count1 = ROW * COL-count;
time_t start, end;
start = time(NULL);
while (count1)
{ printf("请输入要排查的坐标:\n");
scanf("%d %d", &x, &y);
system("cls");
if (show[x][y] != '*')
{
printf("此坐标已经排查过,请重新输入!\n");
displayboard(show, ROW, COL);
continue;
}
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '0' + 1)
{
printf("很遗憾,你被炸死了\n");
displayboard(mine, ROW, COL);
break;
}
else
{
Recursion(mine, show, x, y, &count1);
//displayboard(mine, ROW, COL);
displayboard(show, ROW, COL); }
}
else
{
printf("输入有误,请重新输入\n"); }end = time(NULL); } if (count1 == 0)
{
printf("恭喜你排雷成功!\n"); printf("用时:%d秒\n", (int)difftime(end, start));
}
}

以上便为此扫雷游戏的简单完成,本文到此结束谢谢!

c语言小游戏-扫雷的完成的更多相关文章

  1. 【C语言探索之旅】 第一部分第八课:第一个C语言小游戏

    ​ 内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写 ...

  2. 012-C语言小游戏之推箱子

    012-C语言小游戏之推箱子 一.创建游戏地图   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #define ROWS 11 #define COLS 12   char ...

  3. C语言小游戏: 2048.c

    概要:2048.c是一个C语言编写的2048游戏,本文将详细分析它的源码和实现.C语言是一种经典实用的编程语言,本身也不复杂,但是学会C语言和能够编写实用的程序还是有一道鸿沟的.本文试图通过一个例子展 ...

  4. c++小游戏———扫雷

    大家好,我是芝麻狐! 这是我自制的小游戏,目前仅支持devc++. 如果你没有c++软件, 请打开网站GDB online Debugger | Compiler - Code, Compile, R ...

  5. C语言小游戏: 推箱子 支线(一)--1

    好家伙,考完试了 回顾一下2021 回顾一下某次的作业 妙啊 所以, 做一个推箱子小游戏 1.先去4399找一下关卡灵感 就它了 2.在百度上搜几篇推箱子, 参考其中的"■ ☆"图 ...

  6. c++小游戏——扫雷

    #include<cstdio> #include<cstring> #include<algorithm> #include<conio.h> #in ...

  7. C语言 小游戏之贪吃蛇

    还记得非常久曾经听群里人说做贪吃蛇什么的,那时候大一刚学了C语言,认为非常难,根本没什么思路. 前不久群里有些人又在谈论C语言贪吃蛇的事了,看着他们在做,我也打算做一个出来. 如今大三,经过了这一年半 ...

  8. C语言小游戏——2048

      2048   2048这款游戏的玩法很简单,每次可以选择上下左右滑动,每滑动一次,所有的数字方块都会往滑动的方向靠拢,系统也会在空白的地方乱数出现一个数字方块,相同数字的方块在靠拢.相撞时会相加. ...

  9. C语言小游戏:贪吃蛇

    #include <graphics.h> #include <conio.h> #include <stdio.h> #define WIDTH 40 //设置宽 ...

随机推荐

  1. java中一些常考知识

    一.static的作用 static是修饰符,用于修饰成员变量(静态变量/类变量). static修饰的成员被所有对象共享. static优先于对象存在. static修饰的成员可以用类名.静态成员来 ...

  2. tomcat 介绍及环境搭建

    一.tomcat介绍 Tomcat 服务器是一个免费的开放源代码的 Web 应用服务器,属于轻量级应用服务器,在中小型 系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试 JSP 程序的首选. ...

  3. 图解kubernetes scheduler基于map/reduce无锁设计的优选计算

    优选阶段通过分离计算对象来实现多个node和多种算法的并行计算,并且通过基于二级索引来设计最终的存储结果,从而达到整个计算过程中的无锁设计,同时为了保证分配的随机性,针对同等优先级的采用了随机的方式来 ...

  4. 随机算法 - Miller_Rabin pollard_rho

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  5. KVM虚拟化基础

    关于虚拟化 什么是虚拟化 在计算机技术中,虚拟化(技术)或虚拟技术(英语:Virtualization)是一种资源管理技术,是将计算机的各种实体资源(CPU.内存.磁盘空间.网络适配器等),予以抽象. ...

  6. Treap基本用法总结

    Treap=Tree+Heap  起名的人非常有才 Treap是啥? 一棵二叉搜索树可能退化成链,那样各种操作的效率都比较低 于是可爱的Treap在每个节点原先值v的基础上加了一个随机数rnd,树的形 ...

  7. 关于Navicat连接oralcle出现Cannot load OCI DLL 87,126,193 ,ORA-28547等错误

    navicat连接oracle数据库报ORA-28547: connection to server failed, probable Oracle Net admin error错误的解决方法 na ...

  8. 制作一个类“全能扫描王”的简易扫描软件(opencv)

    相信很多人手机里都装了个“扫描全能王”APP,平时可以用它来可以扫描一些证件.文本,确实很好用,第一次用的时候确实感觉功能很强大啊算法很牛逼啊.但是仔细一想,其实这些实现起来也是很简单的,我想了下,实 ...

  9. GitHub学习之路1

    对于代码的管理以及维护上,GitHub不失为一个较为明智的选择.而对于GitHub的灵活应用也是相当重要的,以下记录为以防自己忘记. 1. 创建SSH Key ssh-keygen -t rsa –C ...

  10. 异想家Golang学习笔记

    1. 简介 官网:https://golang.google.cn/ 2. 编译器.工具链 编译 go build .\demo.go 编译和执行指令合二为一 go run demo.go 3. 注释 ...