-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
158 lines (136 loc) · 3.38 KB
/
Player.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "SDL.h"
#include "Player.h"
#include "pieces.h"
Player::Player(Field *field)
{
m_field = field;
}
Player::~Player()
{
}
void
Player::setContext(Piece *piece, Piece *nextPiece)
{
m_piece.setPiece(piece->getPiece());
m_piece.setRotation(piece->getRotation());
m_nextPiece.setPiece(nextPiece->getPiece());
m_nextPiece.setRotation(nextPiece->getRotation());
calculateDest();
}
int
Player::getNextAction(int posX, int posY, int rotation)
{
if (rotation != m_destRotation) {
return ACTION_ROTATE;
}
if (posX < m_destPosX) {
return ACTION_MOVE_RIGHT;
}
if (posX > m_destPosX) {
return ACTION_MOVE_LEFT;
}
return ACTION_DROP;
}
void
Player::calculateDest()
{
int bestFit = -1000;
int bestX = -1000, bestR = -1000;
for (int x=0; x<FIELD_WIDTH; x++) {
int maxRotation = 4;
// reduce number of rotations for symmetric pieces
if (m_piece.getPiece() == PIECE_O) {
maxRotation = 1;
} else if (
m_piece.getPiece() == PIECE_S
|| m_piece.getPiece() == PIECE_Z
|| m_piece.getPiece() == PIECE_I
) {
maxRotation = 2;
}
for (int r=0; r<maxRotation; r++) { // rotations
m_piece.setRotation(r);
int fit = checkXR(x, r);
if (fit > bestFit) {
bestFit = fit;
bestX = x;
bestR = r;
}
}
}
m_destPosX = bestX;
m_destRotation = bestR;
}
int
Player::checkXR(int posX, int r)
{
int x, y, posY = 0;
int joins = 0, blanks = 0;
int collisions;
// drop piece to find posY
for (;;) {
collisions = m_field->checkCollisions(
&m_piece,
posX,
posY+1,
Field::COLLIDED_HEAP|Field::COLLIDED_LEFT|Field::COLLIDED_RIGHT
);
if (collisions) {
break;
}
posY++;
}
// out of field
if (
collisions&Field::COLLIDED_LEFT
|| collisions&Field::COLLIDED_RIGHT
) {
return 0;
}
m_piece.resetBlocksEnum();
while (m_piece.iterateBlocksEnum(&x, &y)) {
int blockX = posX + x - PIECE_SIZE/2;
int blockY = posY + y - PIECE_SIZE/2;
if (
blockX == 0
|| blockX == FIELD_WIDTH - 1
) {
joins++;
}
// touches the heap with bottom
if (
blockY == FIELD_HEIGHT - 1
|| m_field->checkNotBlank(blockX, blockY + 1)
) {
joins++;
}
// no blocks from below - bad
if (
blockY < FIELD_HEIGHT - 1
&& !m_field->checkNotBlank(blockX, blockY + 1)
&& (
y >= PIECE_SIZE - 1
|| !m_piece.checkHasBlock(x, y+1)
)
) {
blanks++;
}
// touches the heap with left side
if (
blockX > 0
&& m_field->checkNotBlank(blockX - 1, blockY)
) {
joins++;
}
// touches the heap with right side
if (
blockX < FIELD_WIDTH - 1
&& m_field->checkNotBlank(blockX + 1, blockY)
) {
joins++;
}
}
// we don't like blanks twice stronger than we like joins
// and we prefer to put pieces as low we can
return (joins - blanks * 2) * posY;
}