PROGRAM name ;

  CONST
    {$I D:GEMCONST.PAS}

  TYPE
    {$I D:gemtype.pas}

  VAR
    pushed,dummy,button,height : Integer ;
    temp,i,j,k,answer,numb1,numb2,what_key,event,choice  : Integer ;
    alpha : Alfa ;
    Numb_D : Dialog_Ptr ;
    praise : ARRAY [0..7] of String[50] ;
    numbs : ARRAY [0..10] of Integer ;
    keyboard : FILE of Integer ;
    msg_area : Message_Buffer ;
    sign : Char ;
    s: Str255 ;
  {$I D:gemsubs}

FUNCTION Random : Integer ;
   XBIOS( 17 ) ;


  { Text_Height - Set the height in pixels of text, when it is drawn using the
      Draw_String library call. }

  PROCEDURE Text_Height( height : integer ) ;

    TYPE
      Ctrl_Parms      = ARRAY [ 0..11 ] OF integer ;
      Int_In_Parms    = ARRAY [ 0..15 ] OF integer ;
      Int_Out_Parms   = ARRAY [ 0..45 ] OF integer ;
      Pts_In_Parms    = ARRAY [ 0..11 ] OF integer ;
      Pts_Out_Parms   = ARRAY [ 0..11 ] OF integer ;

    VAR
      control : Ctrl_Parms ;
      int_in  : Int_In_Parms ;
      int_out : Int_Out_Parms ;
      pts_in  : Pts_In_Parms ;
      pts_out : Pts_Out_Parms ;

    PROCEDURE VDI_Call( cmd, sub_cmd : integer ; nints, npts : integer ;
                VAR ctrl : Ctrl_Parms ;
                VAR int_in : Int_In_Parms ; VAR int_out : Int_Out_Parms ;
                VAR pts_in : Pts_In_Parms ; VAR pts_out : Pts_Out_Parms ;
                translate : boolean ) ;
      EXTERNAL ;

    BEGIN
      pts_in[0] := 0 ;
      pts_in[1] := height ;
      VDI_Call(12, 0, 0, 2, control, int_in, int_out, pts_in, pts_out, false);
    END ;

  { Get_Height - Get the height in pixels of text, when it is drawn using the
      Draw_String library call. }

  FUNCTION Get_Height : integer ;

    TYPE
      Ctrl_Parms      = ARRAY [ 0..11 ] OF integer ;
      Int_In_Parms    = ARRAY [ 0..15 ] OF integer ;
      Int_Out_Parms   = ARRAY [ 0..45 ] OF integer ;
      Pts_In_Parms    = ARRAY [ 0..11 ] OF integer ;
      Pts_Out_Parms   = ARRAY [ 0..11 ] OF integer ;

    VAR
      control : Ctrl_Parms ;
      int_in  : Int_In_Parms ;
      int_out : Int_Out_Parms ;
      pts_in  : Pts_In_Parms ;
      pts_out : Pts_Out_Parms ;

    PROCEDURE VDI_Call( cmd, sub_cmd : integer ; nints, npts : integer ;
                VAR ctrl : Ctrl_Parms ;
                VAR int_in : Int_In_Parms ; VAR int_out : Int_Out_Parms ;
                VAR pts_in : Pts_In_Parms ; VAR pts_out : Pts_Out_Parms ;
                translate : boolean ) ;
      EXTERNAL ;

    BEGIN
      VDI_Call(131, 0, 0, 0, control, int_in, int_out, pts_in, pts_out, false);
      Get_Height := pts_out[9] ;
    END ;

PROCEDURE Pick_Choice ;
BEGIN
choice:=Do_Alert('[0][     What do you want to do?  ][ADD|SUBTRACT|BOTH]',0);
END ;

PROCEDURE Init_Alpha ;
BEGIN
 alpha[10] := '9' ;  alpha[2] := '1' ; alpha[3] := '2' ; alpha[4] := '3' ;
 alpha[5] := '4' ;  alpha[6] := '5' ; alpha[7] := '6' ; alpha[8] := '7' ;
 alpha[9] := '8' ;  alpha[1] := '0';
 praise[0] := 'Great job!' ;
 praise[1] := 'Very Good!' ; praise[2]  := 'Math Maniac' ;
 praise[3] := 'WOW WOW WOW' ; praise[4] := 'Super Good!' ;
 praise[5] := 'GO FOR IT!' ; praise[6]  := 'WONDERFUL!' ;
 praise[7] := 'Math Tiger!' ;

