dim matr[9] as int
dim xp as int
dim yp as int
dim readk as int
dim xm as int
dim ym as int
dim ch as char
dim bool as int
dim id as long
dim par1 as long
dim par2 as long
dim com as int
dim buf[3] as char
dim m as int
dim buf1[3] as char

'the procedure of waiting the
'result of the partner's move

sub wt
 dim id1 as long
 dim bl as int
 dim p1 as long
 dim p2 as long
 dim c as int
'delete the cursor
 ink 0
 line -70+xm*20+2, 30-ym*20-2, -70+xm*20+18, 30-ym*20-2
 line -70+xm*20+2, 30-ym*20-2, -70+xm*20+2, 30-ym*20-18
 line -70+xm*20+2, 30-ym*20-18, -70+xm*20+18, 30-ym*20-18
 line -70+xm*20+18, 30-ym*20-2, -70+xm*20+18, 30-ym*20-18
'output the message
 id1=0
 ink 3
 printxy 24,20,"Move"
 printxy 17,5,"of your"
 printxy 16,-10,"partner"
 bl=1
'wait for partner's move
 while bl
  receive id1,com,p1,p2,buf1
  c=compare(buf1,buf)
  if (id1=id) and c<>0 then
   bl=0
   buf=buf1
  end if
 wend
 matr[3*buf[1]+buf[0]]=2
 if ch=88 then
  printxy -63+buf[0]*20,24-buf[1]*20,"0"
 else     
  printxy -63+buf[0]*20,24-buf[1]*20,"X"
 end if
'delete the message   
 ink 0
 printxy 24,20,"Move"
 printxy 17,5,"of your"
 printxy 16,-10,"partner"
'draw the cursor
 ink 2
 line -70+xm*20+2, 30-ym*20-2, -70+xm*20+18, 30-ym*20-2
 line -70+xm*20+2, 30-ym*20-2, -70+xm*20+2, 30-ym*20-18
 line -70+xm*20+2, 30-ym*20-18, -70+xm*20+18, 30-ym*20-18
 line -70+xm*20+18, 30-ym*20-2, -70+xm*20+18, 30-ym*20-18
end sub   


'choose partner procedure

sub select
 dim y as int
 dim id1 as long
 dim p as long
 dim buf1[3] as char
 dim name[20] as char
 for i=0 to 2 
  buf1[i]=55
 next

 y=2
 ink 3
'output the message
 printxy 22,28,"I want" 
 printxy 18,15,"  to "
 printxy 20,2," invite"
 printxy 25,-11," wait"
 printxy 11,y,">        <"
'choose who will be waiting 
'for a link
 while readk<>272
  if (readk=265)or(readk=267) then
   ink 0
   printxy 11,y,">        <"
   ink 3
   if y=2 then 
    y=-11
   else
    y=2
   end if
   print
   printxy 11,y,">        <"
  end if
  wait 5
  readk=key              
 wend
'delete the message
 ink 0
 printxy 22,28,"I want" 
 printxy 18,15,"  to"
 printxy 20,2," invite"
 printxy 25,-11," wait"
 printxy 11,y,">        <"
'output the message
 ink 3
 printxy 10,10," Wait..."

 if y=2 then
  cls
  input "Enter name:",name
  id=get_id_user(name)
  par1=rnd(1)
  if par1 then
   com=88
   ch=48
  else
   com=48
   ch=88
  end if 
  id1=0
  prepare
  printxy 10,10," Wait..."
  while (id1<>id)or(com<>2)
   post id,com,com,0,buf1
   receive id1,com,par2,p,buf
  wend 
 else
  com=0
  while (com<>48) and (com<>88)
   receive id,com,par1,par2,buf
  wend
  ink 3
  ch=com
  com=2
  for i=1 to 5 
   post id,2,10,10,buf 
  next     
 end if                     
'delete the message
 ink 0
 printxy 10,10," Wait..."   
 
 if ch=48 then
  wt
 end if   
end sub


'ready to game start procedure
'draw the playing field and set
'the inital parameters points

sub prepare
'initialize the field
 for i=0 to 2
  for j=0 to 2
   matr[3*i+j]=0
  next
 next

'draw the field
 ink 1
 for i=-45 to 45
  line -85,i,0,i
 next
 ink 0 
 for i=-30 to 30
  line -70,i,-10,i
 next
 ink 1
 for i=0 to 90 step 5
  line 5+i,42,-5+i,32
  line 5+i,-32,-5+i,-42
  line 5+i,32,-5+i,42
  line 5+i,-42,-5+i,-32
 next

 ink 3 
 for i=0 to 3
  line -70+i*20,30, -70+i*20,-30
 next
 for i=0 to 3
  line -70,30-i*20, -10,30-i*20
 next
 xm=1
 ym=1
 ink 2
'draw the cursor
 line -70+xm*20+2, 30-ym*20-2, -70+xm*20+18, 30-ym*20-2
 line -70+xm*20+2, 30-ym*20-2, -70+xm*20+2, 30-ym*20-18
 line -70+xm*20+2, 30-ym*20-18, -70+xm*20+18, 30-ym*20-18
 line -70+xm*20+18, 30-ym*20-2, -70+xm*20+18, 30-ym*20-18
 
 xp=-63
 yp=24
 bool=0
