      PROGRAM GUESSNUMBER
      IMPLICIT NONE
      INTEGER DIM
      PARAMETER (DIM = 4)
      INTEGER GUESS(DIM)
      INTEGER ANSWER(DIM)
      COMMON GUESS(DIM), ANSWER(DIM)
      
      INTEGER A, B
      
      INTEGER MAXGUESS
      PARAMETER (MAXGUESS = 3)
      INTEGER GUESSED
      LOGICAL OVER
      
      PRINT *, 'INPUT THE ANSWER'
      CALL GETINPUT(ANSWER, DIM)
      PRINT *, 'ANSWER IS ', ANSWER
      
      OVER = .FALSE.
      GUESSED = 0
      DO WHILE (GUESSED .LE. MAXGUESS .AND. (.NOT. OVER))
        CALL GETINPUT(GUESS, DIM)
        PRINT *, 'GUESS IS ', GUESS
      
        CALL COMPARE(DIM, A, B)
        PRINT *, A, 'A', B, 'B'
        IF (A .EQ. DIM) THEN 
          OVER = .TRUE.
          PRINT *, 'YOU GOT IT!!'
        ENDIF  
        
        GUESSED = GUESSED + 1
      ENDDO
      
      IF (.NOT. OVER) THEN
        PRINT *, 'YOU DID NOT GET IT!'
      ENDIF

      STOP
      END
      
      SUBROUTINE COMPARE(DIM, A, B)
      IMPLICIT NONE
      INTEGER DIM, A, B
      INTEGER GUESS(DIM), ANSWER(DIM)
      COMMON GUESS(DIM), ANSWER(DIM)
      
      INTEGER I, J
      A = 0
      B = 0
      
      DO 10 I = 1, DIM
        DO 20 J = 1, DIM
          IF (GUESS(I) .EQ. ANSWER(J)) THEN
             IF (I .EQ. J) THEN
               A = A + 1 
             ELSE
               B = B + 1
             ENDIF
          ENDIF      
20      CONTINUE
10    CONTINUE
        
      RETURN
      END
      
      SUBROUTINE GETINPUT(INPUT, DIM)
      IMPLICIT NONE 
      INTEGER DIM
      INTEGER INPUT(DIM)
      
      LOGICAL VALID
      
      PRINT *, 'INPUT A NUMBER'
      READ *, INPUT
      DO WHILE(.NOT. VALID(INPUT, DIM))
        PRINT *, 'INPUT A NUMBER'
        READ *, INPUT
      ENDDO
      
      RETURN
      END
      
      LOGICAL FUNCTION VALID(INPUT, DIM)
      IMPLICIT NONE 
      INTEGER DIM
      INTEGER INPUT(DIM)
      
      INTEGER I, J
      
      DO 10 I = 1, DIM
        IF ((INPUT(I) .LT. 0) .OR. (INPUT(I) .GT. 9)) THEN
          VALID = .FALSE.
          RETURN
        ENDIF
10    CONTINUE  
      
      DO 20 I = 1, DIM -1
        DO 30 J = I + 1, DIM
          IF (INPUT(I) .EQ. INPUT(J)) THEN
            VALID = .FALSE.
            RETURN
          ENDIF
30      CONTINUE
20    CONTINUE           
      
      VALID = .TRUE.
      RETURN
      END
      