#/usr/bin/tcsh

set K = $1

./genInstance $K
cat b.txt >> instances
echo "">> instances
@ swon = 0
@ awon = 0
@ bfull = 0
@ num_moves = 0
while ( $swon == 0 &&  $awon == 0 &&  $bfull == 0 )
    cp b.txt  board.txt 
    
    player <in.txt >out.txt 
    
    set move = `less out.txt`
    if ("$move" == "" ) then
	echo Invalid Move >> score
	exit(0)
    endif
    @ num_moves = $num_moves + 1
    set legal = `checkmove $K $move`
    #set legal = "0"
    if( "$legal" == "1" ) then
	echo Invalid Move >> score
	exit(0)
    endif
    #set result = "3"
    set result = `checkwin $K`
    
    if ( "$result" == "0" ) then
	@ swon = 1
    endif

    if( "$result" == "1") then
	@ awon = 1
    endif

    if( "$result" == "2" ) then
	@ bfull = 1
    endif
    
    if ( "$result" == "3" ) then
  	genOmove $K

  	set result = `checkwin $K`

  	if( "$result" == "1" ) then
  	   @ awon = 1
  	endif

 	if( "$result" == "2" ) then
 	    @ bfull = 1
 	endif
	
      endif
end
echo $result >> score
echo $num_moves >> moves
