扫雷代码(C++)
- 格式:doc
- 大小:10.45 KB
- 文档页数:13
C语⾔实现扫雷附完整代码⽬录⼀、理清逻辑⼆、创建⽂件三、具体步骤1.打印菜单2.创建⼆维数组3.初始化⼆维数组并打印棋盘4.布置雷5.排查雷(内含判断胜负)四、完整代码五、待改进⼀、理清逻辑我们先来看⼀下实现扫雷的基本逻辑1.打印游戏菜单2.创建并初始化⼆维数组3.布置雷4.进⾏排雷⼆、创建⽂件我创建了三个⽂件,分别为test.c、game.h和game.ctest.c⽂件⽤于实现进⼊游戏、退出游戏、判断输赢、打印菜单等逻辑game.c⽤于编写游戏的主要实现⽅法game.h存放头⽂件和函数的声明三、具体步骤1.打印菜单void menu(){printf("**********************************\n");printf("*********** 1.start **********\n");printf("*********** 0. exit **********\n");printf("**********************************\n");}void test(){int input = 0;srand((unsigned int)time(NULL));do{menu();printf("请输⼊:>");scanf("%d", &input);switch (input){case 1:printf("游戏开始\n");game();break;case 0:printf("游戏结束\n");break;default :printf("⾮法输⼊,请重新输⼊\n");break;}} while (input);}2.创建⼆维数组因为我们想实现⼀个9*9的扫雷,所以考虑到数组越界的问题,我们要创建⼀个11*11的⼆维数组,需要同时去创建两个⼆维数组,mine数组⽤来查看雷的位置和show数组来展⽰排雷过程。
扫雷小游戏(C开发环境使用Unity引擎开发)扫雷(Minesweeper)是一款经典的单人益智游戏,旨在通过揭开区域中的方块,避免踩中地雷并推断出地雷的位置。
本文将介绍扫雷小游戏的开发过程,使用C开发环境和Unity引擎进行实现。
第一步:项目准备在开始开发之前,需要准备好所需的开发工具和资源。
首先,下载并安装C开发环境和Unity引擎。
确保你已经熟悉这些工具的基本使用方法,并熟悉C语言编程。
第二步:项目设置在Unity中创建一个新项目,并设置好项目的名称和保存路径。
接下来,创建一个新的场景,并将场景设置为游戏的主场景。
同时,将摄像机设置为适当的视角来显示游戏界面。
第三步:创建地图扫雷游戏的核心是一个方块地图,其中包含一些地雷和数字。
在Unity中,可以创建一个正方形的网格来代表地图。
可以使用脚本来随机放置地雷,并计算每个方块周围的地雷数量。
第四步:游戏逻辑编写C语言脚本来实现游戏的逻辑。
首先,需要处理玩家点击方块的事件。
如果玩家点击到地雷方块,游戏失败,显示失败界面。
否则,根据点击到的方块周围的地雷数量显示对应的数字。
若玩家点击到数字为0的方块,则自动揭开周围的方块。
当所有非地雷方块都被揭开时,游戏成功,显示成功界面。
第五步:用户界面设计并创建游戏的用户界面。
包括游戏开始界面、失败界面、成功界面以及游戏进行中的界面。
在界面上显示剩余地雷数量和游戏计时器。
第六步:音效和动画通过添加音效和动画来增强游戏的交互性和趣味性。
例如,当玩家点击到地雷时,播放爆炸声音和特效动画。
第七步:游戏测试和调试在完成游戏开发后,进行测试和调试,确保游戏的各项功能都能正常运行。
根据测试结果修复代码中的bug和错误,以确保游戏的稳定性和流畅性。
第八步:发布游戏当游戏开发和测试都完成后,可以将游戏发布到目标平台上,供玩家下载和游玩。
在发布过程中,确保提供适当的游戏介绍和说明,以便玩家了解游戏规则和操作方法。
通过以上步骤,可以使用C开发环境和Unity引擎成功开发一个扫雷小游戏。
用C和SFML编程实现扫雷小游戏标题:用C和SFML编程实现扫雷小游戏开发小游戏是程序员们锻炼技能、提高编程能力的常见方式之一。
在本文中,我将介绍如何使用C语言和SFML库来编程实现扫雷游戏。
扫雷游戏是一款经典的单人益智游戏,目标是在没有踩雷的情况下揭开所有的方块。
## 准备工作在开始编写代码之前,我们首先需要准备以下几个工具和资源:1. C编译器:我们可以选择GCC、Clang等常见的C编译器。
2. SFML库:SFML是一个跨平台的多媒体库,它提供了图形渲染、输入处理、音频播放等功能,非常适合游戏开发。
3. 开发环境:为了方便代码的编写和调试,我们可以选择使用集成开发环境(IDE)如Code::Blocks、Visual Studio等。
安装好以上工具后,我们就可以开始编写代码了。
## 游戏界面设计在扫雷游戏中,我们需要一个游戏界面来展示方块的状态、玩家的操作等信息。
下面是一个简单的游戏界面设计示例:```+-----+-----+-----+| | | |+-----+-----+-----+| | | |+-----+-----+-----+| | | |+-----+-----+-----+```在代码中,我们可以使用二维数组来存储每个方块的状态。
对于每个方块,我们可以使用0表示未揭开,1表示揭开,2表示标记为雷。
## 游戏逻辑实现接下来,我们需要实现游戏的逻辑。
主要包括以下几个功能:1. 初始化游戏界面:在开始游戏时,需要将所有方块的状态设置为未揭开。
2. 随机布雷:根据游戏难度,我们可以决定雷的数量,并将雷随机分布在游戏界面中。
3. 揭开方块:当玩家点击一个方块时,我们需要判断该方块是否为雷。
如果是雷,则游戏结束;如果不是雷,则根据周围雷的数量进行相应的处理。
4. 标记方块:玩家可以标记某个方块为雷,该方块状态变为标记状态,玩家需要正确标记出所有雷才能获胜。
5. 判断游戏结束:每次揭开方块或者标记方块后,需要判断游戏是否结束。
C语⾔实现经典扫雷游戏流程⽬录扫雷⼩游戏简介⼀、分析与实现1.设计棋盘2.放置雷以及排雷⼆、扫雷⼩游戏演⽰三、源码总结扫雷⼩游戏简介想必很多⼈⼩时候电脑没⽹的时候都玩⼉过这个经典的⼩游戏,也都被它折磨过。
其实这个游戏很简单,通过点击相应位置显⽰的数字来确定周围雷的数量,在避免踩到雷的同时找出所有的雷就能获得胜利。
这次我们⽤C语⾔来实现⼀个简单的扫雷⼩游戏。
⼀、分析与实现1.设计棋盘要玩⼉扫雷游戏,我们⾸先应该有⼀个棋盘。
这个棋盘中的雷应该是在开始玩⼉游戏的时候就已经布置好了,不能随意变化。
但是呢⼜不能给玩家看到雷的位置,所以呢,我们应该有两个棋盘,⼀个显⽰给玩家,⼀个给⽤来给设计者查看。
有了棋盘之后⾸先要进⾏初始化://初始化棋盘void InitChess(char chess[ROWS][COLS], int rows, int cols, char sign){int i = 0;for (i = 0; i < rows; i++){int j = 0;for (j = 0; j < cols; j++){chess[i][j] = sign;}}printf("初始化棋盘成功!\n");}之后呢我们可以将设计好的棋盘打印出来看⼀看是否符合⼼意://打印棋盘void DisplayChess(char chess[ROWS][COLS], int row, int col){int i = 0;printf(" ");for (i = 1; i <= row; i++){printf(" %d ", i);}printf("\n");for (i = 1; i <= row; i++){int j = 0;printf(" ");for (j = 1; j <= col; j++){printf("+---");}printf("+\n");printf(" %d ", i);for (j = 1; j <= col; j++){printf("| %c ", chess[i][j]);}printf("|\n");}int j = 0;printf(" ");for (j = 1; j <= col; j++){printf("+---");}printf("+\n");}这是设计的⼀个简易的9X9的⼩棋盘,*号代表这个位置还没有被探查过,⼤家可以根据⾃⼰的喜好更改棋盘⼤⼩。
扫雷游戏代码standalone; self-contained; independent; self-governed;autocephalous; indie; absolute; unattached; substantive/**/#ifndef BLOCK_H_#define BLOCK_H_#include<QLabel>class QWidget;class Block:public QLabel{Q_OBJECTpublic:explicit Block(bool mine_flag,QWidget*parent=0);void set_number(int number);void turn_over();bool is_mine()const;bool is_turn_over()const;signals:void turn_over(bool is_mine);protected:void mousePressEvent(QMouseEvent*event); private:bool mine_flag_;bool mark_flag_;bool turn_over_flag_;int number_;};#endif#include""#include<QLabel>#include<QMouseEvent>#include<QPixmap>#include<QWidget>Block::Block(bool mine_flag,QWidget*parent) :QLabel(parent){mine_flag_=mine_flag;mark_flag_=false;turn_over_flag_=false;number_=-1;setPixmap(QPixmap(":/images/"));}void Block::set_number(int number){number_=number;}void Block::turn_over(){if(!turn_over_flag_){turn_over_flag_=true;if(mine_flag_)setPixmap(QPixmap(":/images/"));elsesetPixmap(QPixmap(":/images/mine_"+QString("%1").arg(num ber_)+".png"));update();}}bool Block::is_mine()const{return mine_flag_;}bool Block::is_turn_over()const{return turn_over_flag_;}/*鼠标事件的实现*/void Block::mousePressEvent(QMouseEvent*event){if(event->button()==Qt::LeftButton){if(!turn_over_flag_&&!mark_flag_){turn_over_flag_=true;if(mine_flag_==true){setPixmap(QPixmap(":/images/"));update();emit turn_over(true);}else{setPixmap(QPixmap(":/images/mine_"+QString("%1").arg(num ber_)+".png"));update();emit turn_over(false);}}}else if(event->button()==Qt::RightButton){if(!turn_over_flag_){if(!mark_flag_){mark_flag_=true;setPixmap(QPixmap(":/images/"));}else{mark_flag_=false;setPixmap(QPixmap(":/images/"));}update();}}QLabel::mousePressEvent(event);}#ifndef BLOCK_AREA_H_#define BLOCK_AREA_H_#include""#include<QWidget>class QEvent;class QGridLayout;class QObject;class BlockArea:public QWidget{Q_OBJECTpublic:BlockArea(int row,int column,int mine_number,QWidget* parent=0);void set_block_area(int row,int column,intmine_number,int init_flag=false);signals:void game_over(bool is_win);protected:bool eventFilter(QObject*watched,QEvent*event); private slots:void slot_turn_over(bool is_mine);private:int calculate_mines(int x,int y)const;rg(easy_record_time_)),1,1);up_layout->addWidget(new QLabel(easy_record_name_),1,2);up_layout->addWidget(new QLabel(tr("Middle")),2,0);up_layout->addWidget(newQLabel(QString("%1").arg(middle_record_time_)),2,1);up_layout->addWidget(newQLabel(middle_record_name_),2,2);up_layout->addWidget(new QLabel(tr("Hard")),3,0);up_layout->addWidget(newQLabel(QString("%1").arg(hard_record_time_)),3,1);up_layout->addWidget(new QLabel(hard_record_name_),3,2);QPushButton*recount_button=newQPushButton(tr("recount"));QPushButton*close_button=new QPushButton(tr("close"));close_button->setDefault(true);connect(recount_button,SIGNAL(clicked()),&dialog,SLOT(ac cept()));connect(close_button,SIGNAL(clicked()),&dialog,SLOT(reje ct()));QHBoxLayout*bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(recount_button);bottom_layout->addWidget(close_button);QVBoxLayout*main_layout=new QVBoxLayout(&dialog);main_layout->addLayout(up_layout);main_layout->addLayout(bottom_layout);if()==QDialog::Accepted){easy_record_time_=middle_record_time_=hard_record_time_= g_no_record_time;easy_record_name_=middle_record_name_=hard_record_name_= g_no_record_name;}}void MainWindow::slot_show_game_toolBar(bool show){if(show)game_toolBar->show();elsegame_toolBar->hide();}void MainWindow::slot_show_statusBar(bool show){if(show)statusBar()->show();elsestatusBar()->hide();}/*游戏的设置容易、中等、困难及自定义*/void MainWindow::slot_standard(QAction*standard_action) {if(standard_action==easy_standard_action){current_standard_=0;row_=9;column_=9;mine_number_=10;}else if(standard_action==middle_standard_action){ current_standard_=1;row_=16;column_=16;mine_number_=40;}else if(standard_action==hard_standard_action){current_standard_=2;row_=16;column_=30;mine_number_=99;}else if(standard_action==custom_standard_action){ QDialog dialog;(tr("set standard"));QSpinBox*row_spinBox=new QSpinBox;row_spinBox->setRange(5,50);row_spinBox->setValue(row_);QSpinBox*column_spinBox=new QSpinBox;column_spinBox->setRange(5,50);column_spinBox->setValue(column_);QSpinBox*mine_spinBox=new QSpinBox;mine_spinBox->setValue(mine_number_);QHBoxLayout*up_layout=new QHBoxLayout;up_layout->addWidget(row_spinBox);up_layout->addWidget(column_spinBox);up_layout->addWidget(mine_spinBox);QDialogButtonBox*dialog_buttonBox=new QDialogButtonBox;dialog_buttonBox->addButton(QDialogButtonBox::Ok);dialog_buttonBox->addButton(QDialogButtonBox::Cancel);connect(dialog_buttonBox,SIGNAL(accepted()),&dialog,SLOT (accept()));connect(dialog_buttonBox,SIGNAL(rejected()),&dialog,SLOT (reject()));QHBoxLayout*bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(dialog_buttonBox);QVBoxLayout*main_layout=new QVBoxLayout(&dialog);main_layout->addLayout(up_layout);main_layout->addLayout(bottom_layout);if()==QDialog::Accepted)if(row_spinBox->value()*column_spinBox->value()>mine_spinBox->value()){current_standard_=3;row_=row_spinBox->value();column_=column_spinBox->value();mine_number_=mine_spinBox->value();}}slot_new_game();}/*实现帮助菜单中的关于游戏,及功能*/void MainWindow::slot_about_game(){QString introduction("<h2>"+tr("About Mine Sweepr")+"</h2>"+"<p>"+tr("This game is played by revealing squares of the grid,typically by clicking them with a mouse.If a square containing a mine is revealed,the player loses the game.Otherwise,a digit is revealed in the square,indicating the number of adjacent squares(out of the possible eight)that contain this number is zero then the square appears blank,and the surrounding squares are automatically also revealed.By using logic,the player can in many instances use this information to deduce that certain other squares are mine-free, in which case they may be safely revealed,or mine-filled,in which they can be marked as such(which is effected by right-clicking the square and indicated by a flag graphic).")+"</p>"+"<p>"+tr("This program is free software;you can redistribute it and/or under the terms of the GNU General Public License as published by the Software Foundation;either version3of the License,or(at your option)any later version.")+"</p>"+"<p>"+tr("Please see")+"<a href="+tr("for an overview of GPLv3licensing")+"</p>"+"<br>"+tr("Version:")+g_software_version+"</br>"+"<br>"+tr("Author:")+g_software_author+"</br>");QMessageBoxmessageBox(QMessageBox::Information,tr("About Mine Sweeper"),introduction,QMessageBox::Ok);();}/*游戏的判断,及所给出的提示做出判断*/void MainWindow::slot_game_over(bool is_win){();QString name;if(is_win){switch(current_standard_){case0:if(time_label->text().toInt()<easy_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){easy_record_time_=time_label->text().toInt();easy_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;case1:if(time_label->text().toInt()<middle_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){middle_record_time_=time_label->text().toInt();middle_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;case2:if(time_label->text().toInt()<hard_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){hard_record_time_=time_label->text().toInt();hard_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;default:QMessageBox::information(this,tr("Result"),tr("Y ou win"));}}else{QMessageBox::information(this,tr("Result"),tr("You lose"));}}/*定时器的设置*/void MainWindow::slot_timer(){time_label->setText(QString("%1").arg()/1000));}/*关于菜单栏的设置是否显示游戏工具栏和状态栏*/void MainWindow::read_settings(){QSettings settings;("MainWindow");resize("size").toSize());move("pos").toPoint());bool show_game_toolBar=("showGameToolBar").toBool();show_game_toolBar_action->setChecked(show_game_toolBar);slot_show_game_toolBar(show_game_toolBar);bool show_statusBar=("showStatusBar").toBool();show_statusBar_action->setChecked(show_statusBar);slot_show_statusBar(show_statusBar);();("GameSetting");current_standard_=("current_standard").toInt();switch(current_standard_){case0:easy_standard_action->setChecked(true);break;case1:middle_standard_action->setChecked(true);break;case2:hard_standard_action->setChecked(true);break;case3:custom_standard_action->setChecked(true);break;default:;}row_=("row").toInt()==09:("row").toInt();column_=("column").toInt()==09:("column").toInt();mine_number_=("mine_number").toInt()==010:("mine_number" ).toInt();();("Rank");easy_record_time_=("easy_record_time").toInt()==0g_no_re cord_time:("easy_record_time").toInt();middle_record_time_=("middle_record_time").toInt()==0g_n o_record_time:("middle_record_time").toInt();hard_record_time_=("hard_record_time").toInt()==0g_no_re cord_time:("hard_record_time").toInt();easy_record_name_=("easy_record_name").toString()==""g_n o_record_name:("easy_record_name").toString();middle_record_name_=("middle_record_name").toString()==" "g_no_record_name:("middle_record_name").toString();hard_record_name_=("hard_record_name").toString()==""g_n o_record_name:("hard_record_name").toString();();}void MainWindow::write_settings(){QSettings settings;("MainWindow");("size",size());("pos",pos());("showGameToolBar",show_game_toolBar_action->isChecked());("showStatusBar",show_statusBar_action->isChecked());();("GameSetting");("current_standard",current_standard_);("row",row_);("column",column_);("mine_number",mine_number_);();("Rank");("easy_record_time",easy_record_time_);("middle_record_time",middle_record_time_);("hard_record_time",hard_record_time_);("easy_record_name",easy_record_name_);("middle_record_name",middle_record_name_);("hard_record_name",hard_record_name_);();}/*菜单栏里图片的显示*/void MainWindow::create_actions(){new_game_action=new QAction(QIcon(":/images/"),tr("New Game"),this);new_game_action->setShortcut(QKeySequence::New);connect(new_game_action,SIGNAL(triggered()),this,SLOT(sl ot_new_game()));rank_action=newQAction(QIcon(":/images/"),tr("Rank"),this);connect(rank_action,SIGNAL(triggered()),this,SLOT(slot_r ank()));exit_action=newQAction(QIcon(":/images/"),tr("Exit"),this);exit_action->setShortcut(QKeySequence::Quit);connect(exit_action,SIGNAL(triggered()),this,SLOT(close( )));show_game_toolBar_action=new QAction(tr("Show Game Tool Bar"),this);show_game_toolBar_action->setCheckable(true);connect(show_game_toolBar_action,SIGNAL(toggled(bool)),t his,SLOT(slot_show_game_toolBar(bool)));show_statusBar_action=new QAction(tr("Show Status Bar"),this);show_statusBar_action->setCheckable(true);connect(show_statusBar_action,SIGNAL(toggled(bool)),this ,SLOT(slot_show_statusBar(bool)));easy_standard_action=newQAction(QIcon(":/images/"),tr("Easy"),this);easy_standard_action->setCheckable(true);middle_standard_action=newQAction(QIcon(":/images/"),tr("Middle"),this);middle_standard_action->setCheckable(true);hard_standard_action=newQAction(QIcon(":/images/"),tr("Hard"),this);hard_standard_action->setCheckable(true);custom_standard_action=newQAction(QIcon(":/images/"),tr("Custom"),this);custom_standard_action->setCheckable(true);standard_actionGroup=new QActionGroup(this);standard_actionGroup->addAction(easy_standard_action);standard_actionGroup->addAction(middle_standard_action);standard_actionGroup->addAction(hard_standard_action);standard_actionGroup->addAction(custom_standard_action);connect(standard_actionGroup,SIGNAL(triggered(QAction*)) ,this,SLOT(slot_standard(QAction*)));about_game_action=newQAction(QIcon(":/images/"),tr("About Game"),this);connect(about_game_action,SIGNAL(triggered()),this,SLOT( slot_about_game()));about_qt_action=new QAction(QIcon(":/images/"),tr("About Qt"),this);connect(about_qt_action,SIGNAL(triggered()),qApp,SLOT(ab outQt()));}/*菜单栏的创建*/void MainWindow::create_menus(){game_menu=menuBar()->addMenu(tr("Game"));game_menu->addAction(new_game_action);game_menu->addSeparator();game_menu->addAction(rank_action);game_menu->addSeparator();game_menu->addAction(exit_action);setting_menu=menuBar()->addMenu(tr("Setting"));setting_menu->addAction(show_game_toolBar_action);setting_menu->addAction(show_statusBar_action);setting_menu->addSeparator();setting_menu->addAction(easy_standard_action);setting_menu->addAction(middle_standard_action);setting_menu->addAction(hard_standard_action);setting_menu->addAction(custom_standard_action);help_menu=menuBar()->addMenu(tr("Help"));help_menu->addAction(about_game_action);help_menu->addAction(about_qt_action);}void MainWindow::create_game_toolBar(){game_toolBar=addToolBar(tr("Game Tool Bar"));game_toolBar->setFloatable(false);game_toolBar->setMovable(false);game_toolBar->addAction(new_game_action);game_toolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); }void MainWindow::create_statusBar(){time_label=new QLabel;statusBar()->addPermanentWidget(time_label);statusBar()->addPermanentWidget(newQLabel(tr("second")));}#include""#include<QApplication>#include<QTranslator>int main(int argc,char*argv[]){QApplication app(argc,argv);QTranslator translator;(":/");(&translator);MainWindow window;();return();}。
windows10扫雷c语言最简单代码以下是一个使用C语言编写的简单的Windows10扫雷游戏代码示例。
请注意,这只是一个基本示例,实际的扫雷游戏需要更多功能和图形界面的开发。
```c#include<stdio.h>#include<stdlib.h>#include<time.h>#define ROWS8#define COLS8#define MINES10int board[ROWS][COLS];int revealed[ROWS][COLS];void initializeBoard(){int i,j;for(i=0;i<ROWS;i++){for(j=0;j<COLS;j++){board[i][j]=0;revealed[i][j]=0;}}}void placeMines(){int i,j,count=0;while(count<MINES){i=rand()%ROWS;j=rand()%COLS;if(board[i][j]!=-1){board[i][j]=-1;count++;}}}void printBoard(){int i,j;for(i=0;i<ROWS;i++){for(j=0;j<COLS;j++){if(revealed[i][j]==0){printf(".");}else if(board[i][j]==-1){printf("*");}else{printf("%d",board[i][j]);}}printf("\n");}}int main(){srand(time(0));//用于生成随机数的种子initializeBoard();placeMines();printBoard();return0;}```这段代码创建了一个简单的扫雷游戏板,包含了8x8的方格,其中有10个地雷(-1表示地雷)。
项目需求,项目分析,实施方案,核心代码,项目制作体会!#include <iostream>#include "windows.h"#include <iostream>#include "time.h"#include "assert.h"#include "conio.h"using namespace std;struct eachcase{bool havebomb;int state;//if state = 0,means nothing; less then 8 and more then 1 means there are "state" bomb near here; 9 means you have find here ;//10 means you have point here;};class view{friend int check();public:view(int width,int hight,int numofbomb);void show();eachcase **qipan;int gethight();int getwidth();int getnumbomb();void falseshow();int bombnear(int,int);int check(bool** &,int,int);void flashshow();int check();// check whether ignite the bomb, if not, check arrond// return 1 means win ;2 means lose; 0 means nothing~view();private:int hight,width,numbomb;public:int nhight,nwidth;};int main(){bool quit = true;while(quit){std::cout<<"----------------------------------------------------------------"<<std::endl;std::cout<<"- welcome -"<<std::endl;std::cout<<"--"<<std::endl;std::cout<<"--"<<std::endl;std::cout<<"- w:up s:down a:left d :right p:point b:begin e:enter q:qiut-"<<std::endl;std::cout<<"--"<<std::endl;std::cout<<"--"<<std::endl;std::cout<<"--"<<std::endl;std::cout<<"--"<<std::endl;std::cout<<"- by uil from scut -"<<std::endl;std::cout<<"----------------------------------------------------------------"<<std::endl;char gets = cin.get();if(gets == 'q'){quit = false;continue;}else if(gets != 'b'){system("cls");continue;system("cls");int w,h,b;cout<<"please enter the with:"<<endl;cin>>w;if(w<1)w=10;cout<<"please enter the hight:"<<endl;cin>>h;if(h<1)h=10;cout<<"please enter the number of bomb:"<<endl;cin>>b;if(b<1)b=10;view views(h,w,b);clock_t start=clock();clock_t flash = start;bool** forcheck = new bool*[h];for(int f = 0; f < h;f++){forcheck[f] = new bool [w];for(int r = 0;r < w;r++){forcheck[f][r] = true;}}// make it for check() to check whether a case have been findint end=0; // deside whether stopbool restart = true ;while(cin&&!end&&restart&&quit){if((double)((clock() - flash)/clocks_per_sec) > 0.5){views.flashshow();sleep(300);}views.show();char get;get = _getch();switch(get){case 'w':if(views.nhight-1>=0 )//&& views.qipan[views.nhight-1][views.nwidth].state%10{views.nhight--;}break;case 's':if(views.nhight+1<views.gethight()) //&& views.qipan[views.nhight+1][views.nwidth].state%10 == 0){views.nhight++;}break;case 'a':if(views.nwidth-1>=0)// && views.qipan[views.nhight][views.nwidth-1].state%10 == 0){views.nwidth--;}break;case 'd':if(views.nwidth+1<views.getwidth())// && views.qipan[views.nhight][views.nwidth+1].state%10 == 0){views.nwidth++;}break;case 'p':end = views.check();break;case 'r':restart = false;break;// we need to improve herecase 'q':quit = false;break;case 'e':end = views.check(forcheck,views.nhight,views.nwidth);break;default:continue;}}views.show();//the main circle, which get the importif(end == 1){views.falseshow();cout<<endl<<"you are lose"<<endl;cout<<"use time:"<<(double)((clock() - start)/clocks_per_sec)<<endl; cout<<"try again? y/n";bool restarts = false;while(cin && !restarts){char a;cin>>a;if(a == 'n')return 0;else if(a == 'y'){restarts = true;}}}// need to improve.else if(end == 2){cout<<endl<<"you are win!"<<endl;cout<<"use time:"<<(double)((clock() - start)/clocks_per_sec)<<endl; cout<<"try again? y/n";char a;cin>>a;bool restarts = false;while(cin && !restarts){char a;cin>>a;if(a == 'n')return 0;else if(a == 'y'){restarts = true;}}}// need to improvesystem("cls");}return 0;}int view::check(bool** &forcheck,int h,int w){forcheck[h][w] = false;if(qipan[h][w].havebomb){return 1;}int wcheck = bombnear(h,w);// use to deside continue to check;if(wcheck){qipan[h][w].state= wcheck;}else{qipan[h][w].state = 9;if(h-1 >=0 && w-1>=0 && forcheck[h-1][w-1]) // here show the view.qipan[][] here have problem{check(forcheck,h-1,w-1);}if(h-1 >= 0 && forcheck[h-1][w]){check(forcheck,h-1,w);}if(h-1 >=0 && w+1 < width && forcheck[h-1][w+1]){check(forcheck,h-1,w+1);}if(w-1 >= 0 && forcheck[h][w-1]){check(forcheck,h,w-1);}if(w+1 < width && forcheck[h][w+1]){check(forcheck,h,w+1);}if(h+1 < hight && w-1 >= 0 && forcheck[h+1][w-1]){check(forcheck,h+1,w-1);}if(h+1 < hight && forcheck[h+1][w]){check(forcheck,h+1,w);}if(h+1 < hight && w+1 < width && forcheck[h+1][w+1]) {check(forcheck,h+1,w+1);}}return 0;}view::view(int hights,int widths,int numofbomb){hight = hights;////////////////width = hights;///////////////numbomb = numofbomb;nhight = 0;nwidth = 0;qipan = new eachcase*[hight];//assert(sizeof(qipan)<0);srand(time(0));// we use it to make bombint b = numofbomb;int h = 0;for(;h<=hight;h++){qipan[h] = new eachcase[width];//assert(sizeof(qipan[h])<0);for(int w = 0; w <= width ; w++){qipan[h][w].state = 0;qipan[h][w].havebomb = false;}}for(int nb = 0; nb <= numbomb-1;){int hb=rand()%hight;int wb=rand()%width;if(!qipan[hb][wb].havebomb){nb++;}}}int view::gethight(){return hight;}int view::getwidth(){return width;}int view::getnumbomb(){return numbomb;}void view::show(){system("cls");//qipan[nhight][nwidth].state=9;for(int n=0;n<=(width)*4+2;n++){std::cout<<'-';}for(int h=0;h<=hight-1;h++){std::cout<<std::endl<<'|';for(int w=0;w<=width-1;w++){int i=0;if(qipan[h][w].state == 0)std::cout<<"■ ";else if( 0 < qipan[h][w].state && qipan[h][w].state< 9)std::cout<<qipan[h][w].state<<" "; //need to advancedstd::cout<<"· ";else if(qipan[h][w].state == 10)std::cout<<"p ";else if(qipan[h][w].state == 11)std::cout<<"□ ";elseassert(0);}std::cout<<'|'<<std::endl;}for(int k=0;k<=(width)*4+2;k++){std::cout<<'-';}cout<<"hight:"<<nhight+1<<" width:"<<nwidth+1<<endl; }view::~view(){for(int n=0;n<=hight;n++)delete qipan[n];delete qipan;}int view::bombnear(int h,int w){int bombs = 0;if(h-1 >=0 && w-1>=0){if(qipan[h-1][w-1].havebomb)bombs++;}if(h-1 >=0){if(qipan[h-1][w].havebomb)bombs++;}if(h-1 >=0 && w+1 < width ){if(qipan[h-1][w+1].havebomb)bombs++;}if(w-1>=0){if(qipan[h][w-1].havebomb)bombs++;}if(w+1 < width ){if(qipan[h][w+1].havebomb)bombs++;}if(h+1 < hight&& w-1>=0){if(qipan[h+1][w-1].havebomb)bombs++;}if(h+1 < hight){if(qipan[h+1][w].havebomb)bombs++;}if(h+1 < hight && w+1 < width ) {if(qipan[h+1][w+1].havebomb)bombs++;}return bombs;}void view::falseshow(){system("cls");//qipan[nhight][nwidth].state=9; for(int n=0;n<=(width)*4+2;n++) {std::cout<<'-';}for(int h=0;h<=hight-1;h++){std::cout<<std::endl<<'|';for(int w=0;w<=width-1;w++){int i=0;if(qipan[h][w].state == 0){if(qipan[h][w].havebomb){std::cout<<"¤ ";}else{std::cout<<" ";}}else if( 0 < qipan[h][w].state && qipan[h][w].state< 9)std::cout<<qipan[h][w].state<<" "; //need to advanced else if(qipan[h][w].state == 9)std::cout<<" ";else if(qipan[h][w].state == 10){if(qipan[h][w].havebomb)std::cout<<"◎ ";else{std::cout<<"× ";}}else if(qipan[h][w].state == 11)std::cout<<"□";elseassert(0);}std::cout<<'|'<<std::endl;}for(int k=0;k<=(width)*4+2;k++){std::cout<<'-';}}void view::flashshow(){system("cls");//qipan[nhight][nwidth].state=9;for(int n=0;n<=(width)*4+2;n++){std::cout<<'-';}for(int h=0;h<hight;h++){std::cout<<std::endl<<'|';for(int w=0;w<width;w++){if( h == nhight && w == nwidth)std::cout<<"○ ";else if(qipan[h][w].state == 0)std::cout<<"■ ";else if( 0 < qipan[h][w].state && qipan[h][w].state< 9)std::cout<<qipan[h][w].state<<" "; //need to advanced else if(qipan[h][w].state == 9)std::cout<<"· ";else if(qipan[h][w].state == 10)std::cout<<"p ";else if(qipan[h][w].state == 11)std::cout<<"□ ";elseassert(0);}std::cout<<'|'<<std::endl;}for(int k=0;k<=(width)*4+2;k++){std::cout<<'-';}cout<<"hight:"<<nhight+1<<" width:"<<nwidth+1<<endl;}int view::check(){if(qipan[nhight][nwidth].state == 0)qipan[nhight][nwidth].state = 10;else if(qipan[nhight][nwidth].state == 10)qipan[nhight][nwidth].state = 0;int finded=0;for(int hs=0;hs <= hight;hs++){for(int ws=0;ws <= width;ws++){if(qipan[hs][ws].state == 10 /*|| qipan[hs][ws].state == 0*/ && qipan[hs][ws].havebomb)finded++;}}// check where find all bombif(finded == numbomb) //////////////////////////////////////////////////////error///////////////////// //////////////////{return 2;}return 0;}。