+if verbose:
+ if nRounds > 1:
+ print "Simulating " + str(nRounds) + " rounds (" + str(totalRounds) + " to " + str(totalRounds + nRounds-1) + ")"
+ else:
+ print "Simulating one round."
+ print ""
+ print "Identifying possible agents in \""+agentsDirectory+"\""
+
+#Get all agent names from agentsDirectory
+agentNames = os.listdir(agentsDirectory)
+agents = []
+for name in agentNames:
+ if verbose:
+ sys.stdout.write("Scan \""+name+"\"... ")
+ if os.path.isdir(agentsDirectory+name) == False: #Remove non-directories
+ if verbose:
+ sys.stdout.write(" Invalid! (Not a directory)\n")
+ continue
+
+ if os.path.exists(agentsDirectory+name+"/info") == False: #Try and find the special "info" file in each directory; ignore if it doesn't exist
+ if verbose:
+ sys.stdout.write(" Invalid! (No \"info\" file found)\n")
+ continue
+
+ infoFile = open(agentsDirectory+name+"/info", "r")
+ agentExecutable = agentsDirectory+name+"/"+(infoFile.readline().strip())
+ author = infoFile.readline().strip()
+ language = infoFile.readline().strip()
+ description = ""
+ while True:
+ line = infoFile.readline()
+ if len(line) > 0:
+ description += line
+ else:
+ break
+ infoFile.close()
+
+ if os.path.exists(agentExecutable) == False:
+ if verbose:
+ sys.stdout.write(" Invalid! (Path: \""+agentExecutable+"\" does not exist!)\n")
+ continue
+
+
+ if verbose:
+ sys.stdout.write(" Valid! (Path: \""+agentExecutable+"\")\n")
+
+ #Convert array of valid names into array of dictionaries containing information about each agent
+ #I'm starting to like python...
+ agents.append({"name":name, "path":agentExecutable, "author":author, "language":language, "description":description, "score":[0], "VICTORY":[], "DEFEAT":[], "DRAW":[], "ILLEGAL":[], "DEFAULT":[], "INTERNAL_ERROR":[], "SURRENDER":[], "DRAW_DEFAULT":[], "BOTH_ILLEGAL":[], "BAD_SETUP":[], "ALL":[], "totalScore":0, "Wins":0, "Losses":0, "Draws":0, "Illegal":0, "Errors":0})
+
+if len(agents) == 0:
+ print "Couldn't find any agents! Check paths (Edit this script) or generate \"info\" files for agents."
+ sys.exit(0)
+if verbose:
+ print "Total: " + str(len(agents)) + " valid agents found (From "+str(len(agentNames))+" possibilities)"
+ print ""
+
+#Prepare the pretty .html files if they don't exist
+if verbose:
+ print "Preparing .html results files..."
+
+
+for agent in agents:
+ if os.path.exists(resultsDirectory+agent["name"] + ".html") == False:
+ agentFile = open(resultsDirectory+agent["name"] + ".html", "w")
+ agentFile.write("<html>\n<head>\n <title> " + agent["name"] + " overview</title>\n</head>\n<body>\n<h1> Overview for " + agent["name"]+" </h1>\n")
+ agentFile.write("<table border=\"0\" cellpadding=\"10\">\n")
+ agentFile.write("<tr> <th> Name </th> <th> Author </th> <th> Language </th> </tr>\n")
+ agentFile.write("<tr> <td> "+agent["name"]+" </td> <td> "+agent["author"]+" </td> <td> "+agent["language"]+" </td> </tr>\n")
+ agentFile.write("</table>\n");
+
+ agentFile.write("<p> <b>Description</b> </p>\n")
+ agentFile.write("<p> " + agent["description"] + " </p>\n")
+ agentFile.close()
+
+ os.rename(resultsDirectory+agent["name"] + ".html", "tmpfile")
+
+ oldFile = open("tmpfile", "r")
+ agentFile = open(resultsDirectory+agent["name"] + ".html", "w")
+ line = oldFile.readline()
+ while line != "":
+ #if verbose:
+ # print "Interpreting line \"" + line.strip() + "\""
+ if line.strip() == "</body>":
+ break
+ elif line == "<tr> <th> Score </th> <th> Wins </th> <th> Losses </th> <th> Draws </th> <th> Illegal </th> <th> Errors </th></tr>\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 verbose:
+ print "Prepared results file \"" + resultsDirectory+agent["name"] + ".html\"."
+ oldFile.close()
+ agentFile.close()
+ os.remove("tmpfile")
+
+if verbose:
+ print ""
+