#!/usr/bin/python

import subprocess, os, sys

generator = './' + sys.argv[1]
checker = './' + sys.argv[2]
board = '' if len(sys.argv) == 3 else sys.argv[3]

game_file = 'engine_fight.pgn'
count = 0

while True:
    try:
        os.remove(game_file)
    except OSError:
        pass

    if os.path.isfile(game_file):
        print('Could not delete output file:', game_file)

    count += 1
    print('Game #' + str(count))

    if board:
        out = subprocess.run([generator, '-random', '-random', '-game_file', game_file, '-board', board])
    else:
        out = subprocess.run([generator, '-random', '-random', '-game_file', game_file])

    if not os.path.isfile(game_file):
        print('Game file not produced: ' + game_file)
        print('generator = ' + generator)
        print(out.returncode)
        print(out.stdout)
        print(out.stderr)
        break

    result = subprocess.run([checker, '-confirm', game_file])
    if result.returncode != 0:
        print('Found discrepancy. See ' + game_file)
        print('generator = ' + generator)
        print('checker = ' + checker)
        break

    generator, checker = checker, generator

@@ -427,47 +427,28 @@
     
-if board:%0A        out = subprocess.run(
+generator_command = 
 %5Bgen
@@ -500,18 +500,66 @@
 ame_file
-, 
+%5D%0A    if board:%0A        generator_command.extend(%5B
 '-board'
@@ -572,22 +572,8 @@
 d%5D)%0A
-    else:%0A    
     
@@ -593,17 +593,16 @@
 ess.run(
-%5B
 generato
@@ -606,56 +606,16 @@
 ator
-, '-random', '-random', '-game_file', game_file%5D
+_command
 )%0A%0A 

Reduce redundancy in chess_engine_fight