+#Prepare the pretty .html files if they don't exist
+htmlDir = resultsDirectory + "pretty/"
+if os.path.exists(htmlDir) == False:
+ os.mkdir(htmlDir)
+if os.path.exists(htmlDir) == False:
+ print "Couldn't create directory \""+htmlDir+"\"."
+ sys.exit(1)
+
+for agent in agents:
+ if os.path.exists(htmlDir+agent["name"] + ".html") == False:
+ agentFile = open(htmlDir+agent["name"] + ".html", "w")
+ agentFile.write("<html>\n<head>\n <title> " + agent["name"] + " results</title>\n</head>\n<body>\n<h1> Results for " + agent["name"]+" </h1>\n</body>\n</html>\n")
+ agentFile.close()
+
+ os.rename(htmlDir+agent["name"] + ".html", "tmpfile")
+
+ oldFile = open("tmpfile")
+ agentFile = open(htmlDir+agent["name"] + ".html", "w")
+ for line in oldFile:
+ if line.strip() == "</body>":
+ break
+ agentFile.write(line.strip() + "\n")
+ oldFile.close()
+ agentFile.close()
+ os.remove("tmpfile")
+
+#Do each round...
+totalGames = nGames/2 * len(agents) * (len(agents)-1)
+for roundNumber in range(totalRounds, totalRounds + nRounds):
+
+ if os.path.exists(logDirectory + "round"+str(roundNumber)) == False:
+ os.mkdir(logDirectory + "round"+str(roundNumber)) #Check there is a directory for this round's logs
+
+
+ print "Commencing ROUND " + str(roundNumber) + " combat!"
+ print "Total: " + str(totalGames) + " games to be played. This could take a while... (Estimate 60s/game)"
+
+
+
+ managerErrors = 0
+ #This double for loop simulates a round robin, with each agent getting the chance to play as both red and blue against every other agent.
+ gameNumber = 0
+ for red in agents: #for each agent playing as red,
+ for blue in agents: #against each other agent, playing as blue
+ if red == blue:
+ continue #Exclude battles against self
+ gameNumber += 1
+ gameID = str(roundNumber) + "." + str(gameNumber)
+ for i in range(1, nGames/2 + 1):
+ #Play a game and read the result. Note the game is logged to a file based on the agent's names
+ if verbose:
+ sys.stdout.write("Agents: \""+red["name"]+"\" and \""+blue["name"]+"\" playing game (ID: " + gameID + ") ... ")
+ logFile = logDirectory + "round"+str(roundNumber) + "/"+red["name"]+".vs."+blue["name"]+"."+str(i)
+ outline = os.popen(managerPath + " -o " + logFile + " " + red["path"] + " " + blue["path"], "r").read()
+ results = outline.split(' ')
+
+ if len(results) != 6:
+ if verbose:
+ sys.stdout.write("Garbage output! \"" + outline + "\"\n")
+ red["INTERNAL_ERROR"].append((blue["name"], gameID, scores["INTERNAL_ERROR"][0]))
+ blue["INTERNAL_ERROR"].append((red["name"], gameID, scores["INTERNAL_ERROR"][0]))
+ red["ALL"].append((blue["name"], gameID, scores["INTERNAL_ERROR"][0], "INTERNAL_ERROR"))
+ blue["ALL"].append((red["name"], gameID, scores["INTERNAL_ERROR"][0], "INTERNAL_ERROR"))
+ managerErrors += 1
+ else:
+
+ if results[1] == "RED":
+ endColour = red
+ otherColour = blue
+ endStr = "RED"
+ otherStr = "BLUE"
+ elif results[1] == "BLUE":
+ endColour = blue
+ otherColour = red
+ endStr = "BLUE"
+ otherStr = "RED"
+
+
+ if results[1] == "BOTH":
+ red["INTERNAL_ERROR"].append((blue["name"], gameID, scores["INTERNAL_ERROR"][0]))
+ blue["INTERNAL_ERROR"].append((red["name"], gameID, scores["INTERNAL_ERROR"][0]))
+ red["ALL"].append((blue["name"], gameID, scores["INTERNAL_ERROR"][0], "INTERNAL_ERROR", "RED"))
+ blue["ALL"].append((red["name"], gameID, scores["INTERNAL_ERROR"][0], "INTERNAL_ERROR", "BLUE"))
+ managerErrors += 1
+ else:
+ endColour["score"].insert(0,endColour["score"][0] + scores[results[2]][0])
+ endColour[results[2]].append((otherColour["name"], gameID, scores[results[2]][0]))
+ endColour["ALL"].append((otherColour["name"], gameID, scores[results[2]][0], results[2], endStr))
+ 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))
+
+
+ if verbose:
+ sys.stdout.write(" Result \"")
+ for ii in range(1, len(results)):
+ sys.stdout.write(results[ii].strip())
+ if ii < (len(results) - 1):
+ sys.stdout.write(" ")
+ sys.stdout.write("\"\n")