printxy -35,10,"Wait please..."
type string
  stri[4] as char
end type
type str
  str[2] as char
end type
dim col as int
'auxiliary vars
dim str0 as char
dim str1 as char
dim str2 as char
dim str3 as char
'the var storing the number of
'attempts that were made
dim try as int
'the array storing the results
'of attempts
'the auxiliary var
dim bc[15] as int
'the array storing the
'mentioned numbers
dim num[15] as string
'the array storing the next
'analyzable number in the
'character expression
dim tn[4] as char
'the number of bulls
dim b as int
'the number of cows
dim c as int

dim vs as int
dim res as int
'the arrays storing the data
'about previous attempts

dim inp[15] as string
dim ubc[15] as str
'the number that the Cybiko
'computer proposed
dim rands[4] as char
dim x
dim y
dim z


dim buf[4] as char
dim buf1[4] as char
'the partners device number
dim id as long
dim readk as int
dim com as int
'the var, defining the game mode
dim sp as int

dim id1 as long
dim p1 as long
dim p2 as long

'waiting for opponents move
'and input the results of 
'his/her attempt
function wt as int
 dim id1 as long
 dim bl as int
 dim p1 as long
 dim p2 as long
 dim cc as int

 id1=0
 bl=1
 while bl
  receive id1,com,p1,p2,buf1
'check the received information
  cc=compare(buf1,buf)
  if (id1=id) and (cc<>0) and (com=2) then
   bl=0
   buf=buf1
  end if
 wend

 cls 
 tn=buf
 prinp
 p1=c
 p2=b   
 for i=1 to 5
  post id,3,p1,p2,"1111" 
 next
 wt=p2      
end function

'choose the game type
function selpar
 dim y as int
 ink 3
 y=2
 printxy -25,28,"I want" 
 printxy -40,15,"to play with"
 printxy -30,2,"computer"
 printxy -25,-11,"partner"
 printxy -38,y,">          <"
'process the key pressing
 while readk<>272
  if (readk=265)or(readk=267) then
   ink 0
   printxy -38,y,">          <"
   ink 3
   if y=2 then 
    y=-11
   else
    y=2
   end if
   printxy -38,y,">          <"
  end if
  wait 5
  readk=key              
 wend
 selpar=y
 cls
 readk=0
end function

'choose the opponent
sub select
 dim y as int
 dim id1 as long
 dim p1 as long
 dim p2 as long
 dim par1 as long
 buf1="9999"
 y=7
 ink 3
 printxy -36,20,"I want to"
 printxy -24,7,"invite"
 printxy -19,-6,"wait"
 printxy -38,y,">        <"
 while readk<>272
  if (readk=265)or(readk=267) then
   ink 0
   printxy -38,y,">        <"
   ink 3
   if y=7 then 
    y=-6
   else
    y=7
   end if
   printxy -38,y,">        <"
  end if
  wait 5
  readk=key              
 wend
 cls
 ink 3
 printxy -35,10,"Wait please..."
 par1=0
 if y=7 then
  id=select()
  par1=rnd(1)
  id1=0
  while (compare(buf,buf1)<>0)
   post id,1,par1,0,buf1
   receive id1,com,p1,p2,buf
  wend 
 else
  com=0
  while compare(buf,buf1)<>0
   receive id,com,p1,p2,buf        
  wend
  if p1=0 then
   par1=1
  end if
  ink 3
  com=2
  for i=1 to 5 
   post id,2,10,10,"9999" 
  next     
 end if                     
 cls

 if par1 then
  ink 3
  printxy -35,10,"Wait please..."
  res=wt()
 end if   
end sub
'procedure
'of guessing a sequence



sub rand
 for i=0 to 3 
  if i=0 then
   rands[i]=rnd(8)+49
  else
   rands[i]=rnd(9)+48
  end if
  for j=0 to i-1
   if rands[j]=rands[i] then
    i=i-1
    exit for
   end if
  next
 next   
 cls
end sub


'reaction to winning
sub win
 dim bool as int
 cls
 print "         You win"   
 print "    from the ",try," attempts."
 print
 print"      Press any key"
 print"       to continue."
 z=3
 beep 7
 wait(10) 
 for i=0 to 18
  x=10*sin(i)
  y=10*cos(i)
  ink 1
  line 0,0,x,y
  ink 2
  line 60,23,x+60,y+23
  if z=1 then
   z=3
  else
   z=1
  end if
  beep z
  ink 3
  line 60,-23,x+60,y-23
  ink 1
  line -60,23,x-60,y+23
  ink 2
  line -60,-23,x-60,y-23
  if key<>0 then
   bool=1
   exit for
  end if
 next
 ink 3
end sub


