So I found that after I would get an aglo strategy to the point where I thought it was better than a previous version, I wanted to make sure. This meant running the game over and over and checking to see who won for each game. To make this faster I wrote a couple of scripts to make this process faster and easier.
This is only for Windows (except for the python script obviously) since it is in PowerShell.
There are two files, a PowerShell script and a very basic python script.
Both files must be put in the main game directory (eg has README, engine.jar, etc).
The PowerShell script runs as many games as you want while only running so many at a time (what I call batch size).
So to run 20 games but only 5 running at a time you would enter:
.\[NAME OF FILE] 20 5
Which in my case is:
.\run_multiple 20 5
You should not set the batch size to be more than 15, depending on your system because it slows WAY down. I tried 100 when testing because Iām greedy and it crashed after a long time (granted, I donāt have a super powerful system).
After saving the code you will need to change the algo that the script runs, which should be just changing the names to be your algos. The comments have information about changing that as well as the line number.
The Python script simply looks at all the replay files and checks who won the game, player 1 or 2 and then outputs the sums. You should save this file as āget_winners.pyā or you can edit the call in the PowerShell script.
Here is the code for the PowerShell:
# This is a PowerShell script to run a bunch of games with a single command.
# This is intended for when you have worked out all bugs and are testing to see if a new strategy really is better than an old one.
# This program assumes this file is in the main engine directory (eg dir with README and engine.jar)
# You can change this of course, but that is on you :).
# Inside $runprog you can see that there is the execution "scripts/run_match.ps1 algos/my-bot algos/starter-algo" (line 48).
# This should be the same command you use to start your game locally. So change "my-bot" to be whatever the name is of your algo directory.
# The script at the end is a seperate python file that looks at all the replay files and tells you how many each player won.
# You call this script in PowerShell by running .\[THIS_FILE_NAME] [NUM_OF_GAMES] [BATCH_SIZE]
# An example would be: .\run_multiple 20 5
# This would run the game 20 times with only 5 games running at a time.
# You can leave out a batch size and the default is set to 5 (of course you can edit this below)
# so this command would do the same thing: .\run_multiple 20
# DO NOT RUN WITH A LARGE BATCH SIZE (like >15, depending on your computer) or else it will take forever and crash
# If you have questions just ask me on the forums - @Isaac
$global:completed = 0 # Number of games that have finished
$global:running = 0 # Number of games currently running
$dir = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path) #Get the current directory of this script
# Set batch size defaults - sets how many games can run at one time
If (!$args[1]) {
$global:batch = 5
}
ElseIf ($args[1] -lt 0) {
$global:batch = 5
}
Else {
$global:batch = $args[1]
}
# Main loop that runs each game - each loop starts a game in a new powershell (not visible)
For ($i = 1; $i -lt $args[0]+1; $i++) {
Write-Host "started game #$i"
# This is the program that is run, so essentially just starts the game
$runprog = {
param($num,$path)
cd $path
scripts/run_match.ps1 algos/my-bot algos/starter-algo | Out-Null # Edit this to change the algo you are running
}
$job = Start-Job $runprog -ArgumentList $i,$dir # Start the new powershell
$global:running++ # Add 1 to the number of running programs
# This is triggered when the associated game ends
$jobEvent = Register-ObjectEvent $job StateChanged -MessageData $i -Action {
Write-Host ("finished game #$($event.MessageData)")
$global:completed++ # Add to our completed counter
$global:running-- # Remove the number of running games by 1
$jobEvent | Unregister-Event # Remove the job
}
# if the number of games running equals the batch size - wait here
While (($global:batch - $global:running) -eq 0) {}
}
# Wait for it all to complete
While ($global:completed -lt $args[0]) {}
Write-Host "FINISHED ALL GAMES!`n"
# This script looks at all the replay files and prints out the number of winners
py get_winners.py
Here is the code for the python file (save it as āget_winners.pyā or edit the PowerShell command)
'''
This is a python script to check all replay files in the /replays folder.
It looks at each file and simply looks for a string to see which player won.
This program assumes this file is in the main engine directory (eg dir with README and engine.jar)
It also assumes the "replays" folder exists.
You can change this of course, but that is on you :).
If you have questions just ask me on the forums - @Isaac
'''
try:
import os
replayDir = os.getcwd()+'\\replays' # Gets the current directory
# Initialize counters
p1WinCnt = 0
p2WinCnt = 0
unknown = 0
# Loop through every file in "replays" directory
for filename in os.listdir(replayDir):
if filename.endswith(".replay"):
with open(replayDir+'\\'+filename, 'r') as file:
data = file.read()
if (data.find('"winner":1') != -1): p1WinCnt += 1
elif (data.find('"winner":2') != -1): p2WinCnt += 1
else: unknown += 1
# Print results
print ('Player 1 Wins: {}'.format(p1WinCnt))
print ('Player 2 Wins: {}'.format(p2WinCnt))
if (unknown > 0): print ('Could not find winner: {}'.format(unknown))
print ()
except Exception as e:
print (e)
Hope this helps some people :).