Fix some bugs

Fix a case where game folder existed in verious forms
Fix bug where game would not correctly end at draw state
Fix bug where the team order file was looked for in the wrong directory
Extract gamestate function and properly handle game ending scenarios
Stop hitting yourselves

Fixed bug where units would not move forward because they were hitting themselves
This commit is contained in:
mdiluzio 2014-12-20 15:35:18 +00:00
parent f4109e7b36
commit fa7aa15ccd
2 changed files with 71 additions and 23 deletions

View file

@ -144,7 +144,10 @@ int CTTRTSGame::SimulateToNextTurn()
// If any unit is in this spot, or moving unit moving to said spot, reject this // If any unit is in this spot, or moving unit moving to said spot, reject this
for ( const OrderUnitPair& pair2 : m_OrderUnitPairs ) for ( const OrderUnitPair& pair2 : m_OrderUnitPairs )
{ {
if( GetNewPosition(pair2) != newpos ) // Skip myself
if( pair.unit.getID() == pair2.unit.getID() ) continue;
if( GetNewPosition(pair2) == newpos )
{ {
possible = false; possible = false;
break; break;

View file

@ -3,6 +3,7 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdio.h>
#include "game.h" #include "game.h"
@ -26,6 +27,25 @@ inline void WaitForFile( const std::string& name, const std::chrono::millisecond
while( !FileExists(name) ) std::this_thread::sleep_for(time); while( !FileExists(name) ) std::this_thread::sleep_for(time);
} }
bool OutputGameStateFile(CTTRTSGame &game, std::string &gameDir)
{
char turnFileName[128];
snprintf(turnFileName,128,"%s/Turn_%i.txt",gameDir.c_str(),game.GetTurn());
std::ofstream turnFile(turnFileName, std::ios_base::trunc); // truncate to overwrite if a file exists
if ( turnFile.bad() )
{
return false;
}
// Output the turn description
std::string turnDescriptor = game.GetStateAsString();
turnFile<<turnDescriptor;
turnFile.close();
return true;
}
// Main program entry point // Main program entry point
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@ -73,42 +93,57 @@ int main(int argc, char* argv[])
// Current game directory // Current game directory
std::string gameDir = "ttrts_" + game.GetName(); std::string gameDir = "ttrts_" + game.GetName();
// Remove the current game directory // Empty the current game directory
char cmd[128]; struct stat info;
snprintf(cmd,128, "test -e %s && rm -rf %s",gameDir.c_str(),gameDir.c_str()); int ret = stat( gameDir.c_str(), &info );
system(cmd); if( ret == 0 && info.st_mode & S_IFDIR )
{
std::cout<< gameDir << " already exists"<<std::endl;
std::cout<<"Confirm to delete contents [y/N] ";
std::string input;
std::cin>>input;
if( !input.size() || std::tolower(input[0]) != 'y' )
{
std::cerr<<"Aborting..."<<std::endl;
return 1;
}
}
else if ( ret == 0 )
{
std::cerr<< gameDir << " exists but is not directory \nAborting..."<<std::endl;
return 1;
}
// Create the game directory // Create the game directory
char cmd2[128]; char cmd2[128];
snprintf(cmd2,128, "mkdir %s",gameDir.c_str()); snprintf(cmd2,128, "test -d %s || mkdir %s",gameDir.c_str(),gameDir.c_str());
system(cmd2); system(cmd2);
// While the game hasn't been won // Clean out the game directory
Team winningTeam = Team::NUM_INVALID; char cmd1[128];
while ( (winningTeam = game.CheckForWin()) == Team::NUM_INVALID ) snprintf(cmd1,128, "rm -rf %s/*",gameDir.c_str());
{ system(cmd1);
// Create a turn file
char turnFileName[128];
snprintf(turnFileName,128,"ttrts_%s/Turn_%i.txt",game.GetName().c_str(),game.GetTurn());
std::ofstream turnFile(turnFileName,std::ios::trunc); // truncate to overwrite if a file exists
if ( turnFile.bad() ) // While the game hasn't been won
Team winningTeam;
while ( ((winningTeam = game.CheckForWin()) == Team::NUM_INVALID) // We have a winning team
&& game.GetNumUnits() > 0 ) // We have no units left
{
std::cout<<"Starting turn "<<game.GetTurn()<<std::endl;
// Create a turn file
if( !OutputGameStateFile(game, gameDir))
{ {
std::cerr<<"Error: Failed to open new turn file "<< turnFileName <<std::endl; std::cerr<<"Error: Failed to output new turn file" << std::endl;
return 1; return 1;
} }
// Output the turn description
std::string turnDescriptor = game.GetStateAsString();
turnFile<<turnDescriptor;
turnFile.close();
// Wait for order files // Wait for order files
for( Team team : teams ) for( Team team : teams )
{ {
// Construct the team order filename // Construct the team order filename
char teamOrderFileName[128]; char teamOrderFileName[128];
snprintf(teamOrderFileName, 128, "ttrts_%s/Turn_%i_Team_%i.txt", game.GetName().c_str(), game.GetTurn(), (int) team); snprintf(teamOrderFileName, 128, "%s/Turn_%i_Team_%i.txt", gameDir.c_str(), game.GetTurn(), (int) team);
// Wait for the team order file to be created // Wait for the team order file to be created
std::cout<<"Waiting for "<<teamOrderFileName<<std::endl; std::cout<<"Waiting for "<<teamOrderFileName<<std::endl;
@ -140,8 +175,18 @@ int main(int argc, char* argv[])
} }
// Output final gamestate
OutputGameStateFile(game, gameDir);
// Print the winner! // Print the winner!
std::cout<<"TTRTS: Game over! Winner:"<<(int)winningTeam<<std::endl; if ( winningTeam != Team::NUM_INVALID )
{
std::cout<<"Game over! Winner:"<<(int)winningTeam<<std::endl;
}
else
{
std::cout<<"Game over! It was a draw!"<<std::endl;
}
return 0; return 0;
}; };