-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
107 lines (95 loc) · 1.95 KB
/
board.cpp
File metadata and controls
107 lines (95 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "board.h"
board::board():p1(),p2()
{
}
bool board::tryMove(int x1,int y1,int x2,int y2) //from 1 to 2
{
block& target=m.blockAt(x2,y2);
if(target.inable())
{
if(target.hidable())
return tryInToHide(x1,y1,x2,y2);
else
return trySimpleMove(x1,y1,x2,y2);
}
else
{
if(target.pushable())
return tryPush(x1,y1,x2,y2);
else return false;
}
}
//bug?
bool board::trySimpleMove(int x1,int y1,int x2,int y2)
{
m.moveAToB(x1,y1,x2,y2);
return true;
}
bool board::tryPush(int x1,int y1,int x2,int y2)
{
block& target1=m.blockAt(x2,y2);
int x3=x2+x2-x1,y3=y2+y2-y1;
block& target2=m.blockAt(x3,y3);
if(target2.inable())
{
if(target1.hidable())
return tryInToHide(x1,y1,x2,y2);
else
return trySimpleMove(x1,y1,x2,y2);
if(target2.hidable())
return tryInToHide(x2,y2,x3,y3);
else
return trySimpleMove(x2,y2,x3,y3);
}
else
return false;
}
bool board::tryInToHide(int x1,int y1,int x2,int y2)
{
m.moveAToB(x1,y1,x2,y2);
return true;
}
void board::countMapForDraw()
{
for(int i=1;i<=10;i++)
for(int j=1;j<=10;j++)
{
mapForDraw[i][j]=m.blockAt(i,j).appearance();
}
}
void board::paintEvent(QPaintEvent* event)
{
countMapForDraw();
}
void board::keyPressEvent(QKeyEvent* event)
{
switch(event->key())
{
//p1:
case Qt::Key_W:
tryMoveUp(p1);
break;
case Qt::Key_S:
tryMoveDown(p1);
break;
case Qt::Key_A:
tryMoveLeft(p1);
break;
case Qt::Key_D:
tryMoveRight(p1);
break;
//p2:
case Qt::Key_Up:
tryMoveUp(p2);
break;
case Qt::Key_Down:
tryMoveDown(p2);
break;
case Qt::Key_Left:
tryMoveLeft(p2);
break;
case Qt::Key_Right:
tryMoveRight(p2);
break;
}
}