program main !*****************************************************************************80 ! !! life_serial() simulates the game of life(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 September 2013 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Martin Gardner, ! Mathematical Games: ! The Fantastic Combinations of John Conway's new solitaire game "Life", ! Scientific American, ! Volume 223, Number 4, October 1970, pages 120-123. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) character ( len = 255 ) filename integer it integer it_max integer m integer n integer, allocatable :: grid(:,:) real ( kind = rk ) prob call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'LIFE_SERIAL():' write ( *, '(a)' ) ' FORTRAN90 version' write ( *, '(a)' ) ' Carry out a few steps of John Conway''s' write ( *, '(a)' ) ' Game of Life.' write ( *, '(a)' ) '' filename = 'life_000.txt' it_max = 10 m = 10 n = 10 prob = 0.20D+00 allocate ( grid(0:m+1,0:n+1) ) do it = 0, it_max if ( it == 0 ) then call life_init ( prob, m, n, grid ) else call life_update ( m, n, grid ) end if call life_write ( filename, m, n, grid ) write ( *, '(2x,a)' ) trim ( filename ) call filename_inc ( filename ) end do deallocate ( grid ) ! ! Terminate. ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'LIFE_SERIAL' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) ' ' call timestamp ( ) stop 0 end subroutine filename_inc ( filename ) !*****************************************************************************80 ! !! filename_inc() increments a partially numeric filename. ! ! Discussion: ! ! It is assumed that the digits in the name, whether scattered or ! connected, represent a number that is to be increased by 1 on ! each call. If this number is all 9's on input, the output number ! is all 0's. Non-numeric letters of the name are unaffected. ! ! If the name is empty, then the routine stops. ! ! If the name contains no digits, the empty string is returned. ! ! Example: ! ! Input Output ! ----- ------ ! 'a7to11.txt' 'a7to12.txt' ! 'a7to99.txt' 'a8to00.txt' ! 'a9to99.txt' 'a0to00.txt' ! 'cat.txt' ' ' ! ' ' STOP! ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 19 September 2012 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character ( len = * ) FILENAME. ! On input, a character string to be incremented. ! On output, the incremented string. ! implicit none character c integer change integer digit character ( len = * ) filename integer i integer lens lens = len_trim ( filename ) if ( lens <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILENAME_INC - Fatal error!' write ( *, '(a)' ) ' The input string is empty.' stop end if change = 0 do i = lens, 1, -1 c = filename(i:i) if ( lge ( c, '0' ) .and. lle ( c, '9' ) ) then change = change + 1 digit = ichar ( c ) - 48 digit = digit + 1 if ( digit == 10 ) then digit = 0 end if c = char ( digit + 48 ) filename(i:i) = c if ( c /= '0' ) then return end if end if end do ! ! No digits were found. Return blank. ! if ( change == 0 ) then filename = ' ' return end if return end subroutine get_unit ( iunit ) !*****************************************************************************80 ! !! get_unit() returns a free FORTRAN unit number. ! ! Discussion: ! ! A "free" FORTRAN unit number is a value between 1 and 99 which ! is not currently associated with an I/O device. A free FORTRAN unit ! number is needed in order to open a file with the OPEN command. ! ! If IUNIT = 0, then no free FORTRAN unit could be found, although ! all 99 units were checked (except for units 5, 6 and 9, which ! are commonly reserved for console I/O). ! ! Otherwise, IUNIT is a value between 1 and 99, representing a ! free FORTRAN unit. Note that GET_UNIT assumes that units 5 and 6 ! are special, and will never return those values. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 26 October 2008 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Output, integer IUNIT, the free unit number. ! implicit none integer i integer ios integer iunit logical lopen iunit = 0 do i = 1, 99 if ( i /= 5 .and. i /= 6 .and. i /= 9 ) then inquire ( unit = i, opened = lopen, iostat = ios ) if ( ios == 0 ) then if ( .not. lopen ) then iunit = i return end if end if end if end do return end subroutine life_init ( prob, m, n, grid ) !*****************************************************************************80 ! !! life_init() initializes the life grid. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 08 September 2013 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, real ( kind = rk ) PROB, the probability that a grid cell ! should be alive. ! ! Input, integer M, N, the number of rows and columns ! of interior grid cells. ! ! Output, integer GRID(1+M+1,1+N+1), the initial grid. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n integer grid(0:m+1,0:n+1) integer i integer j real ( kind = rk ) prob real ( kind = rk ) r grid(0:m+1,0:n+1) = 0 do j = 1, n do i = 1, m call random_number ( harvest = r ) if ( r <= prob ) then grid(i,j) = 1 end if end do end do return end subroutine life_update ( m, n, grid ) !*****************************************************************************80 ! !! life_update() updates a Life grid. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 08 September 2013 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns ! of interior grid cells. ! ! Input/output, integer GRID(1+M+1,1+N+1), the data. ! implicit none integer m integer n integer grid(0:m+1,0:n+1) integer i integer j integer s(m,n) ! ! Count the neighbors. ! s(1:m,1:n) = grid(0:m-1,0:n-1) + grid(0:m-1,1:n) + grid(0:m-1,2:n+1) & + grid(1:m, 0:n-1) + grid(1:m, 2:n+1) & + grid(2:m+1,0:n-1) + grid(2:m+1,1:n) + grid(2:m+1,2:n+1) ! ! Any dead cell with 3 live neighbors becomes alive. ! Any living cell with less than 2 or more than 3 neighbors dies. ! do j = 1, n do i = 1, m if ( grid(i,j) == 0 ) then if ( s(i,j) == 3 ) then grid(i,j) = 1 end if else if ( grid(i,j) == 1 ) then if ( s(i,j) < 2 .or. 3 < s(i,j) ) then grid(i,j) = 0 end if end if end do end do return end subroutine life_write ( output_filename, m, n, grid ) !*****************************************************************************80 ! !! life_write() writes a grid to a file. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 08 September 2013 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) OUTPUT_FILENAME, the output file name. ! ! Input, integer M, N, the number of rows and columns ! of interior grid cells. ! ! Input, integer GRID(1+M+1,1+N+1), the data. ! implicit none integer m integer n integer grid(0:m+1,0:n+1) integer j character ( len = * ) output_filename integer output_status integer output_unit character ( len = 30 ) string ! ! Open the file. ! call get_unit ( output_unit ) open ( unit = output_unit, file = output_filename, & status = 'replace', iostat = output_status ) if ( output_status /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'LIFE_WRITE - Fatal error!' write ( *, '(a,i8)' ) ' Could not open the output file "' // & trim ( output_filename ) // '" on unit ', output_unit output_unit = -1 stop end if ! ! Create a format string. ! if ( 0 < m .and. 0 < n ) then write ( string, '(a1,i8,a8)' ) '(', m+2, '(1x,i1))' ! ! Write the data. ! do j = 0, n + 1 write ( output_unit, string ) grid(0:m+1,j) end do end if ! ! Close the file. ! close ( unit = output_unit ) return end subroutine timestamp ( ) !*****************************************************************************80 ! !! timestamp() prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 18 May 2013 ! ! Author: ! ! John Burkardt ! implicit none character ( len = 8 ) ampm integer d integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s integer values(8) integer y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(i2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end