-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolvestate.cpp
150 lines (119 loc) · 3.76 KB
/
solvestate.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
#include "settings.h"
#include "solvestate.h"
SolveState::SolveState() { }
SolveState::SolveState(Board b, int x, int y): board(b)
{
startX = fingerX = x;
startY = fingerY = y;
calculateScore();
}
void SolveState::addNewStates(std::vector<SolveState> &states)
{
SolveState newState;
// when making a move, check
// that the move doesn't "undo" the immediate previous move
// and we are not at an edge of a board and unable to further move
// move left
if((moves.size() == 0 || moves.back() != RIGHT) && fingerX-1 >= 0) {
newState = *this;
newState.board.swapOrbs(fingerX, fingerY, fingerX-1, fingerY);
newState.moves.push_back(LEFT);
newState.fingerX = fingerX - 1;
newState.board.setFinishLocations();
newState.calculateScore();
if(newState.score - score >= SOLUTION_THRESHOLD)
states.push_back(newState);
}
// move right
if((moves.size() == 0 || moves.back() != LEFT) && fingerX+1 < BOARD_X) {
newState = *this;
newState.board.swapOrbs(fingerX, fingerY, fingerX+1, fingerY);
newState.moves.push_back(RIGHT);
newState.fingerX = fingerX + 1;
newState.board.setFinishLocations();
newState.calculateScore();
if(newState.score - score >= SOLUTION_THRESHOLD)
states.push_back(newState);
}
// move up
if((moves.size() == 0 || moves.back() != DOWN) && fingerY-1 >= 0) {
newState = *this;
newState.board.swapOrbs(fingerX, fingerY, fingerX, fingerY-1);
newState.moves.push_back(UP);
newState.fingerY = fingerY - 1;
newState.board.setFinishLocations();
newState.calculateScore();
if(newState.score - score >= SOLUTION_THRESHOLD)
states.push_back(newState);
}
// move down
if((moves.size() == 0 || moves.back() != UP) && fingerY+1 < BOARD_Y) {
newState = *this;
newState.board.swapOrbs(fingerX, fingerY, fingerX, fingerY+1);
newState.moves.push_back(DOWN);
newState.fingerY = fingerY + 1;
newState.board.setFinishLocations();
newState.calculateScore();
if(newState.score - score >= SOLUTION_THRESHOLD)
states.push_back(newState);
}
}
void SolveState::calculateScore()
{
board.evaluateBoard();
if(moves.size() < CURVE_POINT)
score = board.score;
else
score = board.score * (1 - ((double)moves.size() - CURVE_POINT)/CURVE_FACTOR);
score -= (double)moves.size() / 100;
}
/*
void SolveState::getStartLocation(int *x, int *y)
{
int fx, fy;
fx = fingerX;
fy = fingerY;
// move backwards
for(int i = moves.size() - 1; i >= 0; i--) {
if(moves[i] == LEFT) fx++;
if(moves[i] == RIGHT) fx--;
if(moves[i] == UP) fy++;
if(moves[i] == DOWN) fy--;
}
*x = fx;
*y = fy;
}
*/
bool operator<(SolveState const& a, SolveState const& b)
{
return a.score > b.score;
}
std::ostream &operator<<(std::ostream &os, SolveState & s)
{
int i, j;
int n_moves = 0;
os << "Solution found (score = " << s.score << ")\n";
os << "Start here:\n";
for(j=0; j<BOARD_Y; j++) {
for(i=0; i<BOARD_X; i++) {
if(i == s.startX && j == s.startY)
os << "X";
else
os << ".";
}
os << "\n";
}
os << s.moves.size() << " moves: ";
for(auto &iter : s.moves) {
if(iter == RIGHT) os << "R";
if(iter == UP) os << "U";
if(iter == LEFT) os << "L";
if(iter == DOWN) os << "D";
if(n_moves%5 == 4) os << " ";
n_moves++;
}
os << "\n";
os << "Combos: " << s.board.combos << "\n";
os << "Cascades: " << s.board.cascades << "\n";
return os << s.board;
}