      PROGRAM REVERSI
      IMPLICIT NONE
      
      INTEGER BOARD(8, 8)
      INTEGER COLOR
      LOGICAL VALIDMOVE(8, 8)
      INTEGER CPTRN(8, 8)
      INTEGER CPTRR(8, 8, 50)
      INTEGER CPTRC(8, 8, 50) 
      
      INTEGER I, J
      
      OPEN (UNIT = 1, FILE = 'board')
       DO 10 I = 1, 8
         READ (1, *) (BOARD(I, J), J = 1, 8)
10    CONTINUE  
      
      PRINT *, 'THE INITIAL BOARD IS'
      CALL PRINT8X8(BOARD)
      
      COLOR = 1
      CALL FINDMOVES(BOARD, COLOR, VALIDMOVE, CPTRN, CPTRR, CPTRC)
      
      PRINT *, 'CPTRN IS'
      CALL PRINT8X8(CPTRN)
      
      STOP
      END
      
      SUBROUTINE FINDMOVES(BOARD, COLOR, VALIDMOVE, CPTRN, CPTRR, CPTRC)
      IMPLICIT NONE
      
      INTEGER BOARD(8, 8)
      INTEGER COLOR
      LOGICAL VALIDMOVE(8, 8)
      INTEGER CPTRN(8, 8)
      INTEGER CPTRR(8, 8, 50)
      INTEGER CPTRC(8, 8, 50)
      
      INTEGER R, C, D
      INTEGER DR(8), DC(8)
      DATA DR /1, 1, 0, -1, -1, -1, 0, 1/
      DATA DC /0, 1, 1, 1, 0, -1, -1, -1/
       
      DO 10 R = 1, 8
        DO 20 C = 1, 8
          VALIDMOVE(R, C) = .FALSE.
          CPTRN(R, C) = 0
          
          IF (BOARD(R, C) .EQ. 0) THEN
            DO 30 D = 1, 8
              CALL FINDONEDIR(BOARD, COLOR, VALIDMOVE, 
     +                        CPTRN, CPTRR, CPTRC, R, C, DR(D), DC(D))
30          CONTINUE
          ENDIF
20      CONTINUE
10    CONTINUE

      RETURN
      END
      
      
      SUBROUTINE FINDONEDIR(BOARD, COLOR, VALIDMOVE, 
     +                      CPTRN, CPTRR, CPTRC, R, C, DR, DC)
      IMPLICIT NONE
      INTEGER BOARD(8, 8)
      INTEGER COLOR
      LOGICAL VALIDMOVE(8, 8)
      INTEGER CPTRN(8, 8)
      INTEGER CPTRR(8, 8, 50)
      INTEGER CPTRC(8, 8, 50)
      
      INTEGER R, C
      INTEGER DR, DC
      
      LOGICAL INBOARD
      INTEGER NR, NC, OPPOCOLOR
      
      INTEGER TEMPR(50), TEMPC(50), COUNT, I
     
      NR = R + DR
      NC = C + DC
      OPPOCOLOR = -COLOR

      IF (INBOARD(NR, NC) .AND. (BOARD(NR, NC) .EQ. OPPOCOLOR)) THEN
        NR = R + DR
        NC = C + DC
        
        COUNT = 0
        DO WHILE (INBOARD(NR, NC) .AND. (BOARD(NR, NC) .EQ. OPPOCOLOR))
          COUNT = COUNT + 1
          TEMPR(COUNT) = NR
          TEMPC(COUNT) = NC          
          
          NR = NR + DR
          NC = NC + DC
        ENDDO
        
        IF (INBOARD(NR, NC) .AND. (BOARD(NR, NC) .EQ. COLOR)) THEN
          VALIDMOVE(R, C) = .TRUE.
          DO 10 I = 1, COUNT
            CPTRN(R, C) = CPTRN(R, C) + 1
            CPTRR(R, C, CPTRN(R, C)) = TEMPR(I)
            CPTRC(R, C, CPTRN(R, C)) = TEMPC(I)
10        CONTINUE            
        ENDIF
      ENDIF
      
      RETURN
      END
     
     
     
      LOGICAL FUNCTION INBOARD(R, C)
      IMPLICIT NONE
      
      INTEGER R, C
      
      IF (R .LT. 1 .OR. R .GT. 8 .OR. C .LT. 1 .OR. C .GT. 8) THEN
        INBOARD = .FALSE.
      ELSE
        INBOARD = .TRUE.
      ENDIF
      RETURN
      END
      
      SUBROUTINE PRINT8X8(B)
      IMPLICIT NONE
      INTEGER B(8, 8)
      INTEGER I, J
      
      DO 10 I = 1, 8
        PRINT 100, (B(I, J), J = 1, 8)
10    CONTINUE

100   FORMAT(8(I3))
      RETURN
      END
              
        	 
        	 