summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AIController.cpp40
-rw-r--r--AIController.h32
-rw-r--r--Controller.cpp7
-rw-r--r--Controller.h17
-rw-r--r--Game.cpp2
-rw-r--r--KeyboardController.cpp2
-rw-r--r--KeyboardController.h2
-rw-r--r--Makefile2
-rw-r--r--Player.cpp8
-rw-r--r--Player.h3
-rw-r--r--TrollTron.cpp2
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
diff --git a/Game.cpp b/Game.cpp
index 2357845..2118401 100644
--- a/Game.cpp
+++ b/Game.cpp
@@ -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);
};
diff --git a/Makefile b/Makefile
index be57326..7217c9d 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/Player.cpp b/Player.cpp
index 1886139..36f32c6 100644
--- a/Player.cpp
+++ b/Player.cpp
@@ -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);
+}
diff --git a/Player.h b/Player.h
index 0b31d59..3dd4e64 100644
--- a/Player.h
+++ b/Player.h
@@ -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);