扫雷 c++ 源代码

#c版扫雷#

这是c++扫雷

不多说 直接上代码

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

如上

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
很抱歉,我无法提供百度云链接,但我可以提供一个Java扫雷游戏的源代码,您可以在自己的电脑上使用它。 ```java import java.util.Random; import java.util.Scanner; public class Minesweeper { private static final char HIDDEN_CELL = '-'; private static final char MINE_CELL = '*'; private static final char MARKED_CELL = '!'; private static final char[] NUMBERED_CELLS = {'0', '1', '2', '3', '4', '5', '6', '7', '8'}; private static final int[] ROW_NEIGHBORS = {-1, -1, -1, 0, 0, 1, 1, 1}; private static final int[] COL_NEIGHBORS = {-1, 0, 1, -1, 1, -1, 0, 1}; private int numRows; private int numCols; private int numMines; private char[][] board; private boolean[][] revealed; private boolean[][] marked; private Minesweeper(int numRows, int numCols, int numMines) { this.numRows = numRows; this.numCols = numCols; this.numMines = numMines; board = new char[numRows][numCols]; revealed = new boolean[numRows][numCols]; marked = new boolean[numRows][numCols]; initializeBoard(); } private void initializeBoard() { for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { board[i][j] = HIDDEN_CELL; } } placeMines(); } private void placeMines() { Random random = new Random(); int count = 0; while (count < numMines) { int row = random.nextInt(numRows); int col = random.nextInt(numCols); if (board[row][col] != MINE_CELL) { board[row][col] = MINE_CELL; count++; } } } private boolean isValid(int row, int col) { return row >= 0 && row < numRows && col >= 0 && col < numCols; } private int countNeighborMines(int row, int col) { int count = 0; for (int i = 0; i < ROW_NEIGHBORS.length; i++) { int r = row + ROW_NEIGHBORS[i]; int c = col + COL_NEIGHBORS[i]; if (isValid(r, c) && board[r][c] == MINE_CELL) { count++; } } return count; } private void revealAllMines() { for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { if (board[i][j] == MINE_CELL) { revealed[i][j] = true; } } } } private void revealEmptyCells(int row, int col) { if (!isValid(row, col) || revealed[row][col] || marked[row][col]) { return; } revealed[row][col] = true; if (board[row][col] != HIDDEN_CELL) { return; } int neighborMines = countNeighborMines(row, col); if (neighborMines > 0) { board[row][col] = NUMBERED_CELLS[neighborMines]; return; } board[row][col] = NUMBERED_CELLS[neighborMines]; for (int i = 0; i < ROW_NEIGHBORS.length; i++) { int r = row + ROW_NEIGHBORS[i]; int c = col + COL_NEIGHBORS[i]; revealEmptyCells(r, c); } } private boolean isGameWon() { for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { if (!revealed[i][j] && board[i][j] != MINE_CELL) { return false; } } } return true; } private void printBoard(boolean showMines) { System.out.print(" "); for (int j = 0; j < numCols; j++) { System.out.print(j + " "); } System.out.println(); System.out.print(" "); for (int j = 0; j < numCols; j++) { System.out.print("--"); } System.out.println(); for (int i = 0; i < numRows; i++) { System.out.printf("%2d| ", i); for (int j = 0; j < numCols; j++) { if (showMines && board[i][j] == MINE_CELL) { System.out.print(MINE_CELL + " "); } else if (marked[i][j]) { System.out.print(MARKED_CELL + " "); } else if (revealed[i][j]) { System.out.print(board[i][j] + " "); } else { System.out.print(HIDDEN_CELL + " "); } } System.out.println(); } } private void play() { Scanner scanner = new Scanner(System.in); while (true) { printBoard(false); System.out.print("Enter row and column: "); int row = scanner.nextInt(); int col = scanner.nextInt(); if (!isValid(row, col)) { System.out.println("Invalid row or column!"); continue; } if (board[row][col] == MINE_CELL) { System.out.println("Game over!"); revealAllMines(); printBoard(true); break; } else if (countNeighborMines(row, col) > 0) { board[row][col] = NUMBERED_CELLS[countNeighborMines(row, col)]; revealed[row][col] = true; } else { revealEmptyCells(row, col); } if (isGameWon()) { System.out.println("Congratulations! You won the game!"); break; } } scanner.close(); } public static void main(String[] args) { Minesweeper game = new Minesweeper(8, 8, 10); game.play(); } } ``` 这个代码实现了一个基本的扫雷游戏,可以在控制台中运行。您可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值