Repository navigation
Expand file tree
/
Copy pathMain.cpp
More file actions
299 lines (263 loc) · 9.38 KB
/
Copy pathMain.cpp
File metadata and controls
299 lines (263 loc) · 9.38 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/** @file Main.cpp
* \brief Main public API of the Hamster simulator.
*/
#include <stdarg.h>
#include <stdio.h>
#include "Main.hpp"
using namespace de::unistuttgart::iste::rss::oo::hamstersimulator::external::model;
using namespace de::unistuttgart::iste::rss::oo::hamstersimulator::datatypes;
using namespace de::unistuttgart::iste::rss::oo::hamstersimulator::internal::model;
using namespace net::sourceforge::jnipp;
using namespace std;
/** \brief Initializes the Hamster Simulator, sets up a default territory, and places Paule in it.
*
* This function initalizes the hamster game, sets up a default territory, and places Paule in it.
* The function is useful for early exercises. In addition the UI of the Hamster Simulator is started and
* displayed. After init has executed, you are ready to issue Hamster commands or queries.
*/
void init() {
try
{
JVM::load();
setupHamsterGame("/territories/example01.ter");
TerritoryProxy territory = hamsterGame->getTerritory();
paule = new HamsterProxy();
*paule = territory.getDefaultHamster();
hamsterGame->startGame(false);
}
catch(BaseException& ex)
{
cerr << "caught exception: " << ex.getMessage().c_str() << endl;
exit(-1);
}
}
HamsterSpec addHamster(int row, int column, int direction, int grainCount) {
const char *directions[] = {"WEST","NORTH","EAST","SOUTH"};
HamsterProxy *newHamster = new HamsterProxy();
newHamster->init(hamsterGame->getTerritory(), LocationProxy::from(row, column), DirectionProxy::valueOf(directions[direction]), grainCount);
HamsterSpec result;
result.referencedHamster = newHamster;
return result;
}
void releaseHamster(HamsterSpec hamster) {
if (hamsterGame) {
delete hamster.referencedHamster;
}
hamster.referencedHamster = nullptr;
}
/** \brief Initializes the Hamster Simulator, sets up the provided territory, and places Paule in it.
*
* This function initalizes the hamster game, sets up the provided territory, and places Paule in it.
* The function is useful for early exercises. In addition the UI of the Hamster Simulator is started and
* displayed. After init has executed, you are ready to issue Hamster commands or queries.
*/
void init(const char* filename) {
try
{
JVM::load();
setupHamsterGame(filename);
TerritoryProxy territory = hamsterGame->getTerritory();
paule = new HamsterProxy();
*paule = territory.getDefaultHamster();
hamsterGame->startGame(false);
}
catch(BaseException& ex)
{
cerr << "caught exception: " << ex.getMessage().c_str() << endl;
exit(-1);
}
}
/// @private
void setupHamsterGame(const char* filename) {
hamsterGame = new HamsterGameProxy();
hamsterGame->initialize(filename);
jclass fxClass = JNIEnvHelper::FindClass(fxClassName.c_str());
jmethodID jfxStart = JNIEnvHelper::GetStaticMethodID(fxClass , "start", "()V" );
jmethodID jfxIf = JNIEnvHelper::GetStaticMethodID(fxClass , "getJavaFXInputInterface", "()Lde/unistuttgart/iste/rss/oo/hamstersimulator/internal/model/InputInterface;" );
JNIEnvHelper::CallStaticVoidMethod(fxClass, jfxStart);
jobject fxIf = JNIEnvHelper::CallStaticObjectMethod(fxClass, jfxIf);
hamsterGame->displayInNewGameWindow();
InputInterfaceProxy proxyIf(fxIf);
hamsterGame->setInputInterface(proxyIf);
}
/** \brief Shuts down the hamster game and terminates the hamster simulator core
*
* This function stops the running hamster game and shuts down the core hamster simulator engine.
* The UI stays active and the game history can still be tracked.
*/
void deinit() {
try
{
hamsterGame->stopGame();
delete paule;
delete hamsterGame;
paule = nullptr;
hamsterGame = nullptr;
JVM::unload();
}
catch(BaseException& ex)
{
cerr << "caught exception: " << ex.getMessage().c_str() << endl;
exit(-1);
}
}
/** \brief Moves the hamster a single steps towards its looking direction
*
* This function moves the hamster a single tile forward in its looking direction. The move
* is only allowed if the tile in front of the hamster exists (i.e., the hamster is not allowed
* to leave the gamefield) and it is not blocked by a wall. An attempt to make the move
* nevertheless results in an exception.
*/
void move() {
paule->move();
}
void move(HamsterSpec hamster) {
hamster.referencedHamster->move();
}
/** \brief Rotates the hamster by 90 degrees counterclockwise.
*
* This function changes the hamster's looking direction by rotating the hamster 90 degrees counterclockwise.
*/
void turnLeft() {
paule->turnLeft();
}
void turnLeft(HamsterSpec hamster) {
hamster.referencedHamster->turnLeft();
}
/** \brief Picks up a grain from the hamster's current tile.
*
* This function transfers a grain from the hamster's current tile into the mouth of the hamster. The function
* can only be called if there is a gain on the current tile. If not, the program raises an exception.
*/
void pickGrain() {
paule->pickGrain();
}
void pickGrain(HamsterSpec hamster) {
hamster.referencedHamster->pickGrain();
}
/** \brief Puts a grain from the hamster's mouth on the hamster's current tile.
*
* This function transfers a grain from the hamster's mouth to the hamster's current tile. The function
* can only be called if there is a gain in the hamster's mouth. If not, the program raises an exception.
*/
void putGrain() {
paule->putGrain();
}
void putGrain(HamsterSpec hamster) {
hamster.referencedHamster->putGrain();
}
/** \brief Reads a number from the user by prompting the user using the provided message
*
* This function asks the user to enter a valid number. The passed message is used as
* message prompt.
* \param message The message used when prompting for the user's input.
* \result A pointer to an integer variable used to store the result.
*/
int readNumber(const char* message) {
return paule->readNumber(message);
}
int readNumber(HamsterSpec hamster, const char* message) {
return hamster.referencedHamster->readNumber(message);
}
/** \brief Reads a string from the user by prompting the user using the provided message
*
* This function asks the user to enter a valid message string. The passed message is used as
* message prompt.
* \param message The message used when prompting for the user's input.
* \param result A pointer to a character array used to store the result. The array has to have
* enough memory allocated to store the provided string.
*/
void readString(const char* message, char* result) {
string resultS = paule->readString(message);
strcpy(result, resultS.c_str());
}
void readString(HamsterSpec hamster, const char* message, char* result) {
string resultS = hamster.referencedHamster->readString(message);
strcpy(result, resultS.c_str());
}
/** \brief Prints the given message on the hamster's log
*
* This function prints the provided message on the hamster's log.
* \param message The message to print to the log. Is not allowed to be null.
*
*/
void write(const char* message) {
paule->write(message);
}
void write(HamsterSpec hamster, const char* message) {
hamster.referencedHamster->write(message);
}
/** \brief Gets the row number of the current hamster position
*
* This function gets the row count of the current hamster position
*/
int getRow() {
LocationProxy location = paule->getLocation();
return location.getRow();
}
/** \brief Gets the column number of the current hamster position
*
* This function gets the column count of the current hamster position
*/
int getColumn() {
LocationProxy location = paule->getLocation();
return location.getColumn();
}
/** \brief Returns true if the tile in front of the hmaster is clear
*
* \result true if the tile in front of the hamster is clear
*/
bool frontIsClear() {
return paule->frontIsClear();
}
bool frontIsClear(HamsterSpec hamster) {
return hamster.referencedHamster->frontIsClear();
}
/** \brief Returns true if the hamster has no grains in its mouth
*
* \result true if the hamster's mouth is empty
*/
bool mouthEmpty() {
return paule->mouthEmpty();
}
bool mouthEmpty(HamsterSpec hamster) {
return hamster.referencedHamster->mouthEmpty();
}
/** \brief Returns true if there are grains availabe to pick up on the hamster's current tile
*
* \result true if the hamster's current tile contains at least one grain
*/
bool grainAvailable() {
return paule->grainAvailable();
}
bool grainAvailable(HamsterSpec hamster) {
return hamster.referencedHamster->grainAvailable();
}
/** \brief Lets the hamster say a message formated with the same format string used in printf
*
* Lets the hamster say a message formated with the same format string used in printf. The number
* of arguments has to match the number of format placeholders used. It is also not necessary to
* use a newline ('\\n') at the end.
*
* Find the documentation of the printf
* function under http://www.cplusplus.com/reference/cstdio/printf/
*/
void writef(const char* message_template, ... ) {
char buffer[1000];
va_list args;
va_start(args, message_template);
vsnprintf(buffer, 1000, message_template, args);
write(buffer);
va_end(args);
}
void writef(HamsterSpec hamster, const char* message_template, ... ) {
char buffer[1000];
va_list args;
va_start(args, message_template);
vsnprintf(buffer, 1000, message_template, args);
write(hamster, buffer);
va_end(args);
}
bool gameIsActive() {
return hamsterGame != nullptr;
}