| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 9375 人关注过本帖, 1 人收藏
标题:[原创]我写的一个控制台下的扫雷小游戏源代码
只看楼主 加入收藏
雨中飛燕
Rank: 1
等 级:新手上路
帖 子:765
专家分:0
注 册:2007-10-13
收藏(1)
 问题点数:0 回复次数:42 
[原创]我写的一个控制台下的扫雷小游戏源代码
/********************************************************
** Highlight software by yzfy(雨中飞燕) http:// *
*********************************************************/
//扫雷.cpp : Win32 Console Application
//Compiler: MinGW(GCC), VC6.0, VC2003, VC2005
//Warning: You Can Not Using TC To Compile this file
//Demo Game By yzfy(雨中飞燕) http://
//I'm too lazy to write comments


// header file
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <conio.h>

// defines
#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

// Strings Resource
#define STR_GAMETITLE "ArrowKey:MoveCursor  Key1:Open  \
Key2:Mark  Key3:OpenNeighbors"
#define STR_GAMEWIN   "Congratulations! You Win! Thank you for playing!\n"
#define STR_GAMEOVER  "Game Over, thank you for playing!\n"
#define STR_GAMEEND   "Presented by yzfy . Press ESC to exit\n"

//-------------------------------------------------------------
// Base class
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:
};

//{{// class CConsoleWnd
    //
    //  int CConsoleWnd::GetKey()
    //  Wait for standard input and return the KeyCode
    //
    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)
    //  Move cursor to (x,y)
    //  Only Console Application
    //
    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)
    //  Output a string at current position
    //
    int CConsoleWnd::TextOut(const char* pstr)
    {
        for(;*pstr;++pstr)putchar(*pstr);
        return 0;
    }

    //
    //  int CConsoleWnd::CharOut(int x, int y, const int pstr)
    //  Output a char at (x,y)
    //
    int CConsoleWnd::CharOut(int x, int y, const int pstr)
    {
        GotoXY(x, y);
        return putchar(pstr);
    }

    //
    //  int CConsoleWnd::TextOut(const char* pstr)
    //  Output a string at (x,y)
    //
    int CConsoleWnd::TextOut(int x, int y, const char* pstr)
    {
        GotoXY(x, y);
        return TextOut(pstr);
    }
//}}


//-------------------------------------------------------------
//Application class
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;

//{{// class CSLGame:public CConsoleWnd
    //
    //  int CSLGame::InitPool(int Width, int Height, int nBoom)
    //  Initialize the game pool.
    //  If Width*Height <= nBoom, or nBoom<=0,
    //  or Width and Height exceed limit , then return 1
    //  otherwise return 0
    //
    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;
        }
        // zero memory
        for(int y=0; y<=Height+1; ++y)
        {
            for(int x=0; x<=Width+1; ++x)
            {
                bm_gamepool[y][x]=0;
            }
        }
        // init seed
        srand(time(NULL));
        // init Booms
        while(nBoom)
        {
            int x = rand()%Width + 1, y = rand()%Height + 1;
            if(bm_gamepool[y][x]==0)
            {
                bm_gamepool[y][x] = GMARK_BOOM;
                --nBoom;
            }
        }
        // init cursor position
        curX = curY = 1;
        MoveCursor();
        return 0;
    }

    //
    //  int CSLGame::DrawPool(int bDrawBoom = 0)
    //  Draw game pool to Console window
    //
    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)
    //  return ShowNum at (x, y)
    //
    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)
    //  Try open (x, y) and show the number
    //  If there is a boom, then return EOF
    //
    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)
    //  Private function, no comment
    //
    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()
    //  Game loop, wait and process an input message
    //  return:  0: not end;  1: Win; otherwise: Lose
    //
    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;
    }
//}}


//-------------------------------------------------------------
//{{
    //
    //  main function
    //
    int main(void)
    {
        int x=50, y=20, b=100,n; // define width & height & n_booms
        CSLGame slGame;
        // Init Game
        {
            CConsoleWnd::GotoXY(0,0);
            CConsoleWnd::TextOut(STR_GAMETITLE);
            slGame.InitPool(x,y,b);
            slGame.DrawPool();
            slGame.MoveCursor();
        }
        while((n=slGame.WaitMessage())==0) // Game Message Loop
            ;
        // End of the Game
        {
            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;
    }
//}}


用mingw或者VC/VS在控制台下编译一下玩玩看(不能用TC/WinTC编译,文件名后缀必须为cpp)。
方向键移动光标,1键是翻开当前格,2键是标记,3键是自动翻开周围8格
偶以前写的一个小东东。。。不想用太多API。。。本意不是玩API。。。
不过因为帖子为连续文本的原因,代码只好用单文件的形式了。
不知道你看完的感觉是什么?给个意见吧

[color=white]
搜索更多相关主题的帖子: 源代码 小游戏 扫雷 
2008-05-08 18:57
wltysy
Rank: 1
等 级:新手上路
帖 子:560
专家分:0
注 册:2008-5-2
收藏
得分:0 
不懂,顶下!苦啃编程ing·····
2008-05-08 19:01
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
抢沙发...

学习需要安静。。海盗要重新来过。。
2008-05-08 19:03
菜鸟选手
Rank: 1
等 级:新手上路
帖 子:132
专家分:0
注 册:2008-5-5
收藏
得分:0 
建议你多发点你写的码 .
学习下 .
2008-05-08 19:45
cdmalcl
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:4091
专家分:524
注 册:2005-9-23
收藏
得分:0 
有意思 顶一下 呵呵
2008-05-08 20:09
fzy667
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-12-29
收藏
得分:0 
我运行了,怎么有编译错误啊
2008-05-08 20:24
cdmalcl
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:4091
专家分:524
注 册:2005-9-23
收藏
得分:0 
你用什么编译的?
如果VC6的话可能因为你的库太老出现:
Cannot open include file: 'streambuf': No such file or directory

这样的话你可以把头文件改为:
#include <iostream.h>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
2008-05-08 20:25
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
发现自己学会敬佩别人了……
有些人,是需要敬佩的……
收到的鲜花
  • liyanhong2008-05-08 21:02 送鲜花  3朵   附言:不是从她开始吧?

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-05-08 20:43
★红狼
Rank: 2
等 级:论坛游民
帖 子:190
专家分:17
注 册:2006-7-12
收藏
得分:0 
看完 发现,我还有很多看不懂的.
原来 你就是传说中 高手啊.
原来 我还差你很远,虽然我也能编一些.
哎!!我什么时候,也能用自己的思想.编个你一样的..
2008-05-08 20:50
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
敬佩不是因为自己做不到,也不是看不懂,而是被成果中凝结的心血和思想而感动。

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-05-08 21:56
快速回复:[原创]我写的一个控制台下的扫雷小游戏源代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023241 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved