diff options
author | Dominique Martinet <asmadeus@codewreck.org> | 2017-06-25 02:53:14 +0200 |
---|---|---|
committer | Dominique Martinet <asmadeus@codewreck.org> | 2017-06-25 02:53:14 +0200 |
commit | 21ec35461b30a417201415370cf07ca13273a0df (patch) | |
tree | 04c796d08433b10d01feae9afae80301914869fd | |
parent | 65bbf60e913cc148bd0d36aaf08c26374d08e942 (diff) |
-rw-r--r-- | AIController.cpp | 10 | ||||
-rw-r--r-- | Field.cpp | 2 | ||||
-rw-r--r-- | Game.cpp | 7 | ||||
-rw-r--r-- | KeyboardController.h | 16 | ||||
-rw-r--r-- | MainWindow.cpp | 8 | ||||
-rw-r--r-- | Player.cpp | 4 | ||||
-rw-r--r-- | TrollTron.cpp | 27 |
7 files changed, 34 insertions, 40 deletions
diff --git a/AIController.cpp b/AIController.cpp index 629a17b..149487a 100644 --- a/AIController.cpp +++ b/AIController.cpp @@ -1,6 +1,6 @@ #include <stdio.h> #include "AIController.h" - + AIController::AIController(Player *pPlayer, Field& field, int inhibitDelay, int randomThreshold): Controller(pPlayer), m_field(field), m_inhibitDelay(inhibitDelay), m_randomThreshold(randomThreshold) @@ -28,7 +28,7 @@ void AIController::think() { try { if (m_field.getCell(Coordinates(coord.x+i*sgn_x, coord.y+i*sgn_y)).getState()) { turnRandom(); - } + } } catch (std::out_of_range e) { turnRandom(); } @@ -55,19 +55,19 @@ void AIController::turnRandom() { while (1) { if (m_field.getCell(Coordinates(coord.x+freeSpaceLeft*sgn_x, coord.y+freeSpaceLeft*sgn_y)).getState()) break; freeSpaceLeft++; - } + } } catch (std::out_of_range e) {} try { while (1) { if (m_field.getCell(Coordinates(coord.x-freeSpaceRight*sgn_x, coord.y-freeSpaceRight*sgn_y)).getState()) break; freeSpaceRight++; - } + } } catch (std::out_of_range e) {} if (max(freeSpaceRight,freeSpaceLeft) > m_inhibitDelay*max(abs(leftVelocity.x),abs(leftVelocity.y))+1) { (freeSpaceLeft > freeSpaceRight) ? m_pPlayer->turnLeft() : m_pPlayer->turnRight(); m_inhibit = m_inhibitDelay; - } + } } } @@ -25,7 +25,7 @@ Cell& Field::getCell(Coordinates c) { }
void Field::init(uint width, uint height) {
- // m_fieldCells.assign(width*height, EMPTY_CELL);
+ // m_fieldCells.assign(width*height, EMPTY_CELL);
m_width = width;
m_height = height;
m_fieldCells.clear();
@@ -125,12 +125,14 @@ bool Game::isRunning() { // main function to be called within glut loop
void Game::updateGame(uint timeElapsedMs) {
- if (m_gameRunning) {
+ if (!m_gameRunning)
+ return;
+
// ask the AI if they wanna change something?
for(std::vector<Player*>::iterator it = m_vpPlayers.begin(); it != m_vpPlayers.end(); ++it) {
if ((*it)->isAlive()) {
if ((*it)->getController()) {
- (*it)->getController()->think();
+ (*it)->getController()->think();
}
}
}
@@ -188,7 +190,6 @@ void Game::updateGame(uint timeElapsedMs) { }
} catch (std::exception out_of_bounds) {} // don't do anything
}
- }
}
void Game::killPlayer(Player *player)
diff --git a/KeyboardController.h b/KeyboardController.h index bce2b16..0a19165 100644 --- a/KeyboardController.h +++ b/KeyboardController.h @@ -7,16 +7,16 @@ // Compatible with glut values
typedef enum
{
- KEY_A, KEY_B, KEY_C, KEY_D, KEY_E,
- KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
- KEY_K, KEY_L, KEY_M, KEY_N, KEY_O,
- KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
- KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y,
+ KEY_A, KEY_B, KEY_C, KEY_D, KEY_E,
+ KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
+ KEY_K, KEY_L, KEY_M, KEY_N, KEY_O,
+ KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
+ KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y,
KEY_Z,
- KEY_0, KEY_1, KEY_2, KEY_3,
- KEY_4, KEY_5, KEY_6, KEY_7, KEY_8,
+ KEY_0, KEY_1, KEY_2, KEY_3,
+ KEY_4, KEY_5, KEY_6, KEY_7, KEY_8,
KEY_9,
- KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
+ KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
} KeyCode;
class MainWindow;
diff --git a/MainWindow.cpp b/MainWindow.cpp index 5abdc3d..bafcf7c 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -53,10 +53,10 @@ void MainWindow::drawString(GLfloat x, GLfloat y, char *text, uint color, TextAl hex2fColor(color, red, green, blue); - glLineWidth(3.0); - glPushMatrix(); - glTranslatef(x, y, 0); - glScalef(0.001, 0.001, 0.00); + glLineWidth(3.0); + glPushMatrix(); + glTranslatef(x, y, 0); + glScalef(0.001, 0.001, 0.00); if (align) glTranslatef( ((float) align / 2) * glutStrokeLength(GLUT_STROKE_MONO_ROMAN, (const unsigned char *) text), 0, 0); glColor3f(red, green, blue); @@ -48,8 +48,8 @@ std::vector<Coordinates> Player::move() { cases.push_back(Coordinates(m_currentCoordinates.x+i*sgn_x, m_currentCoordinates.y+j*sgn_y));
}
}
- m_currentCoordinates.x += m_velocityVector.x;
- m_currentCoordinates.y += m_velocityVector.y;
+ m_currentCoordinates.x += m_velocityVector.x;
+ m_currentCoordinates.y += m_velocityVector.y;
return(cases);
}
diff --git a/TrollTron.cpp b/TrollTron.cpp index a8af7d7..8579cbd 100644 --- a/TrollTron.cpp +++ b/TrollTron.cpp @@ -70,44 +70,37 @@ int main(int argc, char **argv) { pPlayer = game.addPlayer();
// And create its controller
- pPlayer->setController(mainWindow.generateNewKeyboardController(pPlayer,
- keys[currNumHumanPlayers*2],
- keys[currNumHumanPlayers*2+1]));
+ pPlayer->setController(mainWindow.generateNewKeyboardController(pPlayer,
+ keys[currNumHumanPlayers*2],
+ keys[currNumHumanPlayers*2+1]));
// Count 1 more (human) player
currNumPlayers ++;
currNumHumanPlayers ++;
- }
+ } else argFail = true;
}
else
{
- bool isABot = false;
- // If it's "easy"
if(! strcmp(argv[argNum], "easy"))
{
- isABot = true;
botInhibitDelay = 10;
botRandomTurn = 10;
}
else if(! strcmp(argv[argNum], "medium"))
{
- isABot = true;
botInhibitDelay = 3;
botRandomTurn = 6;
}
else if(! strcmp(argv[argNum], "hard"))
{
- isABot = true;
botInhibitDelay = 1;
botRandomTurn = 3;
} else argFail = true;
- if(isABot)
- {
- // Count 1 more (non human) player
- currNumPlayers ++;
- pPlayer = game.addPlayer();
- // And instanciate its AI
- pPlayer->setController(new AIController(pPlayer, game.getField(), botInhibitDelay, botRandomTurn));
- }
+
+ // Count 1 more (non human) player
+ currNumPlayers ++;
+ pPlayer = game.addPlayer();
+ // And instanciate its AI
+ pPlayer->setController(new AIController(pPlayer, game.getField(), botInhibitDelay, botRandomTurn));
}
}
|