'reaction to a loss
sub lose  
 dim bool as int 
 cls
 print
 print "        You lose."
 if sp=2 then
  print " The sequence was ",rands,"."
 end if
 print "     Press any key"
 print "      to continue."
 beep 4
 for i=1 to 400 step 2
  line -80,45,-75,45-i*2
  line 30,45,27,45-i
  line -35,45,-40,45-i
  line 15,45,10,45-i*2.5
  line 0,45,5,45-i
  if i>9 then
   line -63,45,-70,55-i
   line -5,45,-10,55-i/1.5
   if key<>0 then
    bool=1
    exit for
   end if
   line 65,45,70,55-i
   line -50,45,-45,55-i/1.2
  end if
  if i>19 then
   line -55,45,-60,65-i/2
   line -25,45,-20,65-i
   line 80,45,75,65-i/1.5
   line 50,45,45,65-i
  end if
  if key<>0 then
   bool=1
   exit for
  end if
 next
end sub

'input by the user of the next
'order of figures procedure
sub user_move
 dim bool as int
 cls
 if try>1 then
  print "     try number B:C"
  for i=1 to try-1
   print "      ",i,"   ",inp[i].stri,"   ",ubc[i].str[0]," ",ubc[i].str[1] 
  next
  print
 else
  printxy -40,10,"Try number 1"
 end if
 for p=1 to 1 step 0   
  input "  Enter your sequence: ",inp[try].stri
  bool=0
  for k=0 to 2
   for l=k+1 to 3
    if inp[try].stri[k]=inp[try].stri[l] then
     bool=1
    end if
   next
  next

'for a check for accuracy
'of the entered symbols
'of numbers
  for l=0 to 3 
   if (inp[try].stri[l]<48) or (inp[try].stri[l]>57) then
    bool=1
   end if
  next
  if bool or (inp[try].stri[0]=48) then
'for a reaction for entering
'the wrong sequence
   cls
   print
   print
   print " You entered the wrong"
   print "  sequence. Try again."
   wait 15
  else

'for a comparison
'between the entered sequence
'and the guessed sequence
   if sp=2 then
    for k=0 to 3
     for l=0 to 3
      if rands[k]=inp[try].stri[l] then
       ubc[try].str[1]=ubc[try].str[1]+1
      end if
     next
    next
    for k=0 to 3
     if rands[k]=inp[try].stri[k] then
      ubc[try].str[0]=ubc[try].str[0]+1
     end if
    next
    ubc[try].str[1]=ubc[try].str[1]-ubc[try].str[0]
    ubc[try].str[0]=ubc[try].str[0]+48
    ubc[try].str[1]=ubc[try].str[1]+48
    cls
    printxy -20,10,ubc[try].str[0]," bulls"   
    printxy -20,-10,ubc[try].str[1]," cows"
    wait 20
    cls
   end if
   exit for
  end if
 next
end sub

'Function that returns the
'number of cows and bulls,
'in the number stored in tn
'and in the next number stored
'in num. -the number of 
'analyzable elements from num.
'The value of function returns
'in the next view: the number
'of bulls*10+the number of cows

function fbc(x as int) as int
'in vars str0..str3 are written
'the category of analyzable
'number stored in num
'to hasten the program
'processing 
str0=num[x].stri[0]      
 str1=num[x].stri[1]       
 str2=num[x].stri[2]
 str3=num[x].stri[3]
 fbc=55
 c=((tn[0]=str0)+(tn[1]=str1)+(tn[2]=str2)+(tn[3]=str3))*10
 if ( c>bc[x])OR(c<bc[x]-10) then
  exit function
 end if
 fbc=c+(tn[0]=str1)+(tn[0]=str2)+(tn[0]=str3)+(tn[1]=str2)+(tn[1]=str3)+(tn[1]=str0)+(tn[2]=str3)+(tn[2]=str0)+(tn[2]=str1)+(tn[3]=str0)+(tn[3]=str1)+(tn[3]=str2)
end function


'the procedure of a reaction
'to the incorrect inputted
'number of cows and number of
'bulls 
sub bad_inp
 printxy -50,-5,"Bad input value."
 printxy -40,-15,"  Try again."
 wait 20
 ink 0
 printxy -50,-5,"Bad input value."
 printxy -40,-15,"  Try again."
 ink 3
end sub

'the procedure of the outputting
'of the next attempt on the
'screen  and saving its results
sub prinp
 dim bool as int
 cls
 bool=1
 c=0
 printxy -40,35,"My number is"
 printxy -15,15,tn
 while bool
  input "How many bulls?",b
'check that the number of bulls
'inputted is correct 
  if (b>4) or (b<0) then
   bad_inp
  else
   bool=0
  end if
 wend
 bool=1
 if b<>4 then
  while bool
   input "How many cows?",c
'check that the number of cows
'inputted is correct
   if (c>(4-b)) or (c<0) then            
    bad_inp
   else
    bool=0
   end if
  wend
 else
  c=0
 end if

'enter in num the next attempt
mid num[try].stri,tn,0,4
'enter in bc the attempt
'result           
 bc[try]=b*10+c                        
 cls
end sub

'the procedure of defining the
'position of the reading from
'file 1
function read(try as int) as int
 dim kor as int
 dim v as int
 for i=0 to 3
  if i<>3 then
   kor=4-i
  else
   kor=0
  end if
  for j=0 to kor
   if (i*10+j)=(bc[try]) then
    read=v
    exit function
   else
    v=v+1
   end if
  next
 next
end function


sub main
'the logical var
 dim bool as int
'the var storing the position
'of reading from file 1
dim pos as int
'the var storing the cardinal
'number of possible numbers
dim cap as int
'open the auxiliary file
 open "numbers.bs.txt" for read as 1      
 try=1
 user_move
 if ubc[try].str[0]=52 then
  win
  exit sub
 end if

'reading the first number
'from file
get 1,,tn[0]          
 get 1,,tn[1]
 get 1,,tn[2]
 get 1,,tn[3]
 prinp

'check that the number input
'on the attempt coincides with
'proposed number 
if bc[try]=40 then     
  exit sub
 end if

'the position defining in
'file 1 for reading the
'second number
pos=(read(1)+1)*4 
 try=2
 user_move
 if ubc[try].str[0]=52 then
  win
  exit sub
 end if

'reading the second number
'from a file 
get 1,pos,tn[0]            
 get 1,,tn[1]
 get 1,,tn[2]
 get 1,,tn[3]
 prinp
 if bc[try]=40 then
  exit sub
 end if

'defining the new position in
'file 1
pos=(pos-4)*13+read(2)*4+56    
'reading from a file the
'cardinal number of possible
'numbers
 get 1,pos+2,cap                         
 get 1,pos,pos
 pos=pos+676+56
 get 1,pos

 while cap>0
  if bc[try]=40 then
   exit sub
  end if

'reading from a file the next
'number
  get 1,,tn[0]            
  get 1,,tn[1]
  get 1,,tn[2]
  get 1,,tn[3]

  if col=0 then
   col=3
  else
   col=0
  end if
  ink col
  printxy -40,10,"I am thinking"
  ink 3
  bool=1

'review of all previous attempts
  for i=1 to try-1
   if fbc(i)<>bc[i] then 
    bool=0
    exit for
   end if
  next

  if bool then
   try=try+1
   user_move
   if ubc[try].str[0]=52 then
    win
    exit sub
   end if
  
   prinp
  end if
  cap=cap-1
 wend
end sub

'the basic procedure of the
'game with a computer
sub main1
 dim bl as int
 dim c as int

 res=0

 select
 try=1
 c=1
 while c
  if res=4 then
   ink 3
   lose
   exit sub
  end if
  user_move
  cls
  printxy -35,10,"Wait please..."
  for i=1 to 5
   post id,2,0,0,inp[try].stri 
  next      
  id1=0
  bl=1
  while bl
   receive id1,com,p1,p2,buf1
    if (id1=id) and (compare(buf1,"1111")=0) then
     bl=0
    end if
  wend
  cls
  ink 3

  ubc[try].str[0]=p2+48
  ubc[try].str[1]=p1+48
  cls
  print "     try number B:C"
  for i=1 to try
   print "      ",i,"   ",inp[i].stri,"   ",ubc[i].str[0]," ",ubc[i].str[1] 
  next
  print"         Wait please..."           
  if p2=4 then
   c=0
   ink 3
   win
   exit sub
  end if
  res=wt()
  try=try+1
 wend
end sub  
   

load "radio.bs.dl"
cls
'choose the game mode
sp=selpar()
'check the chosen game mode
if sp=2 then
 printxy -35,10,"Wait please..."
 rand
'call the game with computer
'procedure 
main
 cls
 close
 if (bc[try]<>40)and(ubc[try].str[0]<>52) then
'the user has mistaken
 print "        You have"
 print "      made an error."
 print "         See it."
 wait 20
 cls
 print "   num     bulls   cows"
 for j=1 to try
  vs=bc[i]/10-0.5
  print "  ",num[j].stri,"      ",vs,"      ",bc[j]-vs
  wait 15
 next
 print "     Press any key"
 print "      to continue..."
 for j=1 to 1 step 0
  if key<>0 then
   exit for
  end if
 next
end if
if  bc[try]=40 then
 lose
 beep 6
 wait 5
 beep 1
end if
else 
'call the game with partner
'procedure
 main1
end if