Some small cleanup and refactoring.

Update name of a few functions to best suit their usage.
Split up checking for winning player and checking for game over state.
This commit is contained in:
mdiluzio 2014-12-29 21:52:25 +00:00
parent d54ccf3d2f
commit 5737ae31be
4 changed files with 21 additions and 14 deletions

View file

@ -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<player_t> 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];

View file

@ -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;

View file

@ -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();

View file

@ -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 "<<game.GetTurn()<<std::endl;
@ -220,6 +218,9 @@ int main(int argc, char* argv[])
// Output final gamestate
OutputGameStateFile(game, gameDir);
// Get the winning player
player_t winningPlayer = game.GetWinningPlayer();
// Print the winner!
if ( winningPlayer != player_t::NUM_INVALID )
{