C C Program Name: SHOW:HI/LO C C Program Title: Hi/Low Number Guessing Game C C Author: Keith Fenske, Department of Mathematics, University of C Alberta C C Date: 11 December 1976. Copyright (c) 1976 by Keith Fenske. All C rights reserved. C C Source Language: FORTRAN C C Purpose: To play both sides in the game of High-Low, which is also C called Coffee. C C Description: The computer picks a random number from 0 to 100 C (inclusive), which the user tries to guess. After each try, C the user is told whether his guess is too high or too low. A C successful guess prints the tries taken, and then an option C list is given to the user: repeat, pick his own number or C stop. C C External References: C *SYSLIB Subroutines: C FREAD C C SHOW:LIB Subroutines: C COPY, RANDOM C C Logical I/O Units Referenced: C 5 - input. Internally assigned to *MSOURCE*@UC. C 6 - output C INTEGER RANDOM INTEGER GUESS, HIGH, LOW, MING/999/, RAND, TRIES INTEGER ICOR/'C'/, IHIGH/'H'/, ILOW/'L'/, INO/'N'/ INTEGER INPUT/' '/, ISTOP/'S'/, IYES/'Y'/ C C Introduction C CALL FTNCMD('ASSIGN 5=*MSOURCE*@UC;') WRITE(6,1010) CALL FREAD(5, 'S:', INPUT, 1) IF(INPUT.EQ.INO) GO TO 10 C C Copy the instructions C CALL COPY(6, 'SHOW:HI/LO.W ', 6) C C Pick an integer from 0 to 100 inclusive C 10 RAND = RANDOM(101) TRIES = 0 WRITE(6,1050) C C Get a guess from the user C 20 TRIES = TRIES + 1 WRITE(6,1060) CALL FREAD(5, 'I:', GUESS) IF((GUESS.GE.0) .AND. (GUESS.LE.100)) GO TO 30 WRITE(6,1070) GO TO 20 C C Process the guess C 30 IF(GUESS-RAND) 40, 60, 50 C C User is too low C 40 WRITE(6,1080) GUESS GO TO 20 C C User is too high C 50 WRITE(6,1090) GUESS GO TO 20 C C User has a correct guess C 60 MING = MIN0(TRIES, MING) WRITE(6,1100) RAND, TRIES IF(TRIES.LE.5) WRITE(6,1110) C C Give user control of the future C 70 WRITE(6,1120) CALL FREAD(5, 'S:', INPUT, 1) IF(INPUT.EQ.INO) GO TO 10 IF(INPUT.EQ.IYES) GO TO 80 IF(INPUT.EQ.ISTOP) GO TO 150 WRITE(6,1130) GO TO 70 C C Now set ourselves up for guessing C 80 CONTINUE HIGH = 101 LOW = -1 TRIES = 0 WRITE(6,1140) GO TO 100 C C Form a new mean value to try on the user C 90 CONTINUE IF((HIGH-LOW).GT.1) GO TO 100 WRITE(6,1150) GO TO 70 100 GUESS = LOW + 1 + RANDOM(HIGH-LOW-1) TRIES = TRIES + 1 110 WRITE(6,1160) GUESS C C Are we high, low or right on (correct)? C CALL FREAD(5, 'S:', INPUT, 1) IF(INPUT.EQ.ILOW) GO TO 120 IF(INPUT.EQ.IHIGH) GO TO 130 IF(INPUT.EQ.ICOR) GO TO 140 WRITE(6,1170) GO TO 110 C C We guessed low C 120 LOW = GUESS GO TO 90 C C We guessed high C 130 HIGH = GUESS GO TO 90 C C Correct try C 140 WRITE(6,1180) GUESS, TRIES IF(TRIES.LT.MING) WRITE(6,1190) GO TO 70 C C Stop the program C 150 STOP C C Format statements C 1010 FORMAT('1Do you want the SHOW:HI/LO instructions? (YES or NO)') 1050 FORMAT('-All right, my number has been chosen') 1060 FORMAT('0What is your guess? (0 to 100)') 1070 FORMAT('0Nice try, numbers are from 0 to 100') 1080 FORMAT('0Try again, ',I3,' is low') 1090 FORMAT('0Try again, ',I3,' is high') 1100 FORMAT('-Right, ',I3,' took you ',I3,' tries') 1110 FORMAT(' Which is rather good') 1120 FORMAT('-Do you want to pick your own number? (YES or NO)',/, 1' or return to MTS? (STOP)') 1130 FORMAT('0You must pick YES, NO or STOP') 1140 FORMAT('-Well, I shall try my best') 1150 FORMAT('-I suspect that you are not telling the truth') 1160 FORMAT('0Is ',I3,' HIGH, LOW, or CORRECT?') 1170 FORMAT('0You must choose HIGH, LOW or CORRECT') 1180 FORMAT('-Good, ',I3,' took me ',I3,' tries') 1190 FORMAT(' Which is less than your best') END