c++小游戏———扫雷
大家好,我是芝麻狐!
这是我自制的小游戏,目前仅支持devc++。
如果你没有c++软件,
请打开网站GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++。
制作不易,记得关注哦!
代码分三段:
首先是头文件部分。
后面两段是主要代码。
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <ctime>
#include <windows.h>
#include <conio.h>
#define KEY_UP 0xE048
#define KEY_DOWN 0xE050
#define KEY_LEFT 0xE04B
#define KEY_RIGHT 0xE04D
#define KEY_ESC 0x001B
#define KEY_1 '1'
#define KEY_2 '2'
#define KEY_3 '3'
#define GAME_MAX_WIDTH 100
#define GAME_MAX_HEIGHT 100
#define STR_GAMETITLE "按方向键移动光标 1:翻开 2:插旗 3:翻开周围"
#define STR_GAMEWIN "祝贺!!! 你赢了!!!!\n谢谢光临!\n\n"
#define STR_GAMEOVER "你输了!!!\n"
#define STR_GAMEEND "按“ESC”继续\n"
class CConsoleWnd
{
public:
static int TextOut(const char*);
static int GotoXY(int, int);
static int CharOut(int, int, const int);
static int TextOut(int, int, const char*);
static int GetKey();
public:
};
int CConsoleWnd::GetKey()
{
int nkey=getch(),nk=0;
if(nkey>=128||nkey==0)nk=getch();
return nk>0?nkey*256+nk:nkey;
}
int CConsoleWnd::GotoXY(int x, int y)
{
COORD cd;
cd.X = x;cd.Y = y;
return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cd);
}
int CConsoleWnd::TextOut(const char* pstr)
{
for(;*pstr;++pstr)putchar(*pstr);
return 0;
}
int CConsoleWnd::CharOut(int x, int y, const int pstr)
{
GotoXY(x, y);
return putchar(pstr);
}
int CConsoleWnd::TextOut(int x, int y, const char* pstr)
{
GotoXY(x, y);
return TextOut(pstr);
}
class CSLGame:public CConsoleWnd
{
private:
private:
int curX,curY;
int poolWidth,poolHeight;
int bm_gamepool[GAME_MAX_HEIGHT+2][GAME_MAX_WIDTH+2];
public:
CSLGame():curX(0),curY(0){poolWidth=poolHeight=0;}
int InitPool(int, int, int);
int MoveCursor(){return CConsoleWnd::GotoXY(curX, curY);}
int DrawPool(int);
int WaitMessage();
int GetShowNum(int, int);
int TryOpen(int, int);
private:
int DFSShowNum(int, int);
private:
const static int GMARK_BOOM;
const static int GMARK_EMPTY;
const static int GMARK_MARK;
};
const int CSLGame::GMARK_BOOM = 0x10;
const int CSLGame::GMARK_EMPTY= 0x100;
const int CSLGame::GMARK_MARK = 0x200;
int CSLGame::InitPool(int Width, int Height, int nBoom)
{
poolWidth = Width; poolHeight = Height;
if(nBoom<=0 || nBoom>=Width*Height
|| Width <=0 || Width >GAME_MAX_WIDTH
|| Height<=0 || Height>GAME_MAX_HEIGHT
){
return 1;
}
for(int y=0; y<=Height+1; ++y)
{
for(int x=0; x<=Width+1; ++x)
{
bm_gamepool[y][x]=0;
}
}
srand(time(NULL));
while(nBoom)
{
int x = rand()%Width + 1, y = rand()%Height + 1;
if(bm_gamepool[y][x]==0)
{
bm_gamepool[y][x] = GMARK_BOOM;
--nBoom;
}
}
curX = curY = 1;
MoveCursor();
return 0;
}
int CSLGame::DrawPool(int bDrawBoom = 0)
{
for(int y=1;y<=poolHeight;++y)
{
CConsoleWnd::GotoXY(1, y);
for(int x=1;x<=poolWidth;++x)
{
if(bm_gamepool[y][x]==0)
{
putchar('.');
}
else if(bm_gamepool[y][x]==GMARK_EMPTY)
{
putchar(' ');
}
else if(bm_gamepool[y][x]>0 && bm_gamepool[y][x]<=8)
{
putchar('0'+bm_gamepool[y][x]);
}
else if(bDrawBoom==0 && (bm_gamepool[y][x] & GMARK_MARK))
{
putchar('@');
}
else if(bm_gamepool[y][x] & GMARK_BOOM)
{
if(bDrawBoom)
putchar('*');
else
putchar('.');
}
}
}
return 0;
}
int CSLGame::GetShowNum(int x, int y)
{
int nCount = 0;
for(int Y=-1;Y<=1;++Y)
for(int X=-1;X<=1;++X)
{
if(bm_gamepool[y+Y][x+X] & GMARK_BOOM)++nCount;
}
return nCount;
}
int CSLGame::TryOpen(int x, int y)
{
int nRT = 0;
if(bm_gamepool[y][x] & GMARK_BOOM)
{
nRT = EOF;
}
else
{
int nCount = GetShowNum(x,y);
if(nCount==0)
{
DFSShowNum(x, y);
}
else bm_gamepool[y][x] = nCount;
}
return nRT;
}
int CSLGame::DFSShowNum(int x, int y)
{
if((0<x && x<=poolWidth) &&
(0<y && y<=poolHeight) &&
(bm_gamepool[y][x]==0))
{
int nCount = GetShowNum(x, y);
if(nCount==0)
{
bm_gamepool[y][x] = GMARK_EMPTY;
for(int Y=-1;Y<=1;++Y)
for(int X=-1;X<=1;++X)
{
DFSShowNum(x+X,y+Y);
}
}
else bm_gamepool[y][x] = nCount;
}
return 0;
}
int CSLGame::WaitMessage()
{
int nKey = CConsoleWnd::GetKey();
int nRT = 0, nArrow = 0;
switch (nKey)
{
case KEY_UP:
{
if(curY>1)--curY;
nArrow=1;
}break;
case KEY_DOWN:
{
if(curY<poolHeight)++curY;
nArrow=1;
}break;
case KEY_LEFT:
{
if(curX>1)--curX;
nArrow=1;
}break;
case KEY_RIGHT:
{
if(curX<poolWidth)++curX;
nArrow=1;
}break;
case KEY_1:
{
nRT = TryOpen(curX, curY);
}break;
case KEY_2:
{
if((bm_gamepool[curY][curX]
& ~(GMARK_MARK|GMARK_BOOM))==0)
{
bm_gamepool[curY][curX] ^= GMARK_MARK;
}
}break;
case KEY_3:
{
if(bm_gamepool[curY][curX] & 0xF)
{
int nb = bm_gamepool[curY][curX] & 0xF;
for(int y=-1;y<=1;++y)
for(int x=-1;x<=1;++x)
{
if(bm_gamepool[curY+y][curX+x] & GMARK_MARK)
--nb;
}
if(nb==0)
{
for(int y=-1;y<=1;++y)
for(int x=-1;x<=1;++x)
{
if((bm_gamepool[curY+y][curX+x]
& (0xF|GMARK_MARK)) == 0)
{
nRT |= TryOpen(curX+x, curY+y);
}
}
}
}
}break;
case KEY_ESC:
{
nRT = EOF;
}break;
}
if(nKey == KEY_1 || nKey == KEY_3)
{
int y=1;
for(;y<=poolHeight;++y)
{
int x=1;
for(;x<=poolWidth; ++x)
{
if(bm_gamepool[y][x]==0)break;
}
if(x<=poolWidth) break;
}
if(! (y<=poolHeight))
{
nRT = 1;
}
}
if(nArrow==0)
{
DrawPool();
}
MoveCursor();
return nRT;
}
int main(void)
{
int x=50, y=20, b=100,n;
CSLGame slGame;
{
CConsoleWnd::GotoXY(0,0);
CConsoleWnd::TextOut(STR_GAMETITLE);
slGame.InitPool(x,y,b);
slGame.DrawPool();
slGame.MoveCursor();
}
while((n=slGame.WaitMessage())==0);
{
slGame.DrawPool(1);
CConsoleWnd::TextOut("\n");
if(n==1)
{
CConsoleWnd::TextOut(STR_GAMEWIN);
}
else
{
CConsoleWnd::TextOut(STR_GAMEOVER);
}
CConsoleWnd::TextOut(STR_GAMEEND);
}
while(CConsoleWnd::GetKey()!=KEY_ESC);
return 0;
}
c++小游戏———扫雷的更多相关文章
- c++小游戏——扫雷
#include<cstdio> #include<cstring> #include<algorithm> #include<conio.h> #in ...
- c语言小游戏-扫雷的完成
C语言-扫雷游戏 本文将对此游戏做一个大致的概述,此代码适合初学者,编写软件使用了vs2017. 该代码可以实现如下功能: 1.用户可以选择3个难度,分别布置不同个数的雷. 2.随机数设置雷的位置. ...
- web版扫雷小游戏(一)
作为一名程序猿,平时的爱好也不多,说起游戏,我不太喜欢大型的网游,因为太耗时间,偶尔玩玩经典的单机小游戏,比如windows下自带的游戏扫雷(秀一下,高级下最高纪录110s). 现阶段正在致力于web ...
- Angular4 扫雷小游戏
扫雷小游戏,可以升级过关,难度随关卡增加.但是有很明显的bug,以后有时间会继续优化! HTML: <div class="mainContent"> <div ...
- 扫雷小游戏PyQt5开发【附源代码】
也没啥可介绍哒,扫雷大家都玩过. 雷的分布算法也很简单,就是在雷地图(map:二维数组)中,随机放雷,然后这个雷的8个方位(上下左右.四个对角)的数字(非雷的标记.加一后不为雷的标记)都加一. 如何判 ...
- C++扫雷小游戏(基于CMD命令行)
这个小游戏是笔者在大一C语言课程设计的时候写的,基于命令行,为了显得漂亮一些,特别加上了彩色特效~~~ 注意:Win10系统须将命令行调为旧版命令行,否则有可能会显示乱码! 代码示例: #includ ...
- 无聊的周末用Java写个扫雷小游戏
周末无聊,用Java写了一个扫雷程序,说起来,这个应该是在学校的时候,写会比较好玩,毕竟自己实现一个小游戏,还是比较好玩的.说实话,扫雷程序里面核心的东西,只有点击的时候,去触发更新数据这一步. Sw ...
- C++复现经典游戏——扫雷
国庆小长假,当大家都去看人山人海的时候,我独自一人狂码代码.这两天想要实现的内容是Windows上的一个经典游戏——扫雷.相信90后和一些上班族对此并不陌生.然而,从win8开始,扫雷就不再是Wind ...
- 使用cocos2d-x v3.1开发小游戏(基本框架)
小游戏的组成 欢迎界面 在游戏资源未全部加载完之前就需要载入,避免进入游戏会有一段黑屏时间. 可以用来展示游戏名称或者开发者logo. 开始菜单界面 一般用于显示游戏名称和关卡选择(或者称游戏难度选择 ...
随机推荐
- 附011.常见Linux镜像站点大全
开源系统镜像站点 国内Mirrors站点 企业类站点 阿里巴巴开源Mirrors站点:https://developer.aliyun.com/mirror/ 腾讯开源Mirrors站点:https: ...
- 电脑UEFI启动是什么?
UEFI 当EFI发展到1.1的时候,英特尔决定把EFI公之于众,于是后续的2.0吸引了众多公司加入,EFI也不再属于英特尔,而是属于了Unified EFI Form的国际组织,EFI在2.0后也遂 ...
- Windows Terminal无法加载WSL [process exited with code 4294967295 (0xffffffff)]
在Windows Terminal中WSL无法打开错误代码是 process exited with code 4294967295 (0xffffffff),但在命令行中 通过 "C:\W ...
- java_简单学习笔记
java_study笔记 前言: 所有笔记均总结自狂神的java基础视频 https://www.bilibili.com/video/BV12J41137hu?p=1 idea小技巧 psvm+回车 ...
- requests入门
1.通过GET请求获得搜索结果的网页源代码 import requests name=input("请输入想要搜索的明星:") url=f'https://www.sogou.co ...
- Hapoop安装学习(第一天)
学习任务: 1.安装虚拟机 Linux使用版本为Centos7,共安装3台虚拟机,一台主机和两台从机.主机命名为master,两台从机分别命名为s1和s2. master分配磁盘空间30G,s1和s2 ...
- 「ARC138E」Decreasing Subsequence(n logn 做法)
考虑一张 \(n\) 个点的图(\(V=\{0,1,\cdots,n\}\)):点 \(i\) 连向 \(a_i-1\),即 \(\text{index}\) 连向 \(\text{value}\). ...
- [USACO2021DEC] HILO 踩标做法
[USACO2021DEC] HILO Solution 参考自 官方题解 里提到的一篇 Obliteration.pdf,但是里面作者写出了极多错误...然后式子还错错得对了. 令 \(y=n-x\ ...
- 什么是Netty编解码,Netty编解码器有哪些?Protostuff怎么使用?
哈喽!大家好,我是小奇,一位热爱分享的程序员 小奇打算以轻松幽默的对话方式来分享一些技术,如果你觉得通过小奇的文章学到了东西,那就给小奇一个赞吧 文章持续更新 一.前言 书接上回,昨天下雨没怎么上街上 ...
- css设置元素背景透明度的2种方式
更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月9日. 设置元素的背景的透明度可以使用2种方式:方式1:opacity属性.方式2:使用rgba值.两种方式有一点差异,opaci ...