diff --git a/source/game/game.cpp b/source/game/game.cpp index fca3376..45c2646 100644 --- a/source/game/game.cpp +++ b/source/game/game.cpp @@ -92,7 +92,7 @@ int CTTRTSGame::IssueOrder( player_t player, const SOrder & order ) } // Verify a position -int CTTRTSGame::VerifyPos(uvector2 vec) const +int CTTRTSGame::VerifyPosIsValidMovement(uvector2 vec) const { // Simply check if within the bounds of our dimensions for now if ( ( vec.x >= dimensions.x ) @@ -137,7 +137,7 @@ int CTTRTSGame::SimulateToNextTurn() uvector2 newpos = GetNewPosition(pair); // Verify the position is even available - bool possible = ( VerifyPos(newpos) == 0 ); + bool possible = (VerifyPosIsValidMovement(newpos) == 0 ); if ( possible ) { @@ -202,7 +202,7 @@ int CTTRTSGame::SimulateToNextTurn() { uvector2 newpos = pair.unit.GetInFront(); // If move would be within the arena - if ( ( newpos.x <= dimensions.x-1 ) && ( newpos.y <= dimensions.y-1 ) ) + if (VerifyPosIsValidMovement(newpos) == 0 ) { pair.unit.SetPos(newpos); @@ -454,7 +454,7 @@ std::vector CTTRTSGame::GetPlayers() const } // Check if we have a win state -player_t CTTRTSGame::CheckForWin() const +player_t CTTRTSGame::GetWinningPlayer() const { // Array of units for each Player unsigned int units[(int) player_t::NUM_INVALID]; diff --git a/source/game/game.h b/source/game/game.h index 99896ea..061ccc5 100644 --- a/source/game/game.h +++ b/source/game/game.h @@ -30,10 +30,16 @@ public: // Returns non-zero if simulation failed int SimulateToNextTurn(); - // Check for a win, returns invalid for no win state reached - // Note: this function will return invalid a draw was reached - // best practice would be to call with GetNumUnits() == 0 - player_t CheckForWin() const; + // Check for winning player, returns invalid for no win state reached + // Note: this function will return invalid if a draw was reached + // do not rely on this to test for end state + player_t GetWinningPlayer() const; + + // Check if the game is over + bool GameOver() const; + + // Check if any of the units can move + bool UnitsCanMove() const; // Get the game information as a string std::string GetStateAsString() const; @@ -80,7 +86,7 @@ private: // Verify any order or position - non-zero is error int VerifyOrder( player_t player, const SOrder & order ) const; - int VerifyPos( uvector2 vec ) const; + int VerifyPosIsValidMovement(uvector2 vec) const; // Get a units new position after an order uvector2 GetNewPosition( const SOrderUnitPair & pair ) const; diff --git a/source/test/test.cpp b/source/test/test.cpp index b4d197c..60d9028 100644 --- a/source/test/test.cpp +++ b/source/test/test.cpp @@ -172,7 +172,7 @@ const char* tests() if (game.GetUnitByIndex(0).GetID() != id ) return "Game killed the wrong unit"; - if ( game.CheckForWin() != player_t::Blue ) + if ( game.GetWinningPlayer() != player_t::Blue ) return "Game failed to recognise a win for the right Player"; std::string game_string = game.GetStateAsString(); diff --git a/source/ttrts/main.cpp b/source/ttrts/main.cpp index 91fbf1b..c17fffb 100644 --- a/source/ttrts/main.cpp +++ b/source/ttrts/main.cpp @@ -133,10 +133,8 @@ int main(int argc, char* argv[]) snprintf(cmd1,128, "rm -rf %s/*",gameDir.c_str()); system(cmd1); - // While the game hasn't been won - player_t winningPlayer; - while ( ((winningPlayer = game.CheckForWin()) == player_t::NUM_INVALID) // We have a winning player - && game.GetNumUnits() > 0 ) // We have no units left + // While the game isn't finished + while ( ! game.GameOver() ) { std::cout<<"Starting turn "<