Created
February 24, 2021 19:07
-
-
Save panchishin/14fd14e57b517528fbc4a52f1167d2de to your computer and use it in GitHub Desktop.
A test harness for uttt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from subprocess import Popen, PIPE | |
import sys | |
player1 = sys.argv[1] | |
player2 = sys.argv[2] | |
def sendReceive(proc, message): | |
proc.stdin.write(message) | |
proc.stdin.flush() | |
return proc.stdout.readline() | |
def oneCombatRound(player1, player2): | |
first_player = False | |
check = "blank" | |
with Popen(player1.split(" "), universal_newlines=True, stdout=PIPE, stderr=PIPE, stdin=PIPE, text=True) as proc1: | |
with Popen(player2.split(" "), universal_newlines=True, stdout=PIPE, stderr=PIPE, stdin=PIPE, text=True) as proc2: | |
with Popen([f'./binaries/check_winner'], universal_newlines=True, stdout=PIPE, stderr=PIPE, stdin=PIPE, text=True) as check_winner: | |
result = sendReceive(proc1, "-1 -1\n0\n") | |
print(result, end="") | |
move = result[:3] + "\n" | |
check = sendReceive(check_winner, move) | |
while(check == "UNDECIDED\n"): | |
result = sendReceive(proc1 if first_player else proc2, move + "0\n") | |
print(result, end="") | |
move = result[:3] + "\n" | |
check = sendReceive(check_winner, move) | |
first_player = not first_player | |
return check | |
FIRST_ROUND = {"WINNER IS 1":player1, "WINNER IS 2":player2, "WINNER IS 3":"DRAW"} | |
SECOND_ROUND = {"WINNER IS 1":player2, "WINNER IS 2":player1, "WINNER IS 3":"DRAW"} | |
def twoCombatRounds(results): | |
result = oneCombatRound(player1, player2) | |
result = FIRST_ROUND[result[:-1]] | |
results[result] = results.get(result,0) + 1 | |
result = oneCombatRound(player2, player1) | |
result = SECOND_ROUND[result[:-1]] | |
results[result] = results.get(result,0) + 1 | |
print(results) | |
results = {} | |
for i in range(250): | |
twoCombatRounds(results) | |
print(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment