/* Program name: GuessNum                                               */
/* This program generates a number between 1 and a high limit chosen    */
/* by the user. It then prompts for guesses until you guess the number  */
/* or give up.                                                          */
/*                                                                      */
/* To run this program, type:                                           */
/* rx GuessNum                                                          */
/*                                                                      */
DO i = 1 to 5                                 /* DO loop for instruct.  */
  SAY SOURCELINE(i)                           /* SAY lines 1-5 above.   */
  END                                         /* End DO loop.           */

OPTIONS PROMPT 'High Number (10-999)? :'      /* Set prompt for input.  */
PULL highnum                                  /* Take input.            */
IF (highnum>999) | (highnum<10) THEN DO       /* Is highnum in range?   */
  SAY "You didn't follow directions!"         /* If not, tell the user, */
  SAY "I'll pick a High Number for you!"      /*   and do it for him.   */
  highnum = RANDOM(10,999,TIME('S'))          /* Pick a valid highnum.  */
  END                                         /* End DO loop.           */
SAY 'Generating a random number between 1 and' highnum  /* Feedback.    */
randnum = RANDOM(1,highnum,TIME('S'))         /* Generate random number.*/
count = 0                                     /* Zero counter.          */

OPTIONS PROMPT 'Guess a number (0 to quit) :' /* Prompt for guess.      */
DO forever                                    /* Infinite loop.         */
  DO until (guess<=highnum) & (guess>-1)      /* Is guess in range?     */
    PULL guess                                /* Get guess from user.   */
    END                                       /* End input loop.        */
  count = count + 1                           /* Increment counter.     */
  SELECT                                      /* Make some comparisons. */
    WHEN guess = 0 THEN DO                    /* Did user quit?         */
      SAY 'Giving up after only' count 'guesses?' /* Chastise user.     */
      SAY 'The number was:' randnum           /* Give the answer.       */
      EXIT                                    /* Quit the program.      */
      END                                     /* End inner DO loop.     */
    WHEN guess = randnum THEN DO              /* If user got it right,  */
      SAY 'You guessed the number in' count 'guesses!' /* tell him so,  */
      EXIT                                    /* and quit the program.  */
      END                                     /* End inner DO loop.     */
    WHEN guess > randnum THEN SAY 'Lower'     /* If high, say 'Lower'.  */

    OTHERWISE SAY 'Higher'                    /* Otherwise, say 'Higher'*/
    END                                       /* End SELECT structure.  */
END
