Skip to content

Commit

Permalink
Add puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
lurenjia committed May 20, 2016
1 parent c7dc6d8 commit 543b04d
Show file tree
Hide file tree
Showing 10 changed files with 738 additions and 5 deletions.
40 changes: 40 additions & 0 deletions 24-puzzle/puzzle/data_normal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 0
2 8 4 5 15 3 1 9 10 20 6 7 0 13 14 12 22 16 17 24 11 21 18 19 23
2 3 2 2
2 4 2 3
1 4 2 4
0 4 1 4
0 3 0 4
0 2 0 3
0 1 0 2
0 0 0 1
1 0 0 0
1 1 1 0
0 1 1 1
0 0 0 1
1 0 0 0
2 0 1 0
3 0 2 0
4 0 3 0
4 1 4 0
3 1 4 1
3 2 3 1
3 3 3 2
4 3 3 3
4 4 4 3
3 4 4 4
2 4 3 4
1 4 2 4
1 3 1 4
1 2 1 3
0 2 1 2
0 1 0 2
1 1 0 1
2 1 1 1
2 0 2 1
3 0 2 0
3 1 3 0
3 2 3 1
4 2 3 2
4 3 4 2
4 4 4 3
54 changes: 54 additions & 0 deletions 24-puzzle/puzzle/data_spiral.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
1 2 3 4 5 16 17 18 19 6 15 24 0 20 7 14 23 22 21 8 13 12 11 10 9
1 3 4 19 5 16 2 18 6 7 24 17 20 21 8 15 14 22 11 10 13 23 12 9 0
4 3 4 4
3 3 4 3
2 3 3 3
2 2 2 3
2 1 2 2
2 0 2 1
3 0 2 0
3 1 3 0
4 1 3 1
4 2 4 1
4 3 4 2
4 4 4 3
3 4 4 4
2 4 3 4
1 4 2 4
1 3 1 4
0 3 1 3
0 2 0 3
0 1 0 2
1 1 0 1
1 2 1 1
1 3 1 2
2 3 1 3
3 3 2 3
4 3 3 3
4 2 4 3
3 2 4 2
3 1 3 2
2 1 3 1
2 2 2 1
2 3 2 2
2 4 2 3
3 4 2 4
3 3 3 4
3 2 3 3
4 2 3 2
4 3 4 2
4 4 4 3
3 4 4 4
2 4 3 4
2 3 2 4
2 2 2 3
3 2 2 2
3 3 3 2
2 3 3 3
1 3 2 3
1 2 1 3
1 1 1 2
2 1 1 1
3 1 2 1
3 2 3 1
2 2 3 2
11 changes: 11 additions & 0 deletions 24-puzzle/puzzle/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}
102 changes: 102 additions & 0 deletions 24-puzzle/puzzle/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "mainwindow.h"
#define tt 100
#define tt1 50
#define tt2 90

queue<Movement> MainWindow::moveQueue;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
BigLabel* biglabel = new BigLabel(this);

fstream fin("/Users/yanshengjia/Desktop/puzzle/data_normal.txt");
for(int r=0;r<N;r++)
for(int c=0;c<N;c++)
fin>>destArr[r][c];

for(int r=0;r<N;r++)
for(int c=0;c<N;c++)
{
int x;
fin>>x;
if(x==0)
labelArr[r][c] = NULL;
else
{
labelArr[r][c] = new MyLabel(x,r,c,biglabel);
if(labelArr[r][c]->value==destArr[r][c])
labelArr[r][c]->changeColor(1);
}
}

int r1,c1,r2,c2;
while(fin>>r1>>c1>>r2>>c2)
{
MainWindow::moveQueue.push(Movement(r1,c1,r2,c2));
}

connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(start()));
}

void MainWindow::start()
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->start(tt+tt2);
}

void MainWindow::timerUpdate()
{
if(MainWindow::moveQueue.empty())
return;

exchange(MainWindow::moveQueue.front().r1, MainWindow::moveQueue.front().c1,
MainWindow::moveQueue.front().r2, MainWindow::moveQueue.front().c2);
MainWindow::moveQueue.pop();
}

