PuzzleGame部分核心算法

#include

"mainwindow.h"
#include

<QGridLayout>
#include

<QPushButton>
#include

<QSizePolicy>
#include

<QButtonGroup>
#include

<QDebug>
#include

<QString>
#include

<qdir.h>
MainWindow
::
MainWindow
(
QWidget

*
parent
)

:

QWidget
(
parent
)
{

init
();

this
->
setFixedSize
(
500
,

500
);
}
void

MainWindow
::
init
(){

gridLayout

=

new

QGridLayout
(
this
);

buttonGroup

=

new

QButtonGroup
(
this
);

int

index

=

0
;

for
(
int

j

=

0
;

j

<

MAX_Y
;

j
++){
//4

for
(
int

i

=

0
;

i

<

MAX_X
;

i
++){
//3

//

qDebug()

<<

i

<<

j;

index

++;

button

=

new

QPushButton
(
QString
::
number
(
index
),

this
);

//

button

=

new

QPushButton(QString::number(i),

this);

//

qDebug()

<<

i

<<

j;

//

button->setText(QString::number(index));

button
->
setSizePolicy
(
QSizePolicy
::
Expanding
,

QSizePolicy
::
Expanding
);

button
->
setStyleSheet
(
"border-image:url(':/test/1.png')"
);

qDebug
()

<<

QDir
::
currentPath
();

//

button->setText("empty");

gridLayout
->
addWidget
(
button
,

j
,

i
);

buttonGroup
->
addButton
(
button
,

index
);

//

buttons[j][i]

=

new

QPushButton(this);

//

buttons[j][i]

=

button;

}

}

buttonGroup
->
button
(
MAX_X

*

MAX_Y
)->
hide
();

buttonGroup
->
button
(
MAX_X

*

MAX_Y
)->
setText
(
"empty"
);

connect
(
buttonGroup
,

SIGNAL
(
buttonClicked
(
int
)),

this
,

SLOT
(
slotClick
(
int
)));

this
->
setLayout
(
gridLayout
);
}
void

MainWindow
::
slotClick
(
int

id
){

QPushButton

*
button

=

(
QPushButton

*)
buttonGroup
->
button
(
id
);

//

qDebug()

<<

button->text();

if
(
id

%

MAX_X

!=

0
){

x

=

id

/

MAX_X
;

y

=

id

-

(
x

*

MAX_X
)

-

1
;

}
else
{

x

=

id

/

MAX_X

-

1
;

y

=

id

-

(
x

*

MAX_X
)

-

1
;

}

qDebug
()

<<

"id:"

<<

id
;

qDebug
()

<<

MAX_X

<<

MAX_Y
;

qDebug
()

<<

"x:"

<<

x

<<

"y:"

<<

y
;

if
(
x

==

0
){
//第一行

if
(
y

==

0
){
//第一列.即左上角。

右移、下移

qDebug
()

<<

"第一列.即左上角。

右移、下移"
;

if
(
right
(
x
,
y
)){

moveRight
();

}
else

if
(
down
(
x
,
y
)){

moveDown
();

}

}
else

if
(
y

==

MAX_X

-

1
){
//第一行最后一列,右上角。左移’下移

qDebug
()

<<

"最后一列,右上角。左移’下移"
;

if
(
left
(
x
,
y
)){

moveLeft
();

}
else

if
(
down
(
x
,
y
)){

moveDown
();

}

}
else
{
//第一行,除了头和尾的两个。左移’右移‘下移

qDebug
()

<<

"第一行,除了头和尾的两个。左移’右移‘下移"
;

if
(
left
(
x
,
y
)){

moveLeft
();

}
else

if
(
right
(
x
,
y
)){

moveRight
();

}
else

if
(
down
(
x
,
y
)){

moveDown
();

}

}

}
else

if
(
x

==

MAX_Y

-

1
){
//最后一行

if
(
y

==

0
){
//第一列。左下角。上移‘右移

qDebug
()

<<

"第一列。左下角。上移‘右移"
;

if
(
up
(
x
,

y
)){

qDebug
()

<<

"可以上移"
;

moveUp
();

}
else

if
(
right
(
x
,
y
)){

moveRight
();

}

}
else

if
(
y

==

MAX_X

-

1
){
//最后一列,即右下角。左移’上移

qDebug
()

<<

"最后一列,即右下角。左移’上移"
;

if
(
left
(
x
,
y
)){

moveLeft
();

}
else

if
(
up
(
x
,
y
)){

moveUp
();

}

}
else
{
//最后一行,除了头和尾的两个。上移‘左移’右移

qDebug
()

<<

"最后一行,除了头和尾的两个。上移‘左移’右移"
;

if
(
up
(
x
,
y
)){

moveUp
();

}
else

if
(
left
(
x
,
y
)){

moveLeft
();

}
else

if
(
right
(
x
,
y
)){

moveRight
();

}

}

}
else
{

if
(
y

==

0
){
//第一列。除了头和尾的两个。上移‘下移’右移

qDebug
()

<<

"第一列。除了头和尾的两个。上移‘下移’右移"
;

if
(
up
(
x
,
y
)){

moveUp
();

}
else

if
(
down
(
x
,
y
)){

moveDown
();

}
else

if
(
right
(
x
,
y
)){

moveRight
();

}

}
else

if
(
y

==

MAX_X

-

1
){
//最后一列,除了头和尾的两个。上移‘下移’左移。

qDebug
()

<<

"最后一列,除了头和尾的两个。上移‘下移’左移。"
;

if
(
down
(
x
,

y
)){

qDebug
()

<<

"可以下移"
;

moveDown
();

}
else

if
(
up
(
x
,

y
)){

moveUp
();

}
else

if
(
left
(
x
,

y
)){

moveLeft
();

}

}
else
{
//中间的,可以上下左右移动的按钮。

qDebug
()

<<

"中间的,可以上下左右移动的按钮。"
;

if
(
up
(
x
,
y
)){

moveUp
();

}
else

if
(
left
(
x
,
y
)){

moveLeft
();

}
else

if
(
right
(
x
,
y
)){

moveRight
();

}
else

if
(
down
(
x
,
y
)){

moveDown
();

}

}

}
}
bool

