;BattleShip computer player
; also for Megan and Jochen
break on
:start
cls
$seed=val(substr("@time",7,2)) $seed=$seed+1
do $=rnd(1) $seed=$seed-1 until $seed=0
$path="c:\scripts"
$gx=0 $gy=0 $l=0 $in2=" " $last="o" $lose=0
$cc=rnd(100)
goto Main
:gameON
del "$path\$name.txt" ; clean up from previous games
$r=open(1,"$path\game.txt")
if $r=2 ; start new game
at(1,0) "Waiting for player..."
$create=1
$=open(1,"$path\game.txt",5)
$=writeline(1,"computer$cc")
$=close(1)
:getreply
$r=open(1,"$path\reply.txt")
if $r=2 sleep 1 goto getreply endif
$badguy=readline(1)
$=close(1)
del "$path\reply.txt"
else ; join this game
$create=0
$badguy=readline(1)
$=close(1)
del "$path\game.txt"
$=open(1,"$path\reply.txt",5)
$=writeline(1,"computer$cc")
$=close(1)
endif
return
:generate
$gx=rnd(9)$gy=rnd(9)
$l=99 $l=rnd(100)+1
if $l=55 $last="lose" else $l=rnd(1)+1 if $l=1 $last="o" else $last="X" endif endif
return
:updatescreen
at(3,0) "$$gx=$gx $$gy=$gy "
at(4,0) "$$l=$l "
at(5,0) "Player attack"
at(6,0) "$in2 "
return
:sendattack
$=open(1,"$path\computer$cc.txt",5)
$out=$last+chr(13)+chr(10)
$=writeline(1,$out)
$out=chr($gx+65)+","+chr($gy+48)
$=writeline(1,$out)
$=close(1)
return
:getattack
$r=open(1,"$path\$badguy.txt")
if $r=2 sleep 1 goto getattack endif
$in1=readline(1)
if $in1="lose" $lose=1 endif
$in2=readline(1)
$=close(1)
del "$path\$badguy.txt"
if $lose=1 goto start endif
return
:MAIN
gosub gameON ; Create/join game
if $create=1 at(1,0) "computer$cc Vs $badguy " else "$badguy Vs computer$cc " endif
:again
gosub generate ; generate attack info
gosub updatescreen
if $create=0 goto waitnow endif
gosub sendattack ; send attack co-ords to bad guy
:waitnow
$create=1
gosub getattack ; get result of attack and get bad guys attack from bad guy
gosub updatescreen
goto again
;END