扫雷程序-地雷位置生成

//过会儿再编辑得简洁点。

#include<stdio.h>    
#include<stdlib.h>    
#include<time.h>    
    
#define nMine   10          // Number of mines  
#define nRow    10  
#define nCol    8  
  
  
// "Mines" or "Not Mines" are marked by numbers.  
// 9 for mines,  
// and other numbers from 0 to 8 stands ...  
// for the number of the 8 surrownding locations.  
int main(void)    
{    
    
    int MineLoc1D[nMine]={0};  
    int MineLoc2D[nRow+2][nCol+2]={0};  
      
    srand(time(NULL));  
    for(int p=0;p<nMine;p++)  
    {  
        MineLoc1D[p]=rand()%(nRow*nCol);        // 1D  

		// Avoid overlap
		for(int t=0;t<p;t++)
		{
			if(MineLoc1D[p]==MineLoc1D[t])
			{
				p--;
			}
以下是一个简单的C++扫雷程序的完整代码: ```cpp #include <iostream> #include <cstdlib> #include <ctime> #include <vector> #include <string> using namespace std; // 定义扫雷方格的状态 enum SquareState { UNREVEALED, // 未翻开 REVEALED, // 已翻开 FLAGGED // 已标记为地雷 }; // 定义扫雷方格 struct Square { bool is_mine; // 是否是地雷 SquareState state; // 方格状态 int neighboring_mines; // 相邻的地雷数量 }; // 定义扫雷游戏 class Minesweeper { private: int rows; // 行数 int cols; // 列数 int mines; // 地雷数量 vector<vector<Square>> board; // 扫雷棋盘 bool game_over; // 游戏结束标志 // 随机生成地雷位置 void generate_mines() { int count = 0; while (count < mines) { int row = rand() % rows; int col = rand() % cols; if (!board[row][col].is_mine) { board[row][col].is_mine = true; count++; } } } // 计算每个方格周围的地雷数量 void count_neighboring_mines() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (!board[i][j].is_mine) { int count = 0; for (int k = i - 1; k <= i + 1; k++) { for (int l = j - 1; l <= j + 1; l++) { if (k >= 0 && k < rows && l >= 0 && l < cols && board[k][l].is_mine) { count++; } } } board[i][j].neighboring_mines = count; } } } } // 翻开指定方格 void reveal_square(int row, int col) { if (board[row][col].state == UNREVEALED) { board[row][col].state = REVEALED; if (board[row][col].is_mine) { game_over = true; } else if (board[row][col].neighboring_mines == 0) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < rows && j >= 0 && j < cols && !(i == row && j == col)) { reveal_square(i, j); } } } } } } public: // 构造函数 Minesweeper(int r, int c, int m) : rows(r), cols(c), mines(m), game_over(false) { // 初始化棋盘 board.resize(rows); for (int i = 0; i < rows; i++) { board[i].resize(cols); for (int j = 0; j < cols; j++) { board[i][j].is_mine = false; board[i][j].state = UNREVEALED; board[i][j].neighboring_mines = 0; } } // 随机生成地雷位置 srand(time(NULL)); generate_mines(); // 计算每个方格周围的地雷数量 count_neighboring_mines(); } // 显示棋盘 void show_board() { cout << " "; for (int j = 0; j < cols; j++) { cout << j << " "; } cout << endl; cout << " "; for (int j = 0; j < cols; j++) { cout << "--"; } cout << endl; for (int i = 0; i < rows; i++) { cout << i << " | "; for (int j = 0; j < cols; j++) { if (game_over && board[i][j].is_mine) { cout << "* "; } else if (board[i][j].state == UNREVEALED) { cout << ". "; } else if (board[i][j].state == FLAGGED) { cout << "F "; } else { cout << board[i][j].neighboring_mines << " "; } } cout << endl; } } // 翻开指定方格 void play(int row, int col) { if (game_over) { cout << "Game over." << endl; return; } if (board[row][col].state == REVEALED) { cout << "This square has already been revealed." << endl; return; } if (board[row][col].state == FLAGGED) { cout << "This square has been flagged." << endl; return; } reveal_square(row, col); if (game_over) { cout << "Game over." << endl; } } // 标记指定方格为地雷 void flag(int row, int col) { if (game_over) { cout << "Game over." << endl; return; } if (board[row][col].state == REVEALED) { cout << "This square has already been revealed." << endl; return; } if (board[row][col].state == FLAGGED) { board[row][col].state = UNREVEALED; } else { board[row][col].state = FLAGGED; } } // 判断游戏是否已结束 bool is_game_over() { return game_over; } }; // 主函数 int main() { int rows, cols, mines; cout << "Enter the number of rows: "; cin >> rows; cout << "Enter the number of columns: "; cin >> cols; cout << "Enter the number of mines: "; cin >> mines; Minesweeper game(rows, cols, mines); game.show_board(); while (!game.is_game_over()) { string action; int row, col; cout << "Enter an action (play/flag): "; cin >> action; cout << "Enter the row and column numbers: "; cin >> row >> col; if (action == "play") { game.play(row, col); } else if (action == "flag") { game.flag(row, col); } game.show_board(); } return 0; } ``` 这个程序实现了以下功能: 1. 随机生成地雷位置; 2. 计算每个方格周围的地雷数量; 3. 翻开指定方格; 4. 标记指定方格为地雷; 5. 显示扫雷棋盘; 6. 判断游戏是否已结束。 可以通过在命令行中输入行数、列数和地雷数量来启动游戏,并通过输入“play”或“flag”来进行操作。在游戏过程中,程序会实时更新棋盘状态并提示游戏是否已结束。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值