c++代码分析

jiao879111 2015-10-26 08:24:40
#include <QPainter>
#include <QtGui/QApplication>
#include <QMouseEvent>
#include<ctime>
#include <QtGui>
#include "dialog.h"
#include "ui_dialog.h"
using namespace std;

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{


source_picture_show=false;
ui->setupUi(this);
ui->spinBox->setMinimum(2);//设置最小值
ui->spinBox->setValue(4);
n=ui->spinBox->value();
//n=4;
N=n*n;

a = new int* [n+2];
for(int i=0; i <n+2; i++)
a[i] = new int[n+2];

wsubsize=100;
hsubsize=100;

bak.load(":/new/prefix1/background3.png");
Bak=bak;

setbak();
ui->verticalWidget->setGeometry(n*wsubsize,10,100,150);
//wsize=400;
//hsize=400;
//move(100,10);
//max_n=5;




//initpicture();
//update();

}



Dialog::~Dialog()
{
delete ui;
}


void Dialog::setbak()
{

Bak=Bak.scaled( n*wsubsize , n*hsubsize,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QPainter pb(&Bak);
for (int i=0;i<=n*wsubsize/127;i++ )
for (int j=0;j<=n*hsubsize/127;j++)
pb.drawPixmap( i*127, j*127, bak);
}

void Dialog::paintEvent(QPaintEvent *)
{
//move( (QApplication::desktop()->width() - width())/2, (QApplication::desktop()->height() - height())/2);

//()<<"start paint";
//setFixedHeight(n*hsubsize);

//setFixedWidth(n*wsubsize+100);


int i,j,p;
QPainter painter(this);
if (source_picture_show)
painter.drawPixmap( 0, 0, pix);
else
{



painter.drawPixmap( 0, 0, Bak);
for (int l=1;l<=pixmaps.size();l++)
{
i=(l-1)%n+1;
j=(l-1)/n+1;
p=a[i][j]-1;
if (p<pixmaps.size()-1)
painter.drawPixmap( (i-1)*wsubsize, (j-1)*hsubsize, pixmaps[p]);
}
}


}

void Dialog::mousePressEvent(QMouseEvent *m)
{

if (source_picture_show)
return;
else
{
int x = m->pos().x();
int y = m->pos().y();
hit(x/wsubsize+1 , y/hsubsize+1);
}
}

void Dialog::hit(int i,int j)
{
if (i<1 || j<1 || i>n || j>n)
return;
if (a[i-1][j]==N)
{

a[i-1][j]=a[i][j];
a[i][j]=N;
}
else if (a[i+1][j]==N)
{
a[i+1][j]=a[i][j];
a[i][j]=N;
}
else if (a[i][j-1]==N)
{
a[i][j-1]=a[i][j];
a[i][j]=N;
}
else if (a[i][j+1]==N)
{
a[i][j+1]=a[i][j];
a[i][j]=N;
}

update();
if (check())
{
QMessageBox msgBox(this);
msgBox.setText(tr("success!"));
msgBox.exec();
//initrandom();
//refresh();
}
}

void Dialog::randomp()
{
for(int i=0; i <n+2; i++)
for(int j=0; j <n+2; j++)
{
if (i==0 || j==0 || i==n+1 || j==n+1)
a[i][j]=0;
else
a[i][j]=(j-1)*n+i;
}
int i,j;
int p;
for(int l=1;l<100*N;l++)
{
p=findN();
i=(p-1)%n+1;
j=(p-1)/n+1;

switch (rand()%4)
{
case 0:
if ((i-1)>0)
swap(a[i][j],a[i-1][j]);
break;
case 1:
if ((i+1)<n+1)
swap(a[i][j],a[i+1][j]);
break;
case 2:
if ((j-1)>0)
swap(a[i][j],a[i][j-1]);
break;
case 3:
if ((j+1)<n+1)
swap(a[i][j],a[i][j+1]);
break;
}
}

}

void Dialog::initpicture()
{

QString tempfile;
//tempfile = QFileDialog::getOpenFileName(this, tr("Open"));
tempfile = QFileDialog::getOpenFileName(this,
tr("Open Image"), "./pictures", tr("Image Files (*.png *.jpg *.bmp)"));

if (!pix.load(tempfile))
{
////()<<"?";
return;
}
n=ui->spinBox->value();
N=n*n;
//()<<"ok4";
a = new int* [n+2];
for(int i=0; i <n+2; i++)
a[i] = new int[n+2];


wsize = pix.width();
hsize = pix.height();
if (hsize>700)
{
pix=pix.scaled( wsize*700/hsize, 700, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
wsize = pix.width();
hsize = pix.height();
}
else if (hsize<400)
{
pix=pix.scaled( wsize*400/hsize, 400, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
wsize = pix.width();
hsize = pix.height();
}

wsubsize = wsize/n;
hsubsize = hsize/n;
pix = pix.copy(0,0, n*wsubsize, n*hsubsize);

pixmaps.clear();

//()<<pixmaps.length();

for (int y = 0; y < n; y++)
{
for (int x = 0; x < n; x++)
{
QPixmap pieceImage = pix.copy(x*wsubsize, y*hsubsize, wsubsize, hsubsize);
pixmaps.append(pieceImage);
}
}
//pixmaps[pixmaps.size()-1]=QPixmap(subsize, subsize);
//pixmaps[pixmaps.length()-1].fill(QColor(200, 200, 200));
randomp();
setbak();
setGeometry( (QApplication::desktop()->width() - (n*wsubsize+100))/2, (QApplication::desktop()->height() - n*hsubsize)/2,
n*wsubsize+100,n*hsubsize);
//ui->pushButton ->setGeometry(n*wsubsize+10,10,80,23);
//ui->pushButton_2->setGeometry(n*wsubsize+10,50,80,23);
ui->verticalWidget->setGeometry(n*wsubsize,10,100,150);
update();
//move( (QApplication::desktop()->width() - width())/2, (QApplication::desktop()->height() - height())/2);
//qDebug()<<QApplication::desktop()->width()<<width();
//pix.fill( &pixmaps[pixmaps.length()-1],0,0);
}

int Dialog::findN()
{
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (a[i][j]==N)
return n*(j-1)+i;
//return 0;
}

bool Dialog::check()
{
bool good=true;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
good=good&&(a[i][j]==n*(j-1)+i);
return good;
}

void Dialog::on_pushButton_clicked()
{
initpicture();
//()<<"ok";

}

void Dialog::on_pushButton_2_clicked()
{
//setGeometry(x(),y(),2*n*subsize+100, n*subsize);
if (source_picture_show)
{
//setFixedWidth(n*wsubsize+100);
ui->pushButton_2->setText(tr("show image"));
source_picture_show=false;
update();
}
else
{
//setFixedWidth(2*n*wsubsize+100);
ui->pushButton_2->setText(tr("Back game"));
source_picture_show=true;
update();
}

}



void Dialog::on_spinBox_editingFinished()
{

}
...全文
166 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
侯壮壮 2015-10-27
  • 打赏
  • 举报
回复
这是不是一个打砖块的游侠?
jiao879111 2015-10-27
  • 打赏
  • 举报
回复
void Dialog::hit(int i,int j) { if (i<1 || j<1 || i>n || j>n) return; if (a[i-1][j]==N) { a[i-1][j]=a[i][j]; a[i][j]=N; } else if (a[i+1][j]==N) { a[i+1][j]=a[i][j]; a[i][j]=N; } else if (a[i][j-1]==N) { a[i][j-1]=a[i][j]; a[i][j]=N; } else if (a[i][j+1]==N) { a[i][j+1]=a[i][j]; a[i][j]=N; } 你能帮我分析一下这些数组的意思么?
jiao879111 2015-10-27
  • 打赏
  • 举报
回复
这是一个拼图游戏
赵4老师 2015-10-26
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
老王爱上猫 2015-10-26
  • 打赏
  • 举报
回复
能注意格式贴么,另外也说下问题呗
jiao879111 2015-10-26
  • 打赏
  • 举报
回复
帮忙注释一下
fly_dragon_fly 2015-10-26
  • 打赏
  • 举报
回复
是什么问题要说清楚

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