-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game6561.cpp
136 lines (124 loc) · 3.8 KB
/
Game6561.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
#include "Game6561.h"
#include "GameException.h"
Game6561::Game6561(std::istream& istream, std::ostream& ostream, std::ostream& logStream, Algorithm& algorithm) :
istream(istream), ostream(ostream), logStream(logStream), algorithm(algorithm) {
algorithm.setGameBoardPtr(&board);
algorithm.setGameGameProgressPtr(&gameProgress);
}
void Game6561::run() {
readNextLine();
if (nextLine[0] == 'A') {
doMove();
#ifdef DEBUG_BUILD
printBoard();
#endif
}
while (readNextLine()) {
parseNextLine();
#ifdef DEBUG_BUILD
printBoard();
#endif
doMove();
#ifdef DEBUG_BUILD
printBoard();
#endif
}
}
bool Game6561::readNextLine() {
std::getline(istream, nextLine);
return nextLine != "Quit";
}
void Game6561::parseNextLine() {
switch (board.getGameRhythmState()) {
case GR_BLUE:
readPiece(BLUE);
break;
case GR_RED:
readPiece(RED);
break;
case GR_GREY:
readPiece(GREY);
break;
case GR_SLIDE1:
case GR_SLIDE2:
readSlide();
break;
}
}
void Game6561::readPiece(PieceColor color) {
const Coords& coords = readCoords();
board.setPiece(coords, color);
gameProgress.updateLastReadMoveAndMoveCounter(coords);
}
void Game6561::readSlide() {
switch (nextLine[0]) {
case 'U':
board.slideUp();
gameProgress.updateLastReadMoveAndMoveCounter(SD_UP);
break;
case 'D':
board.slideDown();
gameProgress.updateLastReadMoveAndMoveCounter(SD_DOWN);
break;
case 'L':
board.slideLeft();
gameProgress.updateLastReadMoveAndMoveCounter(SD_LEFT);
break;
case 'R':
board.slideRight();
gameProgress.updateLastReadMoveAndMoveCounter(SD_RIGHT);
break;
default:
break;
}
}
const Coords Game6561::readCoords() {
coord row = (coord) (nextLine[0] - '1');
coord column = (coord) (nextLine[1] - '1');
return Coords(row, column);
}
void Game6561::doMove() {
if (gameProgress.moveCounter >= MAX_MOVES) {
logStream << "Max moves reached: " << std::endl;
return;
}
try {
Move lastDoneMove;
algorithm.ensureValidState();
switch (board.getGameRhythmState()) {
case GR_BLUE:
lastDoneMove.coords = algorithm.calculateBlueMove();
board.setPiece(lastDoneMove.coords, BLUE);
writeCoords(lastDoneMove.coords);
break;
case GR_RED:
lastDoneMove.coords = algorithm.calculateRedMove();
board.setPiece(lastDoneMove.coords, RED);
writeCoords(lastDoneMove.coords);
break;
case GR_GREY:
lastDoneMove.coords = algorithm.calculateGreyMove();
board.setPiece(lastDoneMove.coords, GREY);
writeCoords(lastDoneMove.coords);
break;
case GR_SLIDE1:
case GR_SLIDE2:
lastDoneMove.direction = algorithm.calculateSlide();
board.slide(lastDoneMove.direction);
writeSlide(lastDoneMove.direction);
break;
}
gameProgress.updateLastDoneMoveAndMoveCounter(lastDoneMove);
} catch (const GameException& e) {
logStream << "Impossible move detected: " << e.what() << std::endl;
}
}
void Game6561::writeCoords(const Coords& coords) {
ostream << coords.row + 1 << coords.column + 1 << std::endl;
}
void Game6561::writeSlide(const SlideDirection& slideDirection) {
ostream << slideDirection << std::endl;
}
void Game6561::printBoard() {
logStream << gameProgress.moveCounter << ": " << board << std::endl;
}