end sub


'check the draw (game result) 
'function
function draw as int
 dim d as int
 d=1
 for i=0 to 2
  for j=0 to 2
   d=d*matr[3*i+j]
  next
 next
'if there are the empty fields
'remaining on the playing 
'field then returns 0 
 draw=d
end function


'the function of reaction on 
'the next move and giving the
'turn to a partner 

function enter as int
 dim buf1[3] as char

 enter=0
 if matr[3*ym+xm]=0 then
  ink 3
'draw the move
  printxy -63+xm*20,24-ym*20,ch
  matr[3*ym+xm]=1
  buf1[0]=xm
  buf1[1]=ym
  if win() then
'if the user won, then send
'message to the user about this  
'delete old message
   ink 0
   printxy 8,10,"Your move" 
   ink 3
'write new message
   beep 4
   printxy 30,25,"YOU"
   printxy 17,10,"ARE THE"
   printxy 20,-5,"WINNER!"
   enter=1
   buf1[2]=10
   for i=1 to 10
    post id,1,0,0,buf1
   next
   exit function
  end if
 
  if draw() then
'if draw (game result) then 
'send message about this to 
'user
'delete old message
   ink 0
   printxy 8,10,"Your move" 
   ink 3
'write new message
   printxy 27,10,"DRAW"
   enter=1
   buf1[2]=20
   for i=1 to 10
    post id,1,0,0,buf1
   next
   exit function
  end if
  enter=2
  for i=1 to 10
   post id,1,0,0,buf1
  next             
 else
'if the field is occupied, 
'send a message about this
  beep 1
  ink 0
  printxy -60,-32,"Oops!!!"
  bool=1
  exit function 
 end if
end function   


'move cursor function

function move as int
 dim e as int
 dim x as int
 dim y as int

 move=0
 x=xm
 y=ym
'if key <Left> is pressed,
'move the cursor left
 if readk=264 then 
  if  xm=0 then
   xm=2
  else
   xm=xm-1
  end if
 end if
'if key <Right> is pressed,
'move the cursor right
 if readk=266 then 
  if  xm=2 then
   xm=0
  else
   xm=xm+1
  end if
 end if
'if key <Up> is pressed,
'move the cursor up
 if readk=265 then 
  if  ym=0 then
   ym=2
  else
   ym=ym-1
  end if
 end if 
'if key <Down> is pressed,
'move the cursor down
 if readk=267 then 
  if  ym=2 then
   ym=0
  else
   ym=ym+1
  end if
 end if 
 if bool then
'if there was a message about an
'illegal move, then delete it
  ink 1
  printxy -60,-32,"Oops!!!"
  bool=0
 end if       
'redraw the cursor 
 if ((readk>263) and (readk<268)) then  
'delete the old position
  ink 0
  line -70+x*20+2, 30-y*20-2, -70+x*20+18, 30-y*20-2
  line -70+x*20+2, 30-y*20-2, -70+x*20+2, 30-y*20-18
  line -70+x*20+2, 30-y*20-18, -70+x*20+18, 30-y*20-18
  line -70+x*20+18, 30-y*20-2, -70+x*20+18, 30-y*20-18
'draw the new position
  ink 2
  line -70+xm*20+2, 30-ym*20-2, -70+xm*20+18, 30-ym*20-2
  line -70+xm*20+2, 30-ym*20-2, -70+xm*20+2, 30-ym*20-18
  line -70+xm*20+2, 30-ym*20-18, -70+xm*20+18, 30-ym*20-18
  line -70+xm*20+18, 30-ym*20-2, -70+xm*20+18, 30-ym*20-18
 else
  e=enter()
  if e then
   move=e
  end if
 end if 
end function


'check if there is a win

function win as int
 win=0
 if ((matr[0]*matr[1]*matr[2])=1) or ((matr[3]*matr[4]*matr[5])=1) or ((matr[6]*matr[7]*matr[8])=1) then
  win=1
 end if
 if ((matr[0]*matr[3]*matr[6])=1) or ((matr[1]*matr[4]*matr[7])=1) or ((matr[2]*matr[5]*matr[8])=1) then
  win=1
 end if
 if ((matr[0]*matr[4]*matr[8])=1) or ((matr[2]*matr[4]*matr[6])=1) then
  win=1
 end if
end function

'game start
load "radio.bs.dl"
prepare
select

'output the message 
ink 3
printxy 8,10,"Your move"
for i=1 to 1 step 0
'read the code of the pressed
'key
 readk=key
'if the admissible key is 
'pressed then react upon it
 if ((readk>263) and (readk<268)) or (readk=272) then
  m=move() 
  if m=1 then
   exit for
  end if
  if m=2 then
   ink 0
   printxy 8,10,"Your move"
   wt
   ink 3
   if buf[2]=10 then
    beep 7
    printxy 10,10,"You lose."
    exit for
   end if
   if buf[2]=20 then
    printxy 27,10,"Draw."
    exit for
   end if
   ink 3
   printxy 8,10,"Your move"
  end if
 end if 
next 
wait