' add
' Programmed by S.Shlien in GFA Basic 3.5  Jan 31 1993
' 624 Courtenay Avenue
' Ottawa, Ontario
' Canada K2A 3B5
'
' board& indicates presence of orange or yellow block or barrier
' if it an yellow block board& is = 1.
' if it is a barrier, board& is = 2.
' if it is an orange block board& contains 3 and board_ans& contains.
' the answer.
' board_ans& contains the desired answer next to the yellow block
' the answer location is marked by board&=4  and board_ans& contains
' the desired answer.
DIM board&(20,14)
DIM board_ans&(20,14)
' x_prob%,y_prob% points to top left square of yellow question block.
' d_prob% indicates whether the block goes across or down.
DIM x_prob%(100),y_prob%(100),d_prob%(100)
DIM deskcolors%(16)
DIM difficulty$(2),operation$(2)
DIM help$(10)
DIM frame%(10000)
DIM frame_ptr%(2)
frame_ptr%(1)=VARPTR(frame%(0))
frame_ptr%(1)=(frame_ptr%(1)+256) AND &HFFFFFF00
frame_ptr%(0)=XBIOS(2)
difficulty$(0)="easy"
difficulty$(1)="harder"
difficulty$(2)="hardest"
operation$(0)="addition"
operation$(1)="mulitplication"
help$(0)="Select option with mouse pointer"
help$(1)="How to run this program"
help$(2)="Sets the largest number"
help$(3)="Add or multiply"
help$(4)="Next file to load"
help$(5)="Joystick delay time"
help$(7)="Lets go"
help$(8)="I am fed up."
DEFMOUSE 0
'
rez%=XBIOS(4)
IF rez%<>0
  ALERT 3," Please switch   to | low  resolution! ",1,"Oops",b%
  STOP
ENDIF
wid%=16
height%=12
xdim%=18
ydim%=12
ytop%=20
xleft%=20
file_num%=1
file_write%=20
difficulty%=0
operation%=0
joystick_response=0.25
@read_put
@get_deskcolors
@load_degas_pi1("addbak.pi1")
' VSETCOLOR 0,10,10,8
VSETCOLOR 2,8,8,4
VSETCOLOR 5,12,10,10
VSETCOLOR 6,14,14,0
VSETCOLOR 7,14,10,0
VSETCOLOR 8,10,8,8
prob_num%=1
ans_num%=1
pnum%=0
i_explorer%=5
j_explorer%=5
quit%=0
REPEAT
  @select_parameters
UNTIL quit%=1
@restore_deskcolors
END
> PROCEDURE get_deskcolors
  LOCAL i%
  FOR i%=0 TO 15
    deskcolors%(i%)=XBIOS(7,i%,-1)
  NEXT i%
RETURN
> PROCEDURE restore_deskcolors
  LOCAL i%
  FOR i%=0 TO 15
    SETCOLOR i%,deskcolors%(i%)
  NEXT i%
