Game* Game::theGame = NULL;
bool Game::gameCreated = false;
-Game::Game(const char * redPath, const char * bluePath, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard), timeoutTime(newTimeoutTime)
+Game::Game(const char * redPath, const char * bluePath, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime, const char * newImageOutput) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard), timeoutTime(newTimeoutTime), imageOutput(newImageOutput)
{
gameCreated = false;
if (gameCreated)
// logMessage("Game initialised.\n");
}
-Game::Game(const char * fromFile, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard), timeoutTime(newTimeoutTime)
+Game::Game(const char * fromFile, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime,const char * newImageOutput) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard), timeoutTime(newTimeoutTime), imageOutput(newImageOutput)
{
gameCreated = false;
if (gameCreated)
// logMessage("Messaging red with \"START\"\n");
red->Message("START");
-
+ int moveCount = 0;
while (!Board::HaltResult(result) && (turnCount < maxTurns || maxTurns < 0))
{
#ifdef BUILD_GRAPHICS
if (graphicsEnabled)
+ {
theBoard.Draw(toReveal);
+ if (imageOutput != "")
+ {
+ string imageFile = "" + imageOutput + "/"+ itostr(moveCount) + ".bmp";
+ Graphics::ScreenShot(imageFile.c_str());
+ }
+
+ }
#endif //BUILD_GRAPHICS
turn = Piece::RED;
theBoard.PrintPretty(stdout, toReveal);
fprintf(stdout, "\n\n");
}
+
+ ++moveCount;
#ifdef BUILD_GRAPHICS
if (graphicsEnabled)
+ {
theBoard.Draw(toReveal);
+ if (imageOutput != "")
+ {
+ string imageFile = "" + imageOutput + "/" + itostr(moveCount) + ".bmp";
+ Graphics::ScreenShot(imageFile.c_str());
+ }
+ }
#endif //BUILD_GRAPHICS
else
ReadUserCommand();
-
+ ++moveCount;
++turnCount;
}
}
+string itostr(int i)
+{
+ stringstream s;
+ s << i;
+ return s.str();
+}
+
class Game
{
public:
- Game(const char * redPath, const char * bluePath, const bool enableGraphics, double newStallTime = 1.0, const bool allowIllegal=false, FILE * newLog = NULL, const Piece::Colour & newRevealed = Piece::BOTH, int maxTurns = 5000, const bool printBoard = false, double newTimeoutTime = 2.0);
- Game(const char * fromFile, const bool enableGraphics, double newStallTime = 1.0, const bool allowIllegal=false, FILE * newLog = NULL, const Piece::Colour & newRevealed = Piece::BOTH, int maxTurns = 5000, const bool printBoard = false, double newTimeoutTime = 2.0);
+ Game(const char * redPath, const char * bluePath, const bool enableGraphics, double newStallTime = 1.0, const bool allowIllegal=false, FILE * newLog = NULL, const Piece::Colour & newRevealed = Piece::BOTH, int maxTurns = 5000, const bool printBoard = false, double newTimeoutTime = 2.0, const char * newImageOutput = "");
+ Game(const char * fromFile, const bool enableGraphics, double newStallTime = 1.0, const bool allowIllegal=false, FILE * newLog = NULL, const Piece::Colour & newRevealed = Piece::BOTH, int maxTurns = 5000, const bool printBoard = false, double newTimeoutTime = 2.0, const char * newImageOutput = "");
virtual ~Game();
private:
double timeoutTime;
+ std::string imageOutput;
};
};
+std::string itostr(int i);
#endif //MAIN_H
SDL_Delay(n);
}
+/* Writes an upside down image???
+void Graphics::ScreenShot(const char * fileName)
+{
+
+ std::vector< GLubyte > pixeldata;
+
+ pixeldata.resize( swidth * sheight * 3 );
+
+ SDL_Surface* image = SDL_CreateRGBSurface(SDL_SWSURFACE, screenWidth, screenHeight, 24,255U << (16),255 << (8),255 << (0),0);
+
+ SDL_LockSurface( image );
+
+ glReadPixels(0, 0, swidth, sheight, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *)image->pixels);
+
+
+
+ SDL_UnlockSurface( image );
+
+ SDL_SaveBMP(image, fileName);
+ SDL_FreeSurface( image );
+
+
+}
+*/
+
+
+// Hacky code from http://www.gamedev.net/topic/389159-bitmap-file-saving-problem/
+// Should probably make it nicer
+
+void Graphics::ScreenShot(const char * fileName)
+{
+
+ unsigned char *pixels = (unsigned char*)(malloc (screenWidth * screenHeight * 3));
+
+ SDL_Surface* reversed_image = SDL_CreateRGBSurface(SDL_SWSURFACE, screenWidth, screenHeight, 24,
+ 255U << (0), // Blue channel
+ 255 << (8), // Green channel
+ 255 << (16), // Red channel
+ 0 /* no alpha! */);
+
+ SDL_LockSurface( reversed_image );
+
+ // Read in the pixel data
+ glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, pixels);
+
+ SDL_UnlockSurface( reversed_image );
+
+ /* At this point the image has been reversed, so we need to re-reverse it so that
+ it is the correct way around. We do this by copying the "image" pixels to another
+ surface in reverse order */
+ SDL_Surface* image = SDL_CreateRGBSurface(SDL_SWSURFACE, screenWidth, screenHeight, 24,
+ 255U << (0), // Blue channel
+ 255 << (8), // Green channel
+ 255 << (16), // Red channel
+ 0 /* no alpha! */);
+
+ uint8_t *imagepixels = reinterpret_cast<uint8_t*>(image->pixels);
+ // Copy the "reversed_image" memory to the "image" memory
+ for (int y = (screenHeight - 1); y >= 0; --y) {
+ uint8_t *row_begin = pixels + y * screenWidth * 3;
+ uint8_t *row_end = row_begin + screenWidth * 3;
+
+ std::copy(row_begin, row_end, imagepixels);
+
+ // Advance a row in the output surface.
+ imagepixels += image->pitch;
+ }
+
+ // Save file
+ SDL_SaveBMP(image, fileName);
+
+ // Clear memory
+ SDL_FreeSurface( reversed_image );
+ SDL_FreeSurface( image );
+}
+
#endif //BUILD_GRAPHICS
+#include <fstream>
};
static bool Initialised() {return initialised;}
-
+ static void ScreenShot(const char * fileName);
protected:
static void DrawColourData(SDL_Surface * dest, int destX, int destY, std::vector<SUint> * R, std::vector<SUint> * G, std::vector<SUint> * B, std::vector<SUint> * A = NULL);
static void DrawColourData(SDL_Surface * dest, int destX, int destY, std::vector<std::vector<SUint> > * R, std::vector<std::vector<SUint> > * G, std::vector<std::vector<SUint> > * B, std::vector<std::vector<SUint> > * A = NULL);
};
+
+
#endif //GRAPHICS_H
#endif //BUILD_GRAPHICS
void DestroyGame();
void PrintResults(const MovementResult & result, string & buffer);
+char * video = NULL;
+
int main(int argc, char ** argv)
{
Game::theGame->red->Message("QUIT " + buffer);
Game::theGame->blue->Message("QUIT " + buffer);
+ if (video != NULL)
+ {
+ string command = "mv "; command += video; command += " tmp; cd tmp; ffmpeg -r 10 -b 1024k -i %d.bmp "; command += video;
+ command += ";mv "; command += video; command += " ../; cd ../; rm -rf tmp;";
+ system(command.c_str());
+ }
+
//Log the message
if (Game::theGame->GetLogFile() != stdout)
Game::theGame->logMessage("%s\n", buffer.c_str());
fprintf(stdout, "%s\n", buffer.c_str());
+
+
exit(EXIT_SUCCESS);
return 0;
}
{
char * red = NULL; char * blue = NULL; double stallTime = 0.0; bool graphics = false; bool allowIllegal = false; FILE * log = NULL;
Piece::Colour reveal = Piece::BOTH; char * inputFile = NULL; int maxTurns = 5000; bool printBoard = false; double timeoutTime = 2.0;
+ char * imageOutput = (char*)"";
+
for (int ii=1; ii < argc; ++ii)
{
case 'g':
#ifdef BUILD_GRAPHICS
- graphics = !graphics;
+ graphics = true;
#else
fprintf(stderr, "ERROR: -g switch supplied, but the program was not built with graphics.\n Please do not use the -g switch.");
exit(EXIT_FAILURE);
inputFile = argv[ii+1];
++ii;
break;
+ case 'v':
+ #ifdef BUILD_GRAPHICS
+ video = argv[ii+1];
+ #endif //BUILD_GRAPHICS
+ case 'I':
+ {
+ #ifdef BUILD_GRAPHICS
+ graphics = true;
+ if (argc - ii <= 1)
+ {
+ fprintf(stderr, "ARGUMENT_ERROR - Expected filename after -I switch!\n");
+ exit(EXIT_FAILURE);
+ }
+ imageOutput = argv[ii+1];
+ string m("mkdir -p "); m += imageOutput;
+ system(m.c_str());
+ ++ii;
+ #else
+ fprintf(stderr, "ERROR: -%c switch supplied, but the program was not built with graphics.");
+ exit(EXIT_FAILURE);
+ #endif //BUILD_GRAPHICS
+
+ break;
+
+ }
+
+
+
case 'h':
system("clear");
system("less manual.txt");
exit(EXIT_FAILURE);
}
- Game::theGame = new Game(red,blue, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
+ Game::theGame = new Game(red,blue, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime, imageOutput);
}
else
{
- Game::theGame = new Game(inputFile, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
+ Game::theGame = new Game(inputFile, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime, imageOutput);
}
if (Game::theGame == NULL)
Now (sortof) generates .html files to display results in a prettiful manner.
+ THIS FILE IS TERRIBLE
author Sam Moore (matches) [SZM]
website http://matches.ucc.asn.au/stratego
print "Preparing .html results files..."
+if os.path.exists(resultsDirectory + "index.html") == True:
+ os.remove(resultsDirectory + "index.html") #Delete the file
+totalFile = open(resultsDirectory + "index.html", "w")
+totalFile.write("<html>\n<head>\n <title> Round in progress... </title>\n</head>\n<body>\n")
+if nRounds > 1:
+ totalFile.write("<h1> Rounds " + str(totalRounds) + " to " + str(totalRounds + nRounds-1) + " in progress...</h1>\n")
+else:
+ totalFile.write("<h1> Round " + str(totalRounds) + " in progress...</h1>\n")
+totalFile.write("<p> Please wait for the rounds to finish. You can view the current progress by watching the <a href = \"../log\"/>Log Files</a> </p>")
+if totalRounds > 1:
+ totalFile.write("<h2> Round Summaries </h2>\n")
+ totalFile.write("<table border=\"0\" cellpadding=\"10\">\n")
+ for i in range(1, totalRounds):
+ totalFile.write("<tr> <td> <a href=round"+str(i)+".html>Round " + str(i) + "</a> </td> </tr>\n")
+ totalFile.write("</table>\n")
+
+totalFile.write("</body>\n<!-- Total Results file autogenerated by \"" + sys.argv[0] + "\" at time " + str(time()) + " -->\n</html>\n\n")
+totalFile.close()
+
+
for agent in agents:
if os.path.exists(resultsDirectory+agent["name"] + ".html") == False:
agentFile = open(resultsDirectory+agent["name"] + ".html", "w")
while line != "":
#if verbose:
# print "Interpreting line \"" + line.strip() + "\""
- if line.strip() == "</body>":
+ if line.strip() == "</body>" or line.strip() == "<!--end-->":
break
- elif line == "<tr> <th> Score </th> <th> Wins </th> <th> Losses </th> <th> Draws </th> <th> Illegal </th> <th> Errors </th></tr>\n":
+ elif line == "<h3> Round Overview </h3>\n":
agentFile.write(line)
line = oldFile.readline()
-
- values = line.split(' ')
- agent["totalScore"] += int(values[2].strip())
- agent["Wins"] += int(values[5].strip())
- agent["Losses"] += int(values[8].strip())
- agent["Draws"] += int(values[11].strip())
- agent["Illegal"] += int(values[14].strip())
- agent["Errors"] += int(values[17].strip())
+ agentFile.write(line)
+ line = oldFile.readline()
+ if line == "<tr> <th> Score </th> <th> Wins </th> <th> Losses </th> <th> Draws </th> <th> Illegal </th> <th> Errors </th></tr>\n":
+ #sys.stdout.write("Adding scores... " + line + "\n")
+ agentFile.write(line)
+ line = oldFile.readline()
+ values = line.split(' ')
+ agent["totalScore"] += int(values[2].strip())
+ agent["Wins"] += int(values[5].strip())
+ agent["Losses"] += int(values[8].strip())
+ agent["Draws"] += int(values[11].strip())
+ agent["Illegal"] += int(values[14].strip())
+ agent["Errors"] += int(values[17].strip())
agentFile.write(line)
line = oldFile.readline()
errorLog = [logDirectory + "error/" + red["name"] + "."+str(gameID), logDirectory + "error/" + blue["name"] + "."+str(gameID)]
#Run the game, outputting to logFile; stderr of (both) AI programs is directed to logFile.stderr
outline = os.popen(managerPath + " -o " + logFile + " -T " + str(timeoutValue) + " \"" + red["path"] + "\" \"" + blue["path"] + "\" 2>> " + logFile+".stderr", "r").read()
+ #os.system("mv tmp.mp4 " + logFile + ".mp4")
#If there were no errors, get rid of the stderr file
if os.stat(logFile+".stderr").st_size <= 0:
otherColour["score"].insert(0, otherColour["score"][0] + scores[results[2]][1])
otherColour[scores[results[2]][2]].append((endColour["name"], gameID, scores[results[2]][1]))
otherColour["ALL"].append((endColour["name"], gameID, scores[results[2]][1], scores[results[2]][2], otherStr))
+ #Write scores to raw text files
+ for agent in [endColour, otherColour]:
+ scoreFile = open(resultsDirectory + agent["name"] + ".scores", "a")
+ scoreFile.write(str(agent["totalScore"] + agent["score"][0]) + "\n")
+ scoreFile.close()
+
+
if verbose:
agentFile.write("</table>\n")
+
+
+
agentFile.close()
#Update round file
for agent in agents:
roundFile.write("<tr> <td> <a href="+agent["name"]+".html>"+agent["name"] + " </a> </td> <td> " + str(agent["score"][0]) + " </td> <td> " + str(agent["totalScore"]) + " </td> </tr>\n")
roundFile.write("</table>\n")
- roundFile.write("<p> <a href=total.html>Current Scoreboard</a></p>\n")
+
+ command = "cp scores.plt " + resultsDirectory + "scores.plt;"
+ os.system(command)
+
+ scorePlot = open(resultsDirectory + "scores.plt", "a")
+ scorePlot.write("plot ")
+ for i in range(0, len(agents)):
+ if i > 0:
+ scorePlot.write(", ")
+ scorePlot.write("\""+agents[i]["name"]+".scores\" using ($0+1):1 with linespoints title \""+agents[i]["name"]+"\"")
+
+ scorePlot.write("\nexit\n")
+ scorePlot.close()
+
+ command = "d=$(pwd); cd " + resultsDirectory + ";"
+ command += "gnuplot scores.plt;"
+ command += "rm -f scores.plt;"
+ command += "mv scores.png round"+str(roundNumber)+".png;"
+ command += "cd $d;"
+ os.system(command)
+
+ roundFile.write("<h2> Accumulated Scores - up to Round " + str(roundNumber)+" </h2>\n")
+ roundFile.write("<img src=\"round"+str(roundNumber)+".png\" alt = \"round"+str(roundNumber)+".png\" title = \"round"+str(roundNumber)+".png\" width = \"640\" height = \"480\"/>\n")
+
+ roundFile.write("<p> <a href=index.html>Current Scoreboard</a></p>\n")
roundFile.write("</body>\n<!-- Results file for Round " + str(roundNumber) + " autogenerated by \"" + sys.argv[0] + "\" at time " + str(time()) + " -->\n</html>\n\n")
roundFile.close()
print "Finalising .html files... "
for agent in agents:
agentFile = open(resultsDirectory + agent["name"]+".html", "a")
-
- #Write the "total" statistics
+ agentFile.write("<!--end-->\n")
+ #Write a graph
+ #Comment out if you don't have gnuplot
+
+ command = "rm -f " + agent["name"] + ".png;"
+ command += "cp template.plt " + resultsDirectory + agent["name"] + ".plt;"
+ command += "d=$(pwd); cd " + resultsDirectory + ";"
+ command += "sed -i \"s:\[NAME\]:"+agent["name"]+":g\" " +resultsDirectory + agent["name"]+".plt;"
+ command += "gnuplot " + resultsDirectory + agent["name"]+".plt;"
+ command += "rm -f " + resultsDirectory + agent["name"] + ".plt;"
+ command += "cd $d;"
+ os.system(command)
+ agentFile.write("<!--end-->\n")
+ agentFile.write("<h3> Score Graph </h3>\n")
+ agentFile.write("<img src=\""+agent["name"]+".png\" alt=\""+agent["name"]+".png\" title=\""+agent["name"]+".png\" width=\"640\" height=\"480\"/>\n")
+
+ #Link to main file
+ agentFile.write("<p> <a href=\"index.html\"/>Total Statistics</a> </p>\n")
agentFile.write("</body>\n<!-- Results file for \"" + agent["name"] + "\" autogenerated by \"" + sys.argv[0] + "\" at time " + str(time()) + " -->\n</html>\n\n")
agentFile.close()
- if os.path.exists(resultsDirectory + "total.html") == True:
- os.remove(resultsDirectory + "total.html") #Delete the file
+if os.path.exists(resultsDirectory + "index.html") == True:
+ os.remove(resultsDirectory + "index.html") #Delete the file
-totalFile = open(resultsDirectory + "total.html", "w")
+totalFile = open(resultsDirectory + "index.html", "w")
totalFile.write("<html>\n<head>\n <title> Total Overview </title>\n</head>\n<body>\n")
totalFile.write("<h1> Total Overview </h1>\n")
totalFile.write("<table border=\"0\" cellpadding=\"10\">\n")
totalFile.write("<tr> <td> <a href="+agent["name"]+".html>"+agent["name"] + " </a> </td> <td> " + str(agent["totalScore"]) + " </td> </tr>\n")
totalFile.write("</table>\n")
+totalFile.write("<h2> Score Graph </h2>\n")
+
+
+command = "d=$(pwd);"
+command += "cd " + resultsDirectory + ";"
+command += "rm -f scores.png;"
+command += "cp round"+str(roundNumber)+".png scores.png;"
+command += "cd $d;"
+os.system(command)
+
+totalFile.write("<img src=\"scores.png\" alt=\"scores.png\" title=\"scores.png\" width=\"640\" height=\"480\"/>\n")
+
+
totalFile.write("<h2> Round Summaries </h2>\n")
totalFile.write("<table border=\"0\" cellpadding=\"10\">\n")
for i in range(1, totalRounds+1):
totalFile.write("</body>\n<!-- Total Results file autogenerated by \"" + sys.argv[0] + "\" at time " + str(time()) + " -->\n</html>\n\n")
totalFile.close()
+#Write results to a raw text file as well
+textResults = open(resultsDirectory + "total.txt", "w")
+for agent in agents:
+ textResults.write(agent["name"] + " " + str(agent["totalScore"]) + "\n")
+textResults.close()
if verbose:
print "Done!"
SYNOPSIS
- stratego {[-gpirb] [-o output_file ] [-t stall_time] [-T timeout_time] [-m max_turns] {red_player blue_player | -f input_file} | {-h | --help} }
+ stratego {[-gpirb] [-o output_file ] [-t stall_time] [-T timeout_time] [-m max_turns] [-I image_directory] {red_player blue_player | -f input_file} | {-h | --help} }
QUICK EXAMPLE - Play against a sample AI, using graphics, hiding the AI's pieces
stratego -g -b @human ../../agents/vixen/vixen.py
All switches function as normal with -f.
NOTE: It is recommended that -g is used with -f.
+ -I
+ stratego can output image files in the BMP format, when built with graphics.
+ If this option is supplied, a directory indicated will be used, and a .bmp image will be saved to the directory after each move.
+ The images will be numbered from 0.bmp (before the game starts) with increasing integers for each move.
+ Note that the image number corresponds to the move number, not the turn number.
+ The move number is odd after RED has moved, and even after BLUE has moved.
+
+ NOTE: The -I switch will automatically enable graphics, even if the -g switch is not supplied.
+
+ -v
+ If you have ffmpeg on your system, you can use this to quickly create a video.
+ When this option is supplied, stratego first outputs image files to a temporary directory in BMP format.
+ After the game is finished, these files are used to create an mp4 movie with the specified name:
+
+ $ ffmpeg -r 10 -b 1024k -i tmp/%d.bmp filename
+
+ The temporary images will then be deleted.
+
-h, --help
If the -h switch is used, this page will be printed and stratego will exit.