MainWindow
::
up
(
int

x
,

int

y
){

//if(buttonGroup->button(x-1,

y))

qDebug
()

<<

"text:"

<<

buttonGroup
->
button
(
MAX_X

*

(
x

-

1
)

+

y

+

1
)->
text
();

if
(
buttonGroup
->
button
(
MAX_X

*

(
x

-

1
)

+

y

+

1
)->
text
()

==

"empty"
){

return

true
;

}

return

false
;
}
bool

MainWindow
::
down
(
int

x
,

int

y
){

if
(
buttonGroup
->
button
(
MAX_X

*

(
x

+

1
)

+

y

+

1
)->
text
()

==

"empty"
){

return

true
;

}

return

false
;
}
bool

MainWindow
::
left
(
int

x
,

int

y
){

if
(
buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1

-

1
)->
text
()

==

"empty"
){

return

true
;

}

return

false
;
}
bool

MainWindow
::
right
(
int

x
,

int

y
){

if
(
buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1

+

1
)->
text
()

==

"empty"
){

return

true
;

}

return

false
;
}
void

MainWindow
::
moveUp
(){

buttonGroup
->
button
(
MAX_X

*

(
x

-

1
)

+

y

+

1
)->
setText
(
buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
text
());

buttonGroup
->
button
(
MAX_X

*

(
x

-

1
)

+

y

+

1
)->
show
();

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
setText
(
"empty"
);

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
hide
();
}
void

MainWindow
::
moveDown
(){

buttonGroup
->
button
(
MAX_X

*

(
x

+

1
)

+

y

+

1
)->
setText
(
buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
text
());

buttonGroup
->
button
(
MAX_X

*

(
x

+

1
)

+

y

+

1
)->
show
();

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
setText
(
"empty"
);

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
hide
();
}
void

MainWindow
::
moveLeft
(){

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1

-

1
)->
setText
(
buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
text
());

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1

-

1
)->
show
();

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
setText
(
"empty"
);

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
hide
();
}
void

MainWindow
::
moveRight
(){

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1

+

1
)->
setText
(
buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
text
());

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1

+

1
)->
show
();

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
setText
(
"empty"
);

buttonGroup
->
button
(
MAX_X

*

(
x
)

+

y

+

1
)->
hide
();
}

wKiom1OW2SaAZ9SzAALK-rjef_g562.jpg

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1424700

你可能感兴趣的:(game)