-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.cpp
158 lines (132 loc) · 4 KB
/
game.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
/*
* Project name: Food collection
* Version 4
* Student : Albert Eduard Merino Pulido
*/
#include "game.h"
using namespace std;
// Constructors
Game::Game(){ }
Game::Game(int height, int width, float seed, StrategyType strategyTypePlayer, StrategyType strategyTypeEnemy)
: height(height), width(width), seed(seed), strategyTypePlayer(strategyTypePlayer),
strategyTypeEnemy(strategyTypeEnemy), map(NULL){
player = new Agent(PLAYER, getStrategyByType(PLAYER, strategyTypePlayer));
enemy = new Agent(ENEMY, getStrategyByType(ENEMY, strategyTypeEnemy));
player->setAgent(enemy);
enemy->setAgent(player);
}
// Getters
int Game::getHeight(){ return height; }
int Game::getWidth(){ return width; }
bool Game::isPaused(){ return pause; }
void Game::draw(){
player->draw();
enemy->draw();
vector<vector<Cell *> > m = map->getMap();
for (int i = 0; i < map->getHeight(); i++)
for (int j = 0; j < map->getWidth(); j++)
m[i][j]->draw();
}
void Game::drawText(const char * title, bool isConnected){
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, width, 0.0, height);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
Drawer& drawer = Drawer::getInstance();
drawer.printTitle(title);
drawer.printScore(player->getScore(), enemy->getScore());
drawer.printLevel(level);
drawer.printVelocity(Agent::agentVelocity);
drawer.printFood(map->getFoodRemaining());
drawer.printArduino(isConnected);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
void Game::resetGame(){
level = 0;
pause = false;
newGame();
}
void Game::newGame(){
newMap();
level += 1;
player->setMap(map);
enemy->setMap(map);
enemy->registerInitialState();
std::cout << map->toString();
}
void Game::pauseGame(){
pause = !pause;
}
void Game::integrate(long t){
if (!pause) {
integrate(player, t);
integrate(enemy, t);
}
}
void Game::integrate(Agent * agent, long t){
if (agent->integrate(t) || agent->isQuiet()) {
if (map->hasFood()) {
if (agent->getNeedAction()) {
agent->setPosition(agent->getNextPosition());
Direction d = agent->getAction();
agent->setNextDirection(d);
agent->tryNextDirection();
}
agent->move();
} else {
finish();
}
}
} // integrate
void Game::finish(){
enemy->final(*map);
bool playerWins = playerWin();
showGameResults(playerWins);
if (playerWins) newGame();
else resetGame();
}
bool Game::playerWin(){ return player->getScore() > enemy->getScore(); }
void Game::showGameResults(bool playerWins){
if (playerWins) cout << "Player Wins!" << endl;
else cout << "Enemy Wins!" << endl;
cout << "Player: " << player->getScore() << std::endl;
cout << "Enemy: " << enemy->getScore() << std::endl;
cout << "Level: " << level << std::endl;
cout << std::endl << "**********************" << std::endl << std::endl;
}
void Game::moveAgent(CellType cellType, Direction direction){
if (cellType == PLAYER) {
if (player->isQuiet()) player->setDirection(direction);
else player->setNextDirection(direction);
}
}
void Game::shoot(CellType cellType){
if (cellType == PLAYER) {
player->shoot();
}
}
void Game::newMap(){
if (map != NULL) delete map;
map = new Map(height, width);
if (seed != -1) map->setSeed(seed);
map->generate();
}
Strategy * Game::getStrategyByType(CellType agent, StrategyType strategyType){
switch (strategyType) {
case REFLEX_AGENT:
return new ReflexAgent(map, agent);
case EXPECTIMAX_AGENT:
return new ExpectimaxAgent(map, agent, 4);
case REINFORCEMENT_AGENT:
return new ApproximateQAgent(map, agent);
default:
return new Strategy(map, agent);
}
}