Skip to content

Instantly share code, notes, and snippets.

@shanecelis
Created February 28, 2010 07:20
Show Gist options
  • Save shanecelis/317460 to your computer and use it in GitHub Desktop.
Save shanecelis/317460 to your computer and use it in GitHub Desktop.
#!/bin/bash
# mycompete
if [ $# -eq 0 ]; then
echo "usage: compete <n> <player1-classname> <player2-classnames(s)>" >&2;
echo " n number of rounds for p1 vs. p2 and p2 vs. p1 (so 2n rounds total)" >&2;
echo "creates a CSV output for the competition.">&2;
exit 2;
fi
function match()
{
local p1="${1%%.class}";
local p2="${2%%.class}";
# Uncomment to just play with the formatting.
#return 0;
output="$(java -cp . Main -text -p1 "$p1" -p2 "$p2" | tail -1)"
if [ "$output" == "Player 1 won" ]; then
return 0;
elif [ "$output" == "Player 2 won" ]; then
return 1;
elif [ "$output" == "Draw Game" ]; then
return 2;
else
echo "error: failed to decipher win/loss/draw on output: '$output'" >&2;
return 3;
fi
}
n="$1";
p1="$2";
shift;
shift;
#echo "rounds,$n";
echo "ID,OPPONENT ID,WINS,LOSSES,DRAWS"
for p2 in "$@"; do
echo -n "$p1,$p2"
results=(0 0 0);
for ((j=0; j< n; j+=1)); do
match "$p1" "$p2";
i=$?
results[i]=$((${results[$i]} + 1))
# Now match the other way.
match "$p2" "$p1";
i=$?
if [ "$i" -eq 0 ]; then
i=1;
elif [ "$i" -eq 1 ]; then
i=0;
fi
results[i]=$((${results[$i]} + 1))
done
echo ",${results[0]},${results[1]},${results[2]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment