FORTRAN Computer Games


Magic 8-Ball

A virtual Magic 8-Ball in FORTRAN 77, for fortune-telling and advice seeking. Ask a yes-no question and get the totally correct answer from the computer. You can try the program in your web browser.

Gameplay

Magic-8-Ball in FORTRAN 77

Functions & Subroutines

The program calls three procedures that are not part of the ANSI FORTRAN 77 language standard:

RESULT = TIME()
Returns timestamp in seconds (INTEGER). Required for the initialisation of the pseudo-random number generator.
RESULT = RAND(I)
Returns the next random number (REAL).
CALL SRAND(ISEED)
Initialises the pseudo-random number generator with given seed value (INTEGER).

Most modern compilers provide these procedures through extensions.

Program Listing

Copy and save the program in file magic8.f locally.

C     ******************************************************************
C
C     MAGIC 8-BALL IN FORTRAN 77.
C
C     ******************************************************************
      PROGRAM MAGIC8
      CHARACTER*25 ANSY(20), QUEST
      INTEGER      I, IR, ISTAT

      DATA ANSY /'IT IS CERTAIN','IT IS DECIDEDLY SO',
     &'WITHOUT A DOUBT','YES, DEFINITELY','YOU MAY RELY ON IT',
     &'AS I SEE IT, YES','MOST LIKELY','OUTLOOK GOOD',
     &'SIGNS POINT TO YES','YES','REPLY HAZY, TRY AGAIN',
     &'ASK AGAIN LATER','BETTER NOT TELL YOU NOW','CANNOT PREDICT NOW',
     &'CONCENTRATE AND ASK AGAIN','DON''T BET ON IT','MY REPLY IS NO',
     &'MY SOURCES SAY NO','OUTLOOK NOT SO GOOD','VERY DOUBTFUL'/
C
C     INTIALISE RANDOM NUMBER GENERATOR USING CURRENT TIME.
C
      CALL SRAND(TIME())
C
C     READ QUESTION FROM INPUT.
C
      PRINT 100
  100 FORMAT (' ASK A QUESTION: ',$)
      READ (*, 200, IOSTAT=ISTAT) QUEST
  200 FORMAT (A)
      IF (ISTAT .NE. 0 .OR. QUEST .EQ. ' ') STOP
C
C     THROW AWAY THE FIRST 99 RANDOM NUMBERS FOR HIGHER ENTROPY.
C
      DO 10 I = 1, 100
      IR = 1 + INT(RAND(0) * SIZE(ANSY))
   10 CONTINUE
C
C     PRINT THE ANSWER.
C
      PRINT 300, ANSY(IR)
  300 FORMAT (1X,A)
      END

Build Instructions

UNIXFlang/F18$ flang -o magic8 magic8.f
GNU Fortran$ gfortran -o magic8 magic8.f
Intel Fortran Compiler$ ifort -o magic8 magic8.f
Win32Digital/Compaq Visual Fortran> fl32.exe magic8.f /Fe=magic8.exe

References


Home