END ;

PROCEDURE Paint_Face ;

BEGIN
  numb1 := Random & 7 ;
  Draw_String( 150,130,praise[numb1] ) ;
  Paint_Oval( 200,60,50,50 )     ;
  Frame_Arc( 200,60,35,35,2100,3300 ) ;
  Paint_Oval( 175,40,3,3 ) ;
  Paint_Oval( 225,40,3,3 ) ;
END ;

PROCEDURE Make_Dialog ;
VAR i : Integer ;
BEGIN
  Numb_D := New_Dialog( 11,0,18,40,3 ) ;
  FOR i := 0 TO 9 DO
   BEGIN
     numbs[i]:=Add_DItem(Numb_D,G_Button,5,i*4,0,4,3,-1,$F0+i ) ;
     Set_DText(Numb_D,numbs[i],alpha[i+1],System_Font,TE_Center) ;
   END ;
END ;

FUNCTION Getnumb : Integer ;

VAR
  a,b,c,d : Integer ;
BEGIN
  a := Random & $07 ;
  b := Random & $01 ;
  c := Random & $01 ;
  d := a + b + c ;
  getnumb := d ;
END ;

FUNCTION Val_Ans : Integer ;
VAR i,ans : Integer ;

BEGIN
  FOR i := 0 TO 9 DO
    BEGIN
      IF numbs[i] = pushed THEN Val_Ans := i ;
    END ;
END ;

PROCEDURE Subtraction ;

BEGIN
  sign := '-' ;
  numb2 := getnumb ; numb1 := getnumb ;
  IF numb1 < numb2 THEN
     BEGIN
       temp := numb1 ; numb1 := numb2 ;  numb2 := temp ;
     END ;
  answer := numb1 - numb2 ;
END;

PROCEDURE Addition ;

BEGIN
  sign := '+' ;
  answer := getnumb ; numb1 := getnumb ;
  IF answer < numb1 THEN
     BEGIN
       temp := answer ; answer := numb1 ;  numb1 := temp ;
     END ;
  numb2 := answer - numb1 ;
END ;

PROCEDURE Do_Selection ;
BEGIN
  IF choice = 1 THEN Addition ;
  IF choice = 2 THEN Subtraction ;
  IF choice = 3 THEN
    BEGIN
     dummy := Random & 1 ;
     IF dummy = 1 THEN Addition ELSE Subtraction ;
    END;
END;

PROCEDURE Show_Em ;

BEGIN
  Hide_Mouse ;
  Draw_Mode(1) ;
  Paint_Color( L_Blue ) ;
  Paint_Rect( 0,0,320,200 ) ;
  Draw_Mode(3) ;
  Show_Mouse ;
  Do_Selection ;
  Text_Height(10) ;
  Draw_String( 60,90,alpha[numb1+1] ) ;
  Draw_String( 60,110,alpha[numb2+1] ) ;
  Line( 50,115,70,115 ) ;
  Draw_String( 40,110,sign ) ;
  pushed := Do_Dialog( Numb_D,0 ) ;
  IF Val_Ans = answer THEN
    BEGIN
     Draw_String( 60,130,alpha[answer+1] ) ;
     Paint_Face ;
    END ;
  IF NOT (Val_Ans = answer) THEN
    BEGIN
      Draw_String( 5,130,'Sorry, the answer is  .' ) ;
      Draw_String( 286,130,alpha[answer+1] ) ;
    END ;
  event := Get_Event( E_Button,1,1,1,0,false,0,0,0,0,false,0,0,0,0,
                      msg_area,dummy,dummy,dummy,dummy,dummy,dummy ) ;
  Obj_SetState( Numb_D,pushed,0,false ) ;
END ;

PROCEDURE Do_Em ;
VAR i : Integer ;

BEGIN
  FOR i := 0 TO 9 DO Show_Em ;
END ;

BEGIN
  dummy := Init_Gem ;
  Init_Mouse ;
  Init_Alpha ;
  Make_Dialog ;
REPEAT
  Hide_Mouse ;
  Clear_Screen ;
  Show_Mouse ;
  Pick_Choice ;
  Do_Em ;
UNTIL False ;
END.