void MainWindow::exchange(int r1, int c1, int r2, int c2)
{
QPropertyAnimation *anim1 = new QPropertyAnimation(labelArr[r1][c1],"pos");
anim1->setDuration(tt);
anim1->setStartValue(getXY(r1,c1));
anim1->setEndValue(getXY(r2,c2));

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(changeColor()));
timer->setSingleShot(true);
timer->start(tt+tt1);
anim1->start();

labelArr[r2][c2] = labelArr[r1][c1];
labelArr[r1][c1] = NULL;
rr = r2;
cc = c2;

// labelArr[r1][c1]->setRC(r2,c2);

}

void MainWindow::changeColor()
{
if(labelArr[rr][cc]->value==destArr[rr][cc])
labelArr[rr][cc]->changeColor(1);
else
labelArr[rr][cc]->changeColor(0);
}


QPoint MainWindow::getXY(int r, int c)
{
int x = c*len;
int y = r*len;
return QPoint(x,y);
}

MainWindow::~MainWindow()
{
delete ui;
}
53 changes: 53 additions & 0 deletions 24-puzzle/puzzle/mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#define nn 5
#include <QMainWindow>
#include <QPropertyAnimation>
#include "ui_mainwindow.h"
#include "mylabel.h"
#include "fstream"
#include "qtimer.h"
#include "queue"
using namespace std;
namespace Ui {
class MainWindow;
}

struct Movement
{
int r1,c1,r2,c2;
Movement(int r1, int c1, int r2, int c2)
{
this->r1=r1;this->r2=r2;
this->c1=c1;this->c2=c2;
}
};

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
QPoint getXY(int,int);
void exchange(int, int, int, int);
~MainWindow();

protected slots:
void start();
void timerUpdate();
void changeColor();

private:
Ui::MainWindow *ui;
const int N=nn;
const int len=100;
int rr;
int cc;
MyLabel* labelArr[nn][nn];
int destArr[nn][nn];
public:
static queue<Movement> moveQueue;
};

#endif // MAINWINDOW_H
54 changes: 54 additions & 0 deletions 24-puzzle/puzzle/mainwindow.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>720</width>
<height>589</height>
</rect>
</property>
<property name="windowTitle">
<string>24puzzle</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>580</x>
<y>30</y>
<width>113</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>start</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>720</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
64 changes: 64 additions & 0 deletions 24-puzzle/puzzle/mylabel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef MYLABEL
#define MYLABEL
#include "QLabel"
#include "QWidget"

class MyLabel : public QLabel
{
public:
MyLabel(int x,int r,int c, QWidget* par) : QLabel(par)
{
value = x;
if(x==-1)
this->setText("");
else
this->setText(QString::number(x));
setRC(r,c);
this->setStyleSheet("color:rgb(85, 170, 255);background-color: rgb(255, 255, 255);border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);");
this->setFont(QFont("微软雅黑",30));

this->setAlignment(Qt::AlignCenter);
this->setGeometry(QRect(c*len, r*len, 100, 100));
}

void setRC(int r, int c)
{
this->row=r;
this->col=c;
//this->setGeometry(QRect(col*len, row*len, 100, 100));
}

void changeColor(int x)
{
if(x==0)
this->setStyleSheet("color:rgb(85, 170, 255);background-color: rgb(255, 255, 255);border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);");
else
this->setStyleSheet("color:rgb(85, 170, 255);background-color: rgb(255,255,0);border-width: 1px;border-style: solid;border-color: rgb(0, 0, 0);");
}

public:
int value;

private:
const int len = 100;
int row;
int col;
};

class BigLabel : public QLabel
{

public:
BigLabel(QWidget* par) : QLabel(par)
{
this->setStyleSheet("background-color: rgb(255, 255, 255);");
this->setGeometry(QRect(20,20,N*len, N*len));
this->lower();
}
private:
const int N=5;
const int len = 100;
};

#endif // MYLABEL

Loading

0 comments on commit 543b04d

Please sign in to comment.