diff options
-rw-r--r-- | AIController.cpp | 40 | ||||
-rw-r--r-- | AIController.h | 32 | ||||
-rw-r--r-- | Controller.cpp | 7 | ||||
-rw-r--r-- | Controller.h | 17 | ||||
-rw-r--r-- | Game.cpp | 2 | ||||
-rw-r--r-- | KeyboardController.cpp | 2 | ||||
-rw-r--r-- | KeyboardController.h | 2 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | Player.cpp | 8 | ||||
-rw-r--r-- | Player.h | 3 | ||||
-rw-r--r-- | TrollTron.cpp | 2 |
11 files changed, 100 insertions, 17 deletions
diff --git a/AIController.cpp b/AIController.cpp new file mode 100644 index 0000000..fd74e63 --- /dev/null +++ b/AIController.cpp @@ -0,0 +1,40 @@ +#include "AIController.h" + + +AIController::AIController(Player &player, Field& field, int inhibitSpeed): + Controller(player), m_field(field), m_inhibitSpeed(inhibitSpeed) +{ +} + +AIController::~AIController(void) +{ +} + + +void AIController::think() { + Coordinates coord = m_player.getCoordinates(); + VelocityVector velocity = m_player.getVelocity(); + + int sgn_x = velocity.x >= 0 ? 1 : -1; + int sgn_y = velocity.y >= 0 ? 1 : -1; + + for (int i = 0; i <= abs(velocity.x) ; ++i ) { + for (int j = 0; j <= abs(velocity.y) ; ++j ) { + if (m_field.getCell(Coordinates(coord.x+i*sgn_x, coord.y+j*sgn_y)).getState()) { + turnRandom(); + } + } + } + if (rand() % 100 >=7) turnRandom(); + m_inhibit = max(0,m_inhibit-1); +} + +void AIController::turnRandom() { + if (m_inhibit == 0) { + if (rand()%100>=50) + m_player.turnLeft(); + else + m_player.turnRight(); + m_inhibit = m_inhibitSpeed; + } +} diff --git a/AIController.h b/AIController.h new file mode 100644 index 0000000..4a647a7 --- /dev/null +++ b/AIController.h @@ -0,0 +1,32 @@ +#ifndef _AI_CONTROLLER_H
+#define _AI_CONTROLLER_H
+
+#include "Controller.h"
+
+#ifndef max
+ #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
+#endif
+
+#ifndef min
+ #define min( a, b ) ( ((a) < (b)) ? (a) : (b) )
+#endif
+
+class AIController : public Controller
+{
+
+public:
+ AIController(Player& player, Field& field, int inhibitSpeed);
+ virtual ~AIController(void);
+
+ void think();
+
+private:
+ void turnRandom();
+
+ Field& m_field;
+ int m_inhibit;
+ int m_inhibitSpeed;
+};
+
+
+#endif
diff --git a/Controller.cpp b/Controller.cpp index 8fa14ab..f3b4cb5 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -10,7 +10,8 @@ Controller::~Controller(void) {
}
-//void Controller::changeDirection(VelocityVector v) = 0;
-bool Controller::changeDirection(VelocityVector v){
- return(false);
+Player& Controller::getPlayer() {
+ return(m_player);
}
+
+
diff --git a/Controller.h b/Controller.h index 95a9e28..5f63d82 100644 --- a/Controller.h +++ b/Controller.h @@ -7,22 +7,19 @@ class Controller
{
public:
- Controller(Player &player);
+ Controller(Player& player);
virtual ~Controller(void);
-public:
- Player &getPlayer();
+ Player& getPlayer();
+
/**
- * This function will be useful if an IA controller needs to analyse the world
- * in order to take decisions.
+ * This function will be called on every turn and should update
+ * m_player directly in order to take decisions.
**/
- void think();
- //Game &getGame();
-protected:
- virtual bool changeDirection(VelocityVector v);
+ virtual void think() = 0;
protected:
- Player &m_player;
+ Player& m_player;
};
#endif
@@ -67,7 +67,7 @@ void Game::resumeGame() { // main function to be called within glut loop
void Game::updateGame(uint timeElapsedMs) {
- /* // ask the AI if they wanna change something?
+ /*// ask the AI if they wanna change something?
for(std::vector<Player>::iterator player = m_vPlayers.begin(); player != m_vPlayers.end(); ++player) {
if (player->getController()) {
player->getController()->think();
diff --git a/KeyboardController.cpp b/KeyboardController.cpp index 1a5c91e..dc7155d 100644 --- a/KeyboardController.cpp +++ b/KeyboardController.cpp @@ -1,7 +1,7 @@ #include "KeyboardController.h"
-KeyboardController::KeyboardController(Player &player):
+KeyboardController::KeyboardController(Player& player):
Controller(player)
{
}
diff --git a/KeyboardController.h b/KeyboardController.h index 272d1a5..7ea7ce8 100644 --- a/KeyboardController.h +++ b/KeyboardController.h @@ -11,7 +11,7 @@ class KeyboardController : friend class MainWindow;
public:
- KeyboardController(Player &player);
+ KeyboardController(Player& player);
virtual ~KeyboardController(void);
};
@@ -16,7 +16,7 @@ else LDFLAGS=-lGLU -lglut endif -SOURCES=Cell.cpp Field.cpp Controller.cpp Game.cpp MainWindow.cpp KeyboardController.cpp Player.cpp +SOURCES=Cell.cpp Field.cpp Controller.cpp Game.cpp MainWindow.cpp KeyboardController.cpp Player.cpp AIController.cpp OBJECTS=$(SOURCES:.cpp=.o) MAIN_SOURCE=TrollTron.cpp MAIN=trolltron @@ -68,3 +68,11 @@ PlayerNumber Player::getNumber() { Controller* Player::getController() {
return(m_controller);
}
+
+VelocityVector Player::getVelocity() {
+ return(m_velocityVector);
+}
+
+Coordinates Player::getCoordinates() {
+ return(m_currentCoordinates);
+}
@@ -31,6 +31,9 @@ public: void turnLeft();
void turnRight();
+ VelocityVector getVelocity();
+ Coordinates getCoordinates();
+
bool isAlive();
void kill();
diff --git a/TrollTron.cpp b/TrollTron.cpp index 56e1cc6..80df411 100644 --- a/TrollTron.cpp +++ b/TrollTron.cpp @@ -11,6 +11,8 @@ int main(int argc, char **argv) { // This will define the size and position, in pixel, of the window, originally. It can be moved and resized.
mainWindow.create("Troll Tron", 100, 100, 320, 320);
+ srand(time(NULL));
+
// This will define the grid
Game game(200, 200, 3);
|