#! /usr/local/bin/tcsh -f
#
#  A script to play a connect5 match (two games).
#  Bug: error messages from conn5 are not handled very well.

set debug = 0


if ($1 == -h || $1 == --help || $1 == -help || $#argv > 3) then
  echo "  "A script to play a connect5 match (two games).
  echo Usage: $0 \[-q\] game1 game2
  echo "  "where each gamei is either a comp210 user id, or
  echo "  "the pathname of an connect5 code \(scheme or executable\)
  echo ""
  echo "  "Bug: error messages from conn5 are not handled very well.
  exit
  endif

if ($1 == -q || $1 == --quiet) then
  set quiet = 1
  shift
else
  set quiet = 0
  endif

set noisy = (! $quiet)

set name1 = $1
set name2 = $2
set score1 = 0
set score2 = 0

set winPoints = 2
set drawPoints = 1
set losePoints = 0

set tmpfile = /tmp/match-$user-$name1:t-$name2:t

set conn5 = conn5-text
set boardSize = 15
set timeLimit = 30
set conn5flags = "-t $timeLimit -n $boardSize"
if ($quiet) set conn5flags = "-q $conn5flags"

set ss1 = /home/$name1/comp210/connect5/connect5.ss
if (-r $ss1) then
  set prog1 = $ss1
else if (-r $name1) then
  set prog1 = $name1
else
  echo file not found for $name1
  exit
  endif

set ss2 = /home/$name2/comp210/connect5/connect5.ss
if (-r $ss2) then
  set prog2 = $ss2
else if (-r $name2) then
  set prog2 = $name2
else
  echo file not found for $name2
  exit
  endif




if ($noisy) echo $name1 versus $name2

if ($noisy) echo -n "  " $name1 moves first...
set runConn5 = "$conn5 $conn5flags -p1 $prog1 -p2 $prog2"

if ($noisy) then
  \rm -f $tmpfile
  $runConn5 | tee $tmpfile
  set result = `tail -1 $tmpfile` 
  \rm -f $tmpfile
else
  set result = `$runConn5`
  endif



if ("$result[3]" == "error:") then
  #if ($noisy) echo $result
  echo $result
  @ winner = 3 - $result[2]
  set result = `echo Player $winner wins`
  endif


if ("$result" == "Game is a draw") then
  if ($noisy) echo $result
  @ score1 += $drawPoints
  @ score2 += $drawPoints
else
  if ($debug) echo result is $result
  set winner = $result[2]
  if ($debug) echo winner is $winner
  if ($noisy) echo "  " $argv[$winner] wins first round.
  if ($winner == 1) then
    @ score1 += $winPoints
    @ score2 += $losePoints
  else
    @ score1 += $losePoints
    @ score2 += $winPoints
    endif
  endif



if ($noisy) echo -n "  " now $name2 moves first...
set runConn5 = "$conn5 $conn5flags -p1 $prog2 -p2 $prog1"

if ($noisy) then
  \rm -f $tmpfile
  $runConn5 | tee $tmpfile
  set result = `tail -1 $tmpfile` 
  \rm -f $tmpfile
else
  set result = `$runConn5`
  endif


if ("$result[3]" == "error:") then
  #if ($noisy) echo $result
  echo $result
  @ winner = 3 - $result[2]
  set result = `echo Player $winner wins`
  endif

if ("$result" == "Game is a draw") then
  if ($noisy) echo $result
  @ score1 += $drawPoints
  @ score2 += $drawPoints
else
  @ winner = 3 - $result[2]
  if ($noisy) echo "  " $argv[$winner] wins second round.
  if ($winner == 1) then
    @ score1 += $winPoints
    @ score2 += $losePoints
  else
    @ score1 += $losePoints
    @ score2 += $winPoints
    endif
  endif



echo $name1{:} $score1
echo $name2{:} $score2
