1 Star 0 Fork 0

刘耀月/c++大作业

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MainWindow6.cpp 14.94 KB
一键复制 编辑 原始数据 按行查看 历史
刘耀月 提交于 2022-05-03 14:08 . my commit
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
#include "MainWindow.h"
#include "ui_mainwindow.h"
#include <QAbstractButton>
#include <QMessageBox>
#include <QPaintEvent>
#include <QPainter>
#include <QTime>
//待解决:需要完善findall条件
//已解决:需要改变界面颜色
//待解决:需要添加历史记录功能
//已解决:改变item点开后的数字大小
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//菜单栏 初始化
menu1=new QMenu("新游戏",this);
menu2=new QMenu("难度",this);
menu3=new QMenu("历史纪录",this);
ui->menuBar->addMenu(menu1);
ui->menuBar->addMenu(menu2);
ui->menuBar->addMenu(menu3);
connect(menu1,SIGNAL(aboutToShow()),this,SLOT(slot_newGame()));
//Action 初始化
easy=new QAction("easy",this);
normal=new QAction("normal",this);
hard=new QAction("hard",this);
self=new QAction("自定义",this);
max=new QAction("最高记录",this);
menu2->addAction(easy);
menu2->addAction(normal);
menu2->addAction(hard);
menu2->addAction(self);
menu3->addAction(max);
connect(easy,SIGNAL(triggered()),this,SLOT(slot_level_easy()));
connect(normal,SIGNAL(triggered()),this,SLOT(slot_level_normal()));
connect(hard,SIGNAL(triggered()),this,SLOT(slot_level_hard()));
connect(self,SIGNAL(triggered()),this,SLOT(slot_level_self()));
dialogSelf=new Dialog(this);
connect(dialogSelf,SIGNAL(dia_test()),this,SLOT(slot_level_self_get()));
flagImage = QPixmap(":/images/images/flag.png"); //旗帜
bombImage = QPixmap(":/images/images/bomb.png"); //雷
//默认打开为简单难度
rows = 9;
cols = 9;
mine = 10;
newGame();
}
//主窗口的析构函数
MainWindow::~MainWindow()
{
delete ui;
}
//定义初始化游戏并放置方格的函数
void MainWindow::initItems()
{
//随机初始化 雷
Mines.clear();
for(int i = 0; i<mine; i++)
{
qsrand(QTime::currentTime().msec());
int x = qrand()%cols;
int y = qrand()%rows;
while(Mines.contains(QPoint(x,y)))
{//当已经包含了这个雷 则重新随机生成一个雷
x = qrand()%cols;
y = qrand()%rows;
}
Mines.append(QPoint(x,y));
}
//建立2维数组保存所有元素位置,方便索引
for(int i=0; i<cols; i++) //列
{
QVector<Item*> rowItems;//Item对象指针的容器
for(int j=0; j<rows; j++)
{
QPoint pos = QPoint(i,j);
Item* pItem = new Item(pos);
if(Mines.contains(pos)) //若该位置是雷
{
pItem->isMine = true;
}
rowItems.append(pItem);
}
items.append(rowItems);
}
//计算格子的数字
for(int i=0; i<cols; i++)
{
for(int j=0; j<rows; j++)
{
if (items[i][j]->isMine)
{
continue;
}
int nCountMines = 0;
//求每个点附近的8个点的是雷的总数
for (int m=-1;m<=1;m++)
{
for (int n=-1; n<=1;n++)
{
if (m==0 && n==0)
{
continue;
}
QPoint ptNew = QPoint(i+m,j+n);
if (!pointInGameArea(ptNew))
{
continue;
}
if (items[i+m][j+n]->isMine)
{
nCountMines++;
}
}
}
items[i][j]->number = nCountMines;
}
}
}
//定义释放所有方格对象的函数
void MainWindow::releaseItems()
{ //清除所有的指针
//释放空间
for (int i=0; i<items.size(); i++)
{
for (int j=0;j<items[i].size(); j++)
{
if (items[i][j] != nullptr)
{
delete items[i][j];
items[i][j] = nullptr;
}
}
}
items.clear();
}
//定义新游戏函数
void MainWindow::newGame()
{
m_bGameFail = false;
//初始化界面大小
resize(START_X*2 + cols*RECT_WIDTH ,START_Y*2 + rows*RECT_HEIGHT);
//设置固定大小
this->setFixedSize(START_X*2 + cols*RECT_WIDTH ,START_Y*2 + rows*RECT_HEIGHT);
//释放所有方格对象
releaseItems();
//初始化游戏并放置方格
initItems();
}
//定义失败结束游戏的函数(需要改进结束条件)
void MainWindow::overGame()
{
m_bGameFail = true;
for (int i=0; i<items.size(); i++)
{
for (int j=0;j<items[i].size(); j++)
{
if (items[i][j]->isMine)
{
items[i][j]->isMarked = true;
}
else
{
items[i][j]->isMarked = false;
items[i][j]->isOpen = true;
}
}
}
}
//每次点击之后都会调用这个函数 判断是否找全
bool MainWindow::findAll()
{
bool bFindAll = true;
//遍历二维数组 QVector<QVector<Item*>> m_items
for (int i=0; i<items.size(); i++)
{
for (int j=0;j<items[i].size(); j++)
{
//只要存在一个雷没被标记,或存在一个非雷被没打开,都不算找完
Item* pItem = items[i][j];
if (pItem->isMine)
{
if (!pItem->isMarked)
{
bFindAll = false;
}
}
else
{
if (!pItem->isOpen)
{
bFindAll = false;
}
}
}
}
return bFindAll;
}
void MainWindow::paintEvent(QPaintEvent *)
{
drawBoard();
drawItems();
//刷新游戏画面
update();
}
//鼠标事件 左键右键点击 点击后都要判断是否findall
void MainWindow::mousePressEvent(QMouseEvent *e)
{
//得到鼠标处的格子坐标
QPoint pt;
pt.setX( (e->pos().x() - START_X ) / RECT_WIDTH);
pt.setY( (e->pos().y() - START_X ) / RECT_HEIGHT);
//是否点在游戏区域内
if (!pointInGameArea(pt))
{
return; //如果不在游戏区域内 不做反应
}
//获取所点击矩形元素
Item* pItem = items[pt.x()][pt.y()];
//左键打开元素
if(e->button()==Qt::LeftButton)
{
//不是已标记的或已打开的空白点,也就是未处理的
if(!pItem->isMarked && !pItem->isOpen)
{
//如果是雷,就GAME OVER
if (pItem->isMine)
{
//弹出游戏结束窗口
overGame();
//弹出选择窗口 选择yes就newgame重新初始化游戏 选择no就退出程序
QMessageBox::StandardButton ret;
ret=QMessageBox::information(nullptr, "失败","是否重试?",
QMessageBox::Yes , QMessageBox::No);
if(ret==QMessageBox::Yes)
{
newGame();
}
else
{
QApplication::quit();
}
return;
}
//如果不是雷,打开并判断是否findall
else
{
//打开
pItem->isOpen = true;
if (pItem->number == 0)
{
//如果数字是0,也就是不含任何相邻雷的元素,那么递归打开所有的相邻数字是0的元素
//也就是点到一个空白处,一下打开一大片的效果
openEmptyItem(pt);
}
//如果已找到所有雷 这里需要完善findall的条件
if (findAll())
{
//弹出成功窗口
QMessageBox::StandardButton ret;
//弹出选择窗口 选择yes就newgame重新初始化游戏 选择no就退出程序
ret=QMessageBox::information(nullptr, "成功","是否开始新游戏?",
QMessageBox::Yes , QMessageBox::No);
if(ret==QMessageBox::Yes)
{
newGame();
}
else
{
QApplication::quit();
}
return;
}
}
}
}
//右键插旗帜标记
else if(e->button()==Qt::RightButton)
{
//已标记过的,取消标记
if (pItem->isMarked)
{
pItem->isMarked = false;
}
else if (!pItem->isOpen)
{
//没标记也没打开,就插旗帜标记上
pItem->isMarked = true;
//判断是否findall
if (findAll())
{
QMessageBox::StandardButton ret;
//弹出选择窗口 选择yes就newgame重新初始化游戏 选择no就退出程序
ret=QMessageBox::information(nullptr, "成功","是否开始新游戏?",
QMessageBox::Yes , QMessageBox::No);
if(ret==QMessageBox::Yes)
{
newGame();
}
else
{
QApplication::quit();
}
return;
}
}
}
}
void MainWindow::drawBoard()
{
QPainter painter(this);
painter.setBrush(Qt::lightGray);
painter.drawRect( 0,0,width(),height());
}
void MainWindow::drawItems()
{
QPainter painter(this);
painter.setBrush(Qt::lightGray);
painter.setPen(QPen(QColor(Qt::black),2));
for(int i=0; i<cols; i++)
{
for(int j=0; j<rows; j++)
{
drawItem(painter,items[i][j]);
}
}
}
void MainWindow::drawItem(QPainter &painter, Item *pItem)
{
if(pItem->isMarked)
{
if (m_bGameFail)
{
//游戏结束,显示为雷
QRect rcSrc(0,0,flagImage.width(),flagImage.height());
QRect rcTarget(START_X + pItem->m_pos.x()*RECT_WIDTH + 2,START_Y + pItem->m_pos.y()*RECT_HEIGHT + 2,RECT_WIDTH-4,RECT_HEIGHT-4);
painter.drawPixmap(rcTarget,bombImage,rcSrc);
}
else
{
//游戏没结束,显示为旗子
QRect rcSrc(0,0,flagImage.width(),flagImage.height());
QRect rcTarget(START_X + pItem->m_pos.x()*RECT_WIDTH + 2,START_Y + pItem->m_pos.y()*RECT_HEIGHT + 2,RECT_WIDTH-4,RECT_HEIGHT-4);
painter.drawPixmap(rcTarget,flagImage,rcSrc);
}
painter.setBrush(Qt::transparent);
painter.drawRect( START_X + pItem->m_pos.x()*RECT_WIDTH,START_Y + pItem->m_pos.y()*RECT_HEIGHT,RECT_WIDTH,RECT_HEIGHT);
return;
}
else if (pItem->isOpen)
{
if(pItem->number == 0)
{
painter.setBrush(Qt::gray);
}
else
{
QFont font;
font.setPointSize(20);
font.setFamily(("msyh"));
font.setBold(true);
font.setFamily("Microsoft YaHei");
font.setPointSize(6);
//float f = pItem->m_nNumber/8;
//painter.setBrush(QColor(f*255,f*255,0));
painter.setBrush(Qt::gray);
painter.drawRect( START_X + pItem->m_pos.x()*RECT_WIDTH,
START_Y + pItem->m_pos.y()*RECT_HEIGHT,RECT_WIDTH,RECT_HEIGHT);
painter.setBrush(Qt::white);
painter.setFont(font);
painter.drawText( START_X + pItem->m_pos.x()*RECT_WIDTH + 8,
START_Y + pItem->m_pos.y()*RECT_HEIGHT + 22,QString("%1").arg(pItem->number));
return;
}
}
else
{
painter.setBrush(Qt::white);
}
painter.drawRect( START_X + pItem->m_pos.x()*RECT_WIDTH,START_Y + pItem->m_pos.y()*RECT_HEIGHT,RECT_WIDTH,RECT_HEIGHT);
}
//运气好时点到一个空白元素,可能打开挨着的一大片
void MainWindow::openEmptyItem(QPoint pt)
{
//对于空白元素,有上下左右4个方向挨着的空白元素,就打开并继续查找空白元素
QVector<QPoint> directions;
directions.push_back(QPoint(-1,0));
directions.push_back(QPoint(1,0));
directions.push_back(QPoint(0,-1));
directions.push_back(QPoint(0,1));
for (int i=0; i<directions.size(); i++)
{
QPoint ptNew = pt + directions[i];
if (!pointInGameArea(ptNew))
{
continue;
}
Item* pItem = items[ptNew.x()][ptNew.y()];
if (!pItem->isMine && !pItem->isOpen && !pItem->isMarked && pItem->number >= 0)
{
//若不是雷 未被打开 未被标记 且数为0
//则将其打开
pItem->isOpen = true;
//对于找到的空白元素,在它的8个方向上有数字元素就打开
QVector<QPoint> directions2 = directions;
directions2.push_back(QPoint(-1,-1));
directions2.push_back(QPoint(1,1));
directions2.push_back(QPoint(1,-1));
directions2.push_back(QPoint(-1,1));
for (int j=0; j<directions2.size(); j++)
{
//对四个方向进行递归查看
QPoint ptNew2 = ptNew + directions2[j];
if(!pointInGameArea(ptNew2))
{
continue;
}
Item* pItem2 = items[ptNew2.x()][ptNew2.y()];
if (!pItem2->isMine && !pItem2->isOpen && !pItem2->isMarked && pItem2->number > 0)
{
//若不是雷 未被打开 未被标记 且数大于0
//则将其打开
pItem2->isOpen = true;
}
}
//递归查找上下左右4个方向的空白元素
openEmptyItem(ptNew);
}
}
}
//判断鼠标是否在游戏区域内
bool MainWindow::pointInGameArea(QPoint pt)
{
if(pt.x()>=0 && pt.x()< cols && pt.y()>=0 && pt.y()< rows)
{
return true;
}
return false;
}
//定义新游戏函数
void MainWindow::slot_newGame()
{
qDebug()<<"slot_newGame()";
newGame();
}
//定义easy难度
void MainWindow::slot_level_easy()
{
rows = 9;
cols = 9;
mine = 10;
newGame();
}
//定义normal难度
void MainWindow::slot_level_normal()
{
rows = 16;
cols = 16;
mine = 40;
newGame();
}
//定义hard难度
void MainWindow::slot_level_hard()
{
rows = 16;
cols = 30;
mine = 99;
newGame();
}
//定义自定义难度 调用自定义窗口
void MainWindow::slot_level_self()
{
dialogSelf->show();
}
//定义自定义难度参数获取函数
void MainWindow::slot_level_self_get()
{
rows=dialogSelf->getRows();
cols=dialogSelf->getCols();
mine=dialogSelf->getMine();
// qDebug()<<rows;
// qDebug()<<cols;
// qDebug()<<mine;
//传入行数列数雷数之后开始新游戏
newGame();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/liu-yaoyue/c-major-operation.git
git@gitee.com:liu-yaoyue/c-major-operation.git
liu-yaoyue
c-major-operation
c++大作业
master

搜索帮助