C	MAZE PROGRAM
C
C	PROGRAM DISPLAYS A MAZE ON A CRT TERMINAL AND ACCEPTS INPUTS TO MOVE
C	THE CURSOR UP, DOWN, LEFT OR RIGHT.  THE OBJECT IS TO REACH THE EXIT
C	OF THE MAZE WITHOUT CROSSING WALLS (THE PROGRAM WILL NOT ALLOW THE  
C	CURSOR TO CROSS WALLS OR BOUNDARYS).   A TRACE OPTION, WHEN ENABLED,
C	WILL INDICATE THE PATH TAKEN BY THE CURSOR THROUGH THE MAZE.
C	THIS PROGRAM WAS WRITTEN SPECIFICALLY FOR A BEEHIVE 100 TERMINAL
C	(24 LINES BY 80 CHARACTERS) USING THE KEYS 2, 4, 6 AND 8 TO MOVE
C	THE CURSOR DOWN, LEFT, RIGHT AND UP RESPECTIVELY.  CHANGING THE
C	APPROPRIATE DATA STATEMENT WILL ALTER THE CURSOR MOVEMENT KEYS.
C	THE PROGRAM CAN BE MODIFIED FOR OTHER TERMINALS BY CHANGING THE
C	PARAMETERS MAXH AND MAXV, THE DATA STATEMENT DEFINTION OF SMALL, 
C	MEDIUM AND LARGE MAZES AND THE TWO ROUTINES CLEAR AND POSITION.
C
C   WRITTEN   SEPTEMBER 1977   R. B. FRENCH
C
C
C	MODIFIED BY P.E. MFERRIN FOR THE 'FOR' COMPILER  13-DEC-77
C	AND HAS THE FOLLOWING INPROVEMENTS:
C
C		1. RINGS BELL WHEN ATTEMPTING TO KNOCK DOWN A WALL
C		2. PROVIDES SCORING BASED UPON THE FOLLOWING:
C			A. SET INITIAL SCORE OF 1500, 1000, & 500 FOR
C			   LARGE, MEDIUM, & SMALL MAZE.
C			B. THE INITIAL SCORE IS REDUCED BY 20% FOR EACH
C			   REVERSE (180 DEG) IN DIRECTION.
C			C. THE INITIAL SCORE IS ALSO REDUCED BY 5% FOR
C			   EACH ATTEMPT TO KNOCK DOWN A WALL.
C			D. THE FINAL SCORE IS THEN REDUCED BY 0.5% FOR
C			   EACH SECOND OF ELASPED TIME TO COMPLETE THE
C			   MAZE OVER 10 SECONDS.
C	MODIFIED BY M.T. FORTHOFER FOR LEAR SIEGLER ADM/3A
C
C
C   TASK BUILD FILE
C
C	MAZE,MAZE=MAZE,CLEAR,POSITN
C	/
C	ASG=TI:1
C	//
C
C
C      PARAMETER MAXH=26		!MAXIMUM NUMBER OF HORIZONTAL SQUARES
C      PARAMETER MAXV=11		!MAXIMUM NUMBER OF VERTICAL SQUARES
      INTEGER H
	INTEGER BELPRM(2)
	BYTE BLANK,ASTK,CI,CN,CS,CM,CL,LSTDIR,BELL
C      BYTE SS(3*MAXH+1)
      BYTE SS(79)
      INTEGER X,T,C,R,S,VV,Z,Q
      INTEGER W(26, 10),V(26, 10)
C      INTEGER W(MAXH, MAXV),V(MAXH, MAXV)
      COMMON I1,I2
      DIMENSION IPRM(2),JPRM(2)
      BYTE IANS,CNTRLZ,IEXIT,TRACE,PLUS,TEE,KEY
      BYTE UP,DOWN,LEFT,RIGHT
      BYTE HSML,HMED,HLRG,VSML,VMED,VLRG
      DATA IEXIT,PLUS,TEE,CNTRLZ/0,"53,"124,"32/
      DATA MAXH,MAXV/26,10/
	DATA BLANK,ASTK,CI,CN,CS,CM,CL/' ','*','I','N','S','M','L'/
	DATA BELL/"07/
	DATA MINUS/'-'/
C*********************************************************************
C	THIS DATA STATEMENT DEFINES THE OCTAL CODE FOR THE ASCII     *
C	KEYS TO MOVE THE CURSOR AND CAN BE RE-DEFINED, IF DESIRED.   *
C      DATA UP,DOWN,LEFT,RIGHT/"70,"62,"64,"66/
	DATA LEFT,UP,RIGHT,DOWN/'H','K','L','J'/
C*********************************************************************
C
C*********************************************************************
C	THESE DATA STATEMENTS DEFINES THE HORIZONITAL AND VERTICAL   *
C	SIZE OF THE SMALL, MEDIUM AND LARGE MAZES.                   *
      DATA HSML,HMED,HLRG/9,18,26/
      DATA VSML,VMED,VLRG/5,8,10/
C*********************************************************************
C
C	SETUP AND INTRODUCTION
      CALL GETMCR(V)
      CALL CLEAR(1)
      IPRM(1)=LOCF(KEY)
      IPRM(2)=1
	BELPRM(1)=LOCF(BELL)
	BELPRM(2)=1
      TRACE=-1
      JPRM(1)=LOCF(PLUS)
      JPRM(2)=1
      IWIDTH=3*MAXH+1
      WRITE(1,5000)
5000  FORMAT('$DO YOU NEED INSTRUCTIONS (YES/NO)? ')
      READ(1,8200,END=700) IANS
      IF (IANS.EQ.CN) GO TO 20
      CALL CLEAR
      WRITE(1,5100) UP,DOWN,LEFT,RIGHT
5100  FORMAT(20X,'M   M      AAA      ZZZZZ     EEEEE',
     1 /20X,'MM MM     A   A        Z      E',
     2 /20X,'M M M     AAAAA       Z       EEEE',
     3 /20X,'M   M     A   A      Z        E',
     4 /20X,'M   M     A   A     ZZZZZ     EEEEE',
     5 ///5X,'THE OBJECT IS TO MOVE THE CURSOR THROUGH THE MAZE USING',
     6 ' THE KEYS',
     7 //28X,A1,' TO MOVE UP'//28X,A1,' TO MOVE DOWN',
     8 //28X,A1,' TO MOVE LEFT'//28X,A1,' TO MOVE RIGHT',
     A //28X,'T TO TURN TRACE OPTION ON/OFF',
     B //8X'THE PROGRAM CAN BE ABORTED AT ANY TIME BY ENTERING',
     C ' CNTRL-Z.',
     D ///'$',18X,'PRESS CARRAIGE RETURN TO CONTINUE')
      READ(1,8200,END=700) IANS
      CALL CLEAR
C
20    WRITE(1,5200)
5200  FORMAT('$SMALL, MEDIUM OR LARGE MAZE (S/M/L) ? ')
      READ(1,8200,END=700) IANS
      IF (IANS.NE.CS) GO TO 21
      H=HSML
      VV=VSML
	SCORE=500.
      GO TO 23
21    IF (IANS.NE.CM) GO TO 22
      H=HMED
      VV=VMED
	SCORE=1000.
      GO TO 23
22    IF (IANS.NE.CL) GO TO 20
      H=HLRG
      VV=VLRG
	SCORE=1500.
23    IBOTTM=2*VV+1
	 SMAX=SCORE
C
      INIT=AMOD(SECNDS(0.),500.)
      DO 1 I=1,INIT
1     X=RAN(I1,I2)
C 9000  FORMAT (1X,<IWIDTH>A1)
9000	FORMAT(1X,79A1)
9001  Q=0
      Z = 0
      DO 40 J=1,MAXH
      DO 40 I=1,MAXV
      W(J,I)=0
40    V(J,I)=0
      DO 9999 I=1,79
9999  SS (I)=BLANK 
      X=IRAND (H)
      KK=X
      C=1
      W(X,1)=C
      C=C+1
      R=X
      S=1
      GO TO 900
730   IF (R.NE.H) GO TO 810
      IF (S.NE.VV) GO TO 780
      R=1
      S=1
      GO TO 890
780   R=1
      S=S+1
      GO TO 890
810   R=R+1
890   IF (W(R,S).EQ.0) GO TO 730
900   IF (R .EQ. 1) GO TO 1330
      IF (W(R-1,S) .NE. 0) GO TO 1330
      IF (S .EQ. 1) GO TO 1100
      IF (W(R,S-1).NE.0) GO TO 1100
      IF (R.EQ.H) GO TO 990
      IF  (W(R+1,S).NE.0) GO TO 990
      X=IRAND(3)
      GO TO (1850,1920,1990),X
990   IF (S.NE.VV) GO TO 1030
      IF (Z.EQ.1) GO TO 1070
      Q=1
      GO TO 1040
1030  IF (W(R,S+1).NE.0) GO TO 1070
1040  X=IRAND(3)
      GO TO (1850,1920,2090),X
1070  X=IRAND(2)
      GO TO (1850,1920),X
1100  IF (R.EQ.H) GO TO 1230
      IF (W(R+1,S).NE.0) GO TO 1230
      IF (S.NE.VV) GO TO 1160
      IF (Z.EQ .1) GO TO 1200
      Q=1
      GO TO 1170
1160  IF (W(R,S+1).NE .0) GO TO 1200
1170  X=IRAND(3)
      GO TO (1850,1990,2090),X
1200  X=IRAND(2)
      GO TO (1850,1990),X
1230  IF (S.NE .VV) GO TO 1270
      IF (Z.EQ .1) GO TO 1850
      Q=1
      GO TO 1280
1270  IF (W(R,S+1).NE .0) GO TO 1850
1280  X=IRAND(2)
      GO TO (1850,2090),X
1330  IF (S .EQ. 1) GO TO 1580
      IF (W(R,S-1).NE .0) GO TO 1580
      IF (R.EQ .H) GO TO 1480
      IF (W(R+1,S).NE .0) GO TO 1480
      IF (S.NE .VV) GO TO 1410
      IF (Z .EQ. 1) GO TO 1450
      Q=1
      GO TO 1420
1410  IF (W(R,S+1).NE .0) GO TO 1450
1420  X=IRAND(3)
      GO TO (1920,1990,2090),X
1450  X=IRAND(2)
      GO TO (1920,1990),X
1480  IF (S.NE .VV) GO TO 1520
      IF (Z.EQ .1) GO TO 1920
      Q=1
      GO TO 1530
1520  IF (W(R,S+1).NE .0) GO TO 1920
1530  X=IRAND(2)
      GO TO (1920,2090),X
1580  IF (R.EQ .H) GO TO 1700
      IF (W(R+1,S).NE .0) GO TO 1700
      IF (S.NE .VV) GO TO 1640
      IF (Z.EQ. 1) GO TO 1990
      Q=1
      GO TO 1650
1640  IF (W(R,S+1).NE .0) GO TO 1990
1650  X=IRAND(2)
      GO TO (1990,2090),X
1700  IF (S.NE .VV) GO TO 1740
      IF (Z.EQ .1) GO TO 730
      Q=1
      GO TO 2090
1740  IF (W(R,S+1).NE .0) GO TO 730
      GO TO 2090
1850  W(R-1,S)=C
      C=C+1
      V(R-1,S)=2
      R=R-1
      IF (C.EQ .H*VV+1) GO TO 2310
      Q=0
      GO TO 900
1920  W(R,S-1)=C
      C=C+1
      V(R,S-1)=1
      S=S-1
      IF (C.EQ .(H*VV+1)) GO TO 2310
      Q=0
      GO TO 900
1990  W(R+1,S)=C
      C=C+1
      IF (V(R,S).EQ .0) GO TO 2040
      V(R,S)=3
      GO TO 2050
2040  V(R,S)=2
2050  R=R+1
      IF (C.EQ .(H*VV+1)) GO TO 2310
      Q=0
      GO TO 1330
2090  IF (Q.EQ. 1) GO TO 2190
      W(R,S+1)=C
      C=C+1
      IF (V(R,S).EQ .0) GO TO 2150
      V(R,S)=3
      GO TO 2160
2150  V(R,S)=1
2160  S=S+1
      IF (C.EQ .(H*VV+1)) GO TO 2310
      GO TO 900
2190  Z=1
      IF (V(R,S).EQ .0) GO TO 2240
      V(R,S)=3
      Q=0
      GO TO 730
2240  V(R,S)=1
      Q=0
      R=1
      S=1
      GO TO 890
C
C	CLEAR SCREEN
2310  CALL CLEAR
      IF (IEXIT.NE.0) CALL EXIT
C
      K=3*H+1
      DO 10 I=1,H
      Q= I * 3
      SS (Q-2)=CI 
      SS (Q-1)=MINUS
      SS (Q)=MINUS
10    CONTINUE
      Q = KK * 3
      SS(Q-2)=ASTK
      SS (Q-1)=BLANK 
      SS (Q)=BLANK 
      SS(Q+1)=ASTK
      SS(3*H+1) = CI 
      WRITE (1,9000) (SS(I),I=1,K)
      DO 3000 J=1,VV
      Z=H*3
      DO 70 I=1,Z
70    SS(I)=BLANK 
      SS(1)=CI
      DO 3001 I=1,H
3001  IF (V(I,J).LE.1) SS(3*I+1)=CI
      WRITE (1,9000) (SS(I),I=1,K)
      DO 3002 I=1,H
      Q=3*I
      SS(Q-2)=CI
      IF (V(I,J).EQ.1.OR.V(I,J).EQ.3) GO TO 3002
      SS(Q-1)=MINUS
      SS(Q)=MINUS
3002  CONTINUE
      IF (J.LT.VV) GO TO 3004
      DO 3003,I=1,H
      IF (V(I,VV).EQ.0.OR.V(I,VV).EQ.2) GO TO 3003
      Q=3*I
      SS(Q-2)=ASTK
      SS(Q+1)=ASTK
      GO TO 3004
3003  CONTINUE
3004  WRITE (1,9000) (SS(I),I=1,K)
3000  CONTINUE
3999  CONTINUE
      CALL POSITN(22,1,1)
      WRITE(1,8000) UP,DOWN,LEFT,RIGHT
8000  FORMAT(' KEYS:  ',A1,'=UP   ',A1,'=DOWN   ',A1,'=LEFT   ',A1,
     1 '=RIGHT',5X,'T=TRACE   CNTRL-Z=ABORT')
	STRT=SECNDS(0.0)
      IX=1
      IY=3*(KK-1)+2
      GO TO 201
200   IF (TRACE.GT.0) CALL WTQIO("410,1,1,,,JPRM)
201   CALL POSITN(IX,IY-1,1)
      KX=(IX+1)/2
      KY=(IY+2)/3
	GO TO 211
210	SCORE=SCORE*0.95
	CALL WTQIO("410,1,1,,,BELPRM)
211   CALL WTQIO("1030,1,1,,,IPRM)
      IF (KEY.EQ.CNTRLZ) GO TO 700
      IF (KEY.EQ.TEE) GO TO 600
      IF (KEY.EQ.UP) GO TO 500
      IF (KEY.EQ.DOWN) GO TO 400
      IF (KEY.EQ.LEFT) GO TO 300
      IF (KEY.NE.RIGHT) GO TO 211
C	RIGHT
	IF(LSTDIR.EQ.LEFT) SCORE=SCORE*0.80
	LSTDIR=RIGHT
      IF (IY/KY.EQ.3) GO TO 220
      IY=IY+1
      GO TO 200
220   IF ((IX/2)*2.NE.IX) GO TO 210
      IF (V(KY,KX).LE.1) GO TO 210
      IY=IY+1
      GO TO 200
C	LEFT
300	IF(LSTDIR.EQ.RIGHT) SCORE=SCORE*0.80
	LSTDIR=LEFT
      IF (IY/KY.EQ.2) GO TO 310
      IY=IY-1
      GO TO 200
310   IF ((IX/2)*2.NE.IX) GO TO 210
      IF (KY.EQ.1) GO TO 210
      IF (V(KY-1,KX).LE.1) GO TO 210
      IY=IY-1
      GO TO 200
C	DOWN
400	IF(LSTDIR.EQ.UP) SCORE=SCORE*0.80
	LSTDIR=DOWN
      IF ((IX/2)*2.EQ.IX) GO TO 410
      IX=IX+1
      GO TO 200
410   IF (3*KY-2.EQ.IY) GO TO 210
      IF (V(KY,KX).EQ.0) GO TO 210
      IF (V(KY,KX).EQ.2) GO TO 210
420   IX=IX+1
      IF (IX.LT.IBOTTM) GO TO 200
	TOT=SECNDS(STRT)
	I=TOT-10.
	IF(I.LE.0) GO TO 8162
	DO 8160 J=1,I
8160	SCORE=SCORE*0.995
8162	CONTINUE
      CALL CLEAR
	CALL POSITN(3,1,1)
	WRITE(1,8150)TOT,SCORE,SMAX
8150	FORMAT(16X,'TOTAL TIME TO COMPLETE MAZE WAS',1F5.0,' SECONDS',
     $ ////18X,'YOUR SCORE IS',1F6.0,' OUT OF INITIAL',1F6.0)
C      CALL POSITN(8,1,1)
      WRITE(1,8100)
8100  FORMAT(///20X,'C O N G R A T U L A T I O N S   ! ! !',
     1 /////'$',23X,'CARE TO TRY AGAIN (YES/NO) ? ')
      READ(1,8200,END=700) IANS
8200  FORMAT(4A1)
      IF (IANS.EQ.CN) GO TO 700
      CALL CLEAR
      GO TO 20
C	UP
500	IF(LSTDIR.EQ.DOWN) SCORE=SCORE*0.80
	LSTDIR=UP
      IF ((IX/2)*2.EQ.IX) GO TO 510
      IF (IX.EQ.1) GO TO 210
      IX=IX-1
      GO TO 200
510   IF (3*KY-2.EQ.IY) GO TO 210
      IF (KX.NE.1) GO TO 520
      IF (KX.NE.KK) GO TO 210
      IX=IX-1
      GO TO 200
520   IF (V(KY,KX-1).EQ.0) GO TO 210
      IF (V(KY,KX-1).EQ.2) GO TO 210
530   IX=IX-1
      GO TO 200
C	TRACE
600   TRACE=-TRACE
      GO TO 200
700   IEXIT=1
      GO TO 2310
      END
      FUNCTION IRAND(I)
      COMMON I1,I2
      X=RAN(I1,I2)
      IRAND=X*FLOAT(I)+1.
      RETURN
      END