RETURN
> PROCEDURE read_put
  ' the board
  OPEN "i",#1,"add.put"
  add$=INPUT$(CVI(INPUT$(2,#1)),#1)
  CLOSE #1
RETURN
'
> PROCEDURE make_board
  LOCAL i%
  CLS
  @random_pattern(10,10,319,150)
  COLOR 1
  DEFFILL 5
  PBOX xleft%,ytop%,xleft%+xdim%*wid%,ytop%+ydim%*height%
  FOR i%=0 TO wid%
    LINE xleft%+xdim%*i%,ytop%,xleft%+xdim%*i%,ytop%+height%*ydim%
  NEXT i%
  FOR i%=0 TO height%
    LINE xleft%,ytop%+ydim%*i%,xleft%+xdim%*wid%,ytop%+ydim%*i%
  NEXT i%
  GRAPHMODE 2
RETURN
'
> PROCEDURE design_row
  LOCAL i%,j%,ix%,iy%,k%,ok%
  REPEAT
  UNTIL MOUSEK<>0
  i%=(MOUSEX-xleft%)/xdim%
  j%=(MOUSEY-ytop%)/ydim%
  IF i%<0
    i%=0
  ENDIF
  IF j%<0
    j%=0
  ENDIF
  ok%=1
  IF (j%>=height%)
    ok%=0
  ENDIF
  IF (i%>=wid%-3)
    ok%=0
  ENDIF
  FOR k%=0 TO 3
    IF board&(i%+k%,j%)>0
      ok%=0
    ENDIF
  NEXT k%
  IF ok%=1
    ix%=i%*xdim%+xleft%
    iy%=j%*ydim%+ytop%
    @draw_design_row(i%,j%,ix%,iy%,pnum%)
    x_prob%(pnum%)=i%
    y_prob%(pnum%)=j%
    d_prob%(pnum%)=1
    INC prob_num%
    INC pnum%
    LOCATE 1,1
    PRINT SPACE$(10);
    PAUSE 20
  ELSE
    LOCATE 1,1
    PRINT "sorry;"
  ENDIF
RETURN
> PROCEDURE draw_design_row(i%,j%,ix%,iy%,n%)
  LOCAL k%
  DEFTEXT 3
  COLOR 3
  BOX ix%,iy%,ix%+4*xdim%,iy%+ydim%
  TEXT ix%+1,iy%+8,STR$(prob_num%)
  FOR k%=0 TO 3
    board&(i%+k%,j%)=n%+1
  NEXT k%
RETURN
> PROCEDURE design_column
  LOCAL i%,j%,ix%,iy%,k%,ok%
  REPEAT
  UNTIL MOUSEK<>0
  i%=(MOUSEX-xleft%)/xdim%
  j%=(MOUSEY-ytop%)/ydim%
  ok%=1
  IF i%<0
    i%=0
  ENDIF
  IF j%<0
    j%=0
  ENDIF
  IF (j%>=height%-2)
    ok%=0
  ENDIF
  IF (i%>=wid%)
    ok%=0
  ENDIF
  FOR k%=0 TO 2
    IF board&(i%,j%+k%)>0
      ok%=0
    ENDIF
  NEXT k%
  IF (ok%=1)
    ix%=i%*xdim%+xleft%
    iy%=j%*ydim%+ytop%
    @draw_design_col(i%,j%,ix%,iy%,pnum%)
    x_prob%(pnum%)=i%
    y_prob%(pnum%)=j%
    d_prob%(pnum%)=2
    INC pnum%
    INC prob_num%
    PAUSE 20
  ELSE
    LOCATE 1,1
    PRINT "sorry;"
  ENDIF
RETURN
> PROCEDURE draw_design_col(i%,j%,ix%,iy%,n%)
  LOCAL k%
  COLOR 3
  DEFTEXT 3
  BOX ix%,iy%,ix%+xdim%,iy%+3*ydim%
  TEXT ix%+1,iy%+8,STR$(prob_num%)
  FOR k%=0 TO 2
    board&(i%,j%+k%)=n%+1
  NEXT k%
RETURN
> PROCEDURE design_ans
  LOCAL i%,j%,ix%,iy%,ok%
  REPEAT
  UNTIL MOUSEK<>0
  i%=(MOUSEX-xleft%)/xdim%
  j%=(MOUSEY-ytop%)/ydim%
  IF i%<0
    i%=0
  ENDIF
  IF j%<0
    j%=0
  ENDIF
  ok%=1
  IF (j%>=height%)
    ok%=0
  ENDIF
  IF (i%>=wid%)
    ok%=0
  ENDIF
  IF board&(i%,j%)<>0
    ok%=0
  ENDIF
  IF ok%=1
    ix%=i%*xdim%+xleft%
    iy%=j%*ydim%+ytop%
    x_prob%(pnum%)=i%
    y_prob%(pnum%)=j%
    d_prob%(pnum%)=3
    @draw_design_answer(i%,j%,ix%,iy%,pnum%)
    INC ans_num%
    INC pnum%
    LOCATE 1,1
    PRINT SPACE$(8)
    PAUSE 20
  ELSE
    LOCATE 1,1
    PRINT "sorry";
  ENDIF
RETURN
> PROCEDURE draw_design_answer(i%,j%,ix%,iy%,n%)
  COLOR 3
  DEFTEXT 3
  BOX ix%,iy%,ix%+xdim%,iy%+ydim%
  TEXT ix%+1,iy%+8,STR$(ans_num%)
  board&(i%,j%)=n%+1
RETURN
> PROCEDURE design_block
  LOCAL i%,j%,ix%,iy%
  REPEAT
  UNTIL MOUSEK<>0
  i%=(MOUSEX-xleft%)/xdim%
  j%=(MOUSEY-ytop%)/ydim%
  IF i%<0
    i%=0
  ENDIF
  IF j%<0
    j%=0
  ENDIF
  IF i%<wid% AND j%<height% AND board&(i%,j%)=0
    ix%=i%*xdim%+xleft%
    iy%=j%*ydim%+ytop%
    draw_design_block(i%,j%,ix%,iy%,pnum%)
    x_prob%(pnum%)=i%
    y_prob%(pnum%)=j%
    d_prob%(pnum%)=4
    INC pnum%
    LOCATE 1,1
    PRINT SPACE$(10);
    PAUSE 20
  ELSE
    LOCATE 1,1
    PRINT "sorry";
  ENDIF
RETURN
> PROCEDURE draw_design_block(i%,j%,ix%,iy%,n%)
  COLOR 3
  DEFTEXT 3
  BOX ix%,iy%,ix%+xdim%,iy%+ydim%
  board&(i%,j%)=n%+1
RETURN
> PROCEDURE design_erase
  LOCAL i%,j%,ix%,iy%,k%
  REPEAT
  UNTIL MOUSEK<>0
  i%=(MOUSEX-xleft%)/xdim%
  j%=(MOUSEY-ytop%)/ydim%
  IF i%<0
    i%=0
  ENDIF
  IF j%<0
    j%=0
  ENDIF
  k%=board&(i%,j%)
  LOCATE 10,1
  IF k%>0
    '    PRINT k%;" ";d_prob%(k%-1);
    IF d_prob%(k%-1)=1
      DEC prob_num%
    ELSE IF d_prob%(k%-1)=2
      DEC prob_num%
    ELSE IF d_prob%(k%-1)=3
      DEC ans_num%
    ENDIF
    IF pnum%>0
      DELETE x_prob%(k%-1)
      DELETE y_prob%(k%-1)
      DELETE d_prob%(k%-1)
      DEC pnum%
      @draw_design_set
    ENDIF
    PAUSE 20
  ENDIF
RETURN
> PROCEDURE design_clear
  prob_num%=1
  ans_num%=1
  pnum%=0
  @clear_board
  @make_board
  @design_menu
RETURN
> PROCEDURE design_explorer
  LOCAL i%,j%,ok%
  DEFFILL 0
  DEFTEXT 1
  PBOX 0,168,319,199
  TEXT 0,180,"Explorer's starting position."
  REPEAT
  UNTIL MOUSEK<>0
  i%=(MOUSEX-xleft%)/xdim%
  j%=(MOUSEY-ytop%)/ydim%
  i_explorer%=i%
  j_explorer%=j%
  i_explorer_init%=i%
  j_explorer_init%=j%
  PAUSE 30
RETURN
> PROCEDURE design_menu
  LOCAL i%
  COLOR 1
  BOX 0,168,28,182
  BOX 28,168,58,182
  BOX 58,168,88,182
  BOX 88,168,134,182
  BOX 134,168,180,182
  BOX 180,168,224,182
  BOX 224,168,268,182
  FOR i%=1 TO 7
    highlight_menu_element(i%,0)
  NEXT i%
RETURN
'
> PROCEDURE highlight_menu_element(elem%,high%)
  IF high%=1
    DEFTEXT 4
  ELSE
    DEFTEXT 1
  ENDIF
  SELECT elem%
  CASE 1
    TEXT 1,180,"row"
  CASE 2
    TEXT 30,180,"col"
  CASE 3
    TEXT 60,180,"ans"
  CASE 4
    TEXT 90,180,"block"
  CASE 5
    TEXT 136,180,"erase"
  CASE 6
    TEXT 182,180,"clear"
  CASE 7
    TEXT 226,180,"done"
  ENDSELECT
RETURN
> PROCEDURE design_control
  LOCAL quit%,command%,old_command%
  quit%=0
  command%=0
  old_command%=0
  CLS
  INPUT "Enter width ",wid%
  INPUT "Enter height ",height%
  PAUSE 10
  CLS
  IF wid%>16
    wid%=16
  ENDIF
  IF height%>12
    height%=12
  ENDIF
  IF wid%<4
    wid%=4
  ENDIF
  IF height%<3
    height%=3
  ENDIF
  @draw_design_set
  @design_menu
  PAUSE 30
  REPEAT
    REPEAT
    UNTIL MOUSEK<>0
    IF MOUSEY>160
      IF MOUSEX<28
        command%=1
      ELSE IF MOUSEX<58
        command%=2
      ELSE IF MOUSEX<88
        command%=3
      ELSE IF MOUSEX<134
        command%=4
      ELSE IF MOUSEX<182
        command%=5
      ELSE IF MOUSEX<224
        command%=6
        @design_clear
        PAUSE 20
      ELSE
        command%=7
        quit%=1
      ENDIF
    ELSE
      SELECT command%
      CASE 1
        @design_row
      CASE 2
        @design_column
      CASE 3
        @design_ans
      CASE 4
        @design_block
      CASE 5
        @design_erase
      CASE 7
        quit%=1
      ENDSELECT
    ENDIF
    IF command%<>old_command%
      @highlight_menu_element(old_command%,0)
      @highlight_menu_element(command%,1)
      old_command%=command%
    ENDIF
  UNTIL quit%=1
  PAUSE 20
  @design_explorer
  CLS
  @show_all_parameters
RETURN
> PROCEDURE draw_design_set
  LOCAL i%,j%,k%,ix%,iy%
  @clear_board
  prob_num%=1
  ans_num%=1
  @make_board
  COLOR 1
  FOR k%=0 TO pnum%-1
    i%=x_prob%(k%)
    j%=y_prob%(k%)
    ix%=i%*xdim%+xleft%
    iy%=j%*ydim%+ytop%
    SELECT d_prob%(k%)
    CASE 1
      draw_design_row(i%,j%,ix%,iy%,k%)
      INC prob_num%
    CASE 2
      @draw_design_col(i%,j%,ix%,iy%,k%)
      INC prob_num%
    CASE 3
      @draw_design_answer(i%,j%,ix%,iy%,k%)
      INC ans_num%
    CASE 4
      @draw_design_block(i%,j%,ix%,iy%,k%)
    ENDSELECT
  NEXT k%
  @design_menu
RETURN
'
> PROCEDURE clear_board
  LOCAL i%,j%
  FOR i%=0 TO wid%
    FOR j%=0 TO height%
      board&(i%,j%)=0
    NEXT j%
  NEXT i%
RETURN
> PROCEDURE make_prob(n%)
  LOCAL ix%,iy%,n1%,n2%,maxnum%
  ix%=x_prob%(n%)*xdim%+xleft%
  iy%=y_prob%(n%)*ydim%+ytop%
  IF difficulty%=2
    maxnum%=10
    IF operation%=1
      maxnum%=9
    ENDIF
  ELSE IF difficulty%=1
    maxnum%=8
  ELSE
    maxnum%=5
  ENDIF
  n1%=RANDOM(maxnum%)
  n2%=RANDOM(maxnum%)
  IF operation%=1 AND difficulty%>0
    n1%=n1%+1
    n2%=n2%+1
  ENDIF
  DEFFILL 6
  IF d_prob%(n%)=1
    PBOX ix%+1,iy%+1,ix%+xdim%*3-1,iy%+ydim%-1
    TEXT ix%+7,iy%+9,STR$(n1%)
    ix%=(1+x_prob%(n%))*xdim%+xleft%+1
    IF operation%=0
      TEXT ix%,iy%+9,"+"+STR$(n2%)
    ELSE
      TEXT ix%,iy%+9,"x"+STR$(n2%)
    ENDIF
    ix%=(2+x_prob%(n%))*xdim%+xleft%+4
    TEXT ix%,iy%+9,"="
    IF operation%=0
      board_ans&(x_prob%(n%)+3,y_prob%(n%))=n1%+n2%
    ELSE
      board_ans&(x_prob%(n%)+3,y_prob%(n%))=n1%*n2%
    ENDIF
    board&(x_prob%(n%),y_prob%(n%))=1
    board&(x_prob%(n%)+1,y_prob%(n%))=1
    board&(x_prob%(n%)+2,y_prob%(n%))=1
    board&(x_prob%(n%)+3,y_prob%(n%))=4
    ix%=(x_prob%(n%)+3)*xdim%+xleft%
    DEFFILL 8
    PBOX ix%+1,iy%+1,ix%+xdim%-1,iy%+ydim%-1
    IF operation%=0
      @place_answer(n1%+n2%)
    ELSE
      @place_answer(n1%*n2%)
    ENDIF
  ELSE IF d_prob%(n%)=2
    PBOX ix%+1,iy%+1,ix%+xdim%-1,iy%+2*ydim%-1
    TEXT ix%+7,iy%+9,STR$(n1%)
    ix%=x_prob%(n%)*xdim%+xleft%+1
    iy%=(1+y_prob%(n%))*ydim%+ytop%+9
    IF operation%=0
      TEXT ix%,iy%,"+"+STR$(n2%)
    ELSE
      TEXT ix%,iy%,"x"+STR$(n2%)
    ENDIF
    IF operation%=0
      board_ans&(x_prob%(n%),y_prob%(n%)+2)=n1%+n2%
    ELSE
      board_ans&(x_prob%(n%),y_prob%(n%)+2)=n1%*n2%
    ENDIF
    board&(x_prob%(n%),y_prob%(n%))=1
    board&(x_prob%(n%),y_prob%(n%)+1)=1
    board&(x_prob%(n%),y_prob%(n%)+2)=4
    iy%=(2+y_prob%(n%))*ydim%+ytop%
    DEFFILL 8
    PBOX ix%,iy%+1,ix%+xdim%-1,iy%+ydim%-1
    IF operation%=0
      @place_answer(n1%+n2%)
    ELSE
      @place_answer(n1%*n2%)
    ENDIF
  ELSE IF d_prob%(n%)=4
    DEFFILL 3
    board&(x_prob%(n%),y_prob%(n%))=2
    PBOX ix%+1,iy%+1,ix%+xdim%-1,iy%+ydim%-1
  ENDIF
RETURN
> PROCEDURE place_answer(n%)
  LOCAL i%,j%,ix%,iy%,done%,kount%,k%
  done%=0
  ' search for answer if any
  kount%=0
  k%=0
  '  LOCATE 1,1
  REPEAT
    INC k%
    IF d_prob%(k%)=3
      INC kount%
    ENDIF
  UNTIL k%>=pnum%-1 OR kount%=prob_num%
  IF kount%<>prob_num%
    '    PRINT prob_num%;" ";
    REPEAT
      i%=RANDOM(wid%)
      j%=RANDOM(height%)
      IF board&(i%,j%)=0
        done%=1
      ENDIF
    UNTIL done%=1
  ELSE
    i%=x_prob%(k%)
    j%=y_prob%(k%)
  ENDIF
  board_ans&(i%,j%)=n%
  board&(i%,j%)=3
  ix%=i%*xdim%+xleft%
  iy%=j%*ydim%+ytop%
  DEFFILL 7
  PBOX ix%+1,iy%+1,ix%+xdim%-1,iy%+ydim%-1
  IF n%<10
    TEXT ix%+7,iy%+9,STR$(n%)
  ELSE
    TEXT ix%+1,iy%+9,STR$(n%)
  ENDIF
RETURN
> PROCEDURE display_problems
  LOCAL i%
  COLOR 1
  prob_num%=1
  CLS
  @clear_board
  @make_board
  DEFTEXT 4
  FOR i%=0 TO pnum%-1
    @make_prob(i%)
    IF d_prob%(i%)=1 OR d_prob%(i%)=2
      INC prob_num%
    ENDIF
  NEXT i%
  TEXT 10,180,"Press Esc key to exit"
  TEXT 10,190,"Press Undo to restart"
RETURN
'
> FUNCTION stick_handler
LOCAL i%
move%=STICK(1)
FOR i%=0 TO 20
  IF STRIG(1)=TRUE
    RETURN 71
  ENDIF
  t$=INKEY$
  IF t$<>""
    scancode%=CVI(t$)
    IF ASC(t$)=0
      RETURN scancode%
    ELSE
      RETURN ASC(t$)
    ENDIF
  ENDIF
NEXT i%
SELECT move%
CASE 1
  RETURN 72
CASE 2
  RETURN 80
CASE 4
  RETURN 75
CASE 8
  RETURN 77
DEFAULT
ENDSELECT
RETURN 0
ENDFUNC
> PROCEDURE shift_explorer(scancode%)
LOCAL i%,j%,k%,m%,index%,last_index%,code|,temp%
i%=i_explorer%
j%=j_explorer%
'  PRINT scancode%;" ";
SELECT scancode%
CASE 71
  LOCATE 1,1
  PRINT SPACE$(4);
  IF carry%=-1
    IF board&(i%,j%)=3
      carry%=board_ans&(i%,j%)
      ix%=i%*xdim%+xleft%+1
      iy%=j%*ydim%+ytop%+1
      GET ix%,iy%,ix%+xdim%-2,iy%+ydim%-2,ansblock$
      board&(i%,j%)=0
      COLOR 4
      @draw_explorer
      PAUSE 3
      COLOR 3
      @draw_explorer
      WAVE 512*8+8,1,1,2000,20
      SOUND 0,0
    ENDIF
  ELSE IF board&(i%,j%)=0
    board_ans&(i%,j%)=carry%
    board&(i%,j%)=3
    carry%=-1
    COLOR 2
    @draw_explorer
    PAUSE 2
    COLOR 3
    @draw_explorer
    WAVE 512*15+8,1,1,2000,20
    SOUND 0,0
  ELSE IF board&(i%,j%)=4
    IF carry%=board_ans&(i%,j%)
      DEFFILL 6
      PBOX ix%+1,iy%+1,ix%+xdim%-1,iy%+ydim%-1
      DEFTEXT 4
      IF carry%<10
        TEXT ix%+7,iy%+9,STR$(carry%)
      ELSE
        TEXT ix%+1,iy%+9,STR$(carry%)
      ENDIF
      WAVE 512*5+8,1,1,2000,40
      SOUND 0,0
      carry%=-1
      board&(i%,j%)=1
      INC num_done%
      lastblock$=""
      IF num_done%=prob_num%
        GOTO shift_exit
      ENDIF
    ELSE
      LOCATE 1,1
      PRINT "No";
    ENDIF
  ENDIF
  DELAY 0.1
CASE 72
  DEC j%
CASE 75
  DEC i%
CASE 77
  INC i%
CASE 80
  INC j%
CASE 97
  @display_problems
  num_done%=1
  i%=i_explorer_init%
  j%=j_explorer_init%
CASE 27
  exit%=1
DEFAULT
  LOCATE 1,20
  PRINT scancode%;
ENDSELECT
IF j%>=0 AND j%<height%
  IF i%>=0 AND i%<wid%
    IF board&(i%,j%)=0 OR (board&(i%,j%)=3 AND carry%=-1) OR board&(i%,j%)=4
      DEFTEXT 5
      COLOR 1
      @draw_explorer
      IF carry%<>-1
        ix%=i_explorer%*xdim%+xleft%
        iy%=j_explorer%*ydim%+ytop%
        IF lastblock$=""
          DEFFILL 5
          PBOX ix%+1,iy%+1,ix%+xdim%-1,iy%+ydim%-1
        ELSE
          PUT ix%+1,iy%+1,lastblock$
        ENDIF
      ENDIF
      i_explorer%=i%
      j_explorer%=j%
      IF carry%<>-1
        ix%=i_explorer%*xdim%+xleft%
        iy%=j_explorer%*ydim%+ytop%
        DEFFILL 5
        GET ix%+1,iy%+1,ix%+xdim%-1,iy%+ydim%-1,lastblock$
        PUT ix%+1,iy%+1,ansblock$
      ENDIF
      COLOR 3
      DEFTEXT 4
      @draw_explorer
    ENDIF
  ENDIF
ENDIF
shift_exit:
RETURN
> PROCEDURE draw_explorer
LOCAL ix%,iy%
ix%=i_explorer%*xdim%+xleft%
iy%=j_explorer%*ydim%+ytop%
BOX ix%,iy%,ix%+xdim%,iy%+ydim%
'  IF carry%>0
' TEXT ix%+7,iy%+9,STR$(carry%)
' ENDIF
RETURN
> PROCEDURE run_game
carry%=-1
num_done%=1
SETMOUSE 199,319
exit%=0
COLOR 3
@draw_explorer
lastblock$=""
HIDEM
REPEAT
  scancode%=@stick_handler
  IF scancode%<>0
    shift_explorer(scancode%)
    DELAY joystick_response
  ENDIF
  STICK 0
UNTIL num_done%=prob_num% OR exit%=1
'  @show_all_parameters
RETURN
> PROCEDURE write_prob_on_disk
' records problem
LOCAL i%
FILESELECT "*.dat","add"+STR$(file_write%)+".dat",name$
OPEN "o",#1,name$
IF name$<>""
  PRINT #1,wid%
  PRINT #1,height%
  PRINT #1,pnum%
  PRINT #1,i_explorer%
  PRINT #1,j_explorer%
  FOR i%=0 TO pnum%-1
    PRINT #1,x_prob%(i%)
    PRINT #1,y_prob%(i%)
    PRINT #1,d_prob%(i%)
  NEXT i%
  INC file_write%
  CLOSE #1
ENDIF
RETURN
> PROCEDURE read_prob_from_disk
LOCAL i%,name$
'  IF nsongs%>0
' i%=RANDOM(nsongs%)
' @dosound(i%)
' ENDIF
@clear_board
name$="add"+STR$(file_num%)+".dat"
IF EXIST(name$)
  TEXT 1,8,"loading "+name$
  OPEN "i",#1,name$
  INPUT #1,wid%
  INPUT #1,height%
  INPUT #1,pnum%
  INPUT #1,i_explorer_init%
  INPUT #1,j_explorer_init%
  FOR i%=0 TO pnum%-1
    INPUT #1,x_prob%(i%)
    INPUT #1,y_prob%(i%)
    INPUT #1,d_prob%(i%)
  NEXT i%
  CLOSE #1
  i_explorer%=i_explorer_init%
  j_explorer%=j_explorer_init%
  @display_problems
  INC file_num%
  file_ok%=1
ELSE
  TEXT 1,8,name$+" does not exist."
  file_num%=1
  file_ok%=0
ENDIF
RETURN
'
> PROCEDURE show_parameter(num%)
' show the current parameter in the top menu .
GRAPHMODE 2
SELECT num%
CASE 1
  TEXT 25,(num%+1)*10,"Instructions"
CASE 2
  TEXT 25,(num%+1)*10,"Difficulty"
  PBOX 120,(num%)*10+2,200,(num%+1)*10+1
  TEXT 120,(num%+1)*10,difficulty$(difficulty%)
CASE 3
  TEXT 25,(num%+1)*10,"Operation"
  PBOX 120,(num%)*10+2,280,(num%+1)*10+1
  TEXT 120,(num%+1)*10,operation$(operation%)
CASE 4
  TEXT 25,(num%+1)*10,"First file"
  PBOX 120,(num%)*10+2,210,(num%+1)*10
  TEXT 120,(num%+1)*10,"add"+STR$(file_num%)+".dat"
CASE 5
  TEXT 25,(num%+1)*10,"Response"
  PBOX 120,(num%)*10+2,210,(num%+1)*10
  TEXT 120,(num%+1)*10,STR$(joystick_response)
CASE 7
  TEXT 25,(num%+1)*10,"OK"
CASE 8
  TEXT 25,(num%+1)*10,"QUIT"
ENDSELECT
GRAPHMODE 0
RETURN
> PROCEDURE show_all_parameters
' display the menu with all its parameters.
LOCAL i%,x1%,y1%,r%
DEFTEXT 4,0,0,6
DEFFILL 2
PRBOX 0,0,319,199
FOR i%=1 TO 8
  @show_parameter(i%)
NEXT i%
GRAPHMODE 2
PUT 150,70,add$,7
TEXT 10,155,"Please turn volume up."
GRAPHMODE 1
RETURN
> PROCEDURE select_parameters
' select and modify parameter using mouse.
LOCAL choice%,highlight%,key$
DEFFILL 0
' select character height
DEFTEXT 1,0,0,6
' clear screen
PBOX 0,0,319,199
highlight%=0
@show_all_parameters
' play music if any.
'  @dosound
SETMOUSE 10,185,0
REPEAT
  key$=""
  REPEAT
    key$=INKEY$
    SHOWM
    choice%=(MOUSEY-10)/10+1
    ' highlight if mouse moved to a new parameter
    IF highlight%<>choice%
      DEFTEXT 4,0
      show_parameter(highlight%)
      DEFTEXT 5,0
      show_parameter(choice%)
      highlight%=choice%
      show_help(choice%)
    ENDIF
    IF MOUSEK=0
      tim%=TIMER
    ENDIF
  UNTIL MOUSEK<>0 OR key$<>""
  ' left mouse button increases parameter value, right button decreases
  IF MOUSEK=1
    modify_parameter(choice%,1)
  ENDIF
  IF MOUSEK=2
    modify_parameter(choice%,-1)
  ENDIF
  IF choice%<>6
    show_parameter(choice%)
  ENDIF
  PAUSE 20
  IF key$="e"
    @design_control
    choice%=1
  ENDIF
  IF key$="w"
    @write_prob_on_disk
    choice%=1
  ENDIF
  IF key$="r"
    @display_problems
    @run_game
    @show_all_parameters
    choice%=1
  ENDIF
UNTIL choice%=8 OR choice%=7
DEFTEXT 1,0
RETURN
> PROCEDURE modify_parameter(num%,dir%)
' raise or lower selected parameter withen limits.
'  LOCATE 1,1
'  PRINT num%;
SELECT num%
CASE 1
  @instructions
CASE 2
  difficulty%=difficulty%+dir%
  IF difficulty%>2
    difficulty%=0
  ENDIF
  IF difficulty%<0
    difficulty%=2
  ENDIF
CASE 3
  operation%=1-operation%
CASE 4
  file_num%=file_num%+dir%
  IF file_num%<1
    file_num%=1
  ENDIF
CASE 5
  joystick_response=joystick_response+dir%*0.02
  IF joystick_response<0
    joystick_response=0
  ELSE IF joystick_response>0.4
    joystick_response=0.4
  ENDIF
CASE 7
  @read_prob_from_disk
  IF file_ok%=1
    @run_game
    '    @show_all_parameters
  ELSE
    PAUSE 50
  ENDIF
CASE 8
  quit%=1
ENDSELECT
RETURN
> PROCEDURE show_help(num%)
' show running commentary on bottom of screen for menu.
PBOX 10,160,310,190
GRAPHMODE 2
IF num%>0 AND num%<9
  TEXT 10,170,help$(num%)
ELSE
  TEXT 10,170,help$(0)
ENDIF
TEXT 10,180,"Click left or right button."
RETURN
> PROCEDURE instructions
CLS
PRINT "Simple Addition  31-1-93"
PRINT
PRINT "The object is to move the answer"
PRINT "blocks using the joystick or arrow"
PRINT "keys to complete the arithmetic"
PRINT "problems. Pick up or drop the"
PRINT "answer block using the joystick"
PRINT "button or the Clr Home key."
PRINT "The Esc key allows you to exit"
PRINT "prematurely. The Undo key restarts"
PRINT "the puzzle."
PRINT
PRINT "The first four puzzles are trivial."
PRINT "The next five puzzles require a"
PRINT "little planning. From then on the"
PRINT "puzzles require both care and"
PRINT "planning."
PRINT
PRINT "....Click mouse button to continue"
PAUSE 30
REPEAT
UNTIL MOUSEK<>0
CLS
PRINT "For making your own problems,"
PRINT "access the editor by typing e"
PRINT "from the main menu. Enter the"
PRINT "dimensions of the screen (max"
PRINT "18 by 12). Use the control buttons"
PRINT "at the bottom of the screen to"
PRINT "reserve row, column, answer and"
PRINT "barrier areas."
PRINT "Clear button restarts editor, done"
PRINT "exits the editor. You may test the"
PRINT "current problem screen by typing r"
PRINT "from the main menu. The w key"
PRINT "produces an output file."
PRINT
PRINT "The program creates random problems"
PRINT "and places them and the answers"
PRINT "in the reserved areas."
PRINT
PRINT "The file addbak.pi1 contains "
PRINT "background pattterns. You can make"
PRINT "your own if you have a paint program."
PRINT
PRINT "...Click mouse button to continue."
PAUSE 30
REPEAT
UNTIL MOUSEK<>0
CLS
PRINT
PRINT "Programmed by Seymour Shlien"
PRINT "        624 Courtenay Avenue"
PRINT "       Ottawa, Canada K2A 3B5"
PRINT
PRINT "The program and the sources are public"
PRINT "domain."
PRINT
PRINT "...Click mouse button to continue."
PAUSE 30
REPEAT
UNTIL MOUSEK<>0
CLS
@show_all_parameters
RETURN
> PROCEDURE load_degas_pi1(file$)
'
IF EXIST(file$)
  OPEN "i",#1,file$
  s$=INPUT$(2,#1)
  m__current_colors$=INPUT$(32,#1)
  s$=INPUT$(32000,#1)
  CLOSE #1
  VSYNC
  ~XBIOS(5,L:frame_ptr%(1),L:-1,-1)
  SPUT s$
  ~XBIOS(5,L:frame_ptr%(0),L:-1,-1)
ELSE
  PRINT file$+" was not found."
  PAUSE 200
ENDIF
RETURN
> PROCEDURE random_pattern(ix1%,iy1%,ix2%,iy2%)
LOCAL i%,j%,ni%,nj%
~XBIOS(5,L:frame_ptr%(1),L:-1,-1)
i%=RANDOM(300)
j%=RANDOM(180)
GET i%,j%,i%+20,j%+20,pattern$
~XBIOS(5,L:frame_ptr%(0),L:-1,-1)
nx1%=(ix2%-ix1%)/20
ny1%=(iy2%-iy1%)/20
FOR i%=0 TO nx1%
  FOR j%=0 TO ny1%
    PUT ix1%+i%*20,iy1%+j%*20,pattern$
  NEXT j%
NEXT i%
RETURN
