WELCOME

任何一个伟大的目标,都有一个微不足道的开始。

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; }


__EOF__

本文作者绿树公司
本文链接https://www.cnblogs.com/wp-lvshu/p/16471698.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   绿树公司  阅读(1716)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示