Task: Conway's Game of Life



C#: using System;
using System.Text;
using System.Threading;
 
namespace ConwaysGameOfLife
{
    
    class Program
    {
        
        private const int DELAY = 50;
 
        
        private const ConsoleColor DEAD_COLOR = ConsoleColor.White;
        private const ConsoleColor LIVE_COLOR = ConsoleColor.Black;
 
        
        private const ConsoleColor EXTRA_COLOR = ConsoleColor.Gray;
 
        private const char EMPTY_BLOCK_CHAR = ' ';
        private const char FULL_BLOCK_CHAR = '\u2588';
 
        
        private static bool[,] board;
 
        
        private static int width = 32;
        private static int height = 32;
 
        
        private static bool loopEdges = true;
 
 
        static void Main(string[] args)
        {
            
            initializeDemoBoard();
 
            initializeConsole();
 
            
            while (!Console.KeyAvailable || Console.ReadKey(true).Key != ConsoleKey.Escape) {
                Program.drawBoard();
                Program.updateBoard();
 
                
                Thread.Sleep(DELAY);
            }
        }
 
        
        private static void initializeConsole()
        {
            Console.BackgroundColor = EXTRA_COLOR;
            Console.Clear();
 
            Console.CursorVisible = false;
 
            
            
            int width = Math.Max(Program.width, 8) * 2 + 1;
            int height = Math.Max(Program.height, 8) + 1;
            Console.SetWindowSize(width, height);
            Console.SetBufferSize(width, height);
 
            Console.BackgroundColor = DEAD_COLOR;
            Console.ForegroundColor = LIVE_COLOR;
        }
 
        
        private static void initializeRandomBoard()
        {
            var random = new Random();
 
            Program.board = new bool[Program.width, Program.height];
            for (var y = 0; y < Program.height; y++) {
                for (var x = 0; x < Program.width; x++) {
                    
                    Program.board[x, y] = random.Next(2) == 0;
                }
            }
        }
 
        
        private static void initializeDemoBoard()
        {
            Program.width = 3;
            Program.height = 3;
 
            Program.loopEdges = false;
 
            Program.board = new bool[3, 3];
            Program.board[1, 0] = true;
            Program.board[1, 1] = true;
            Program.board[1, 2] = true;
        }
 
        
        private static void drawBoard()
        {
            
            var builder = new StringBuilder();
 
            for (var y = 0; y < Program.height; y++) {
                for (var x = 0; x < Program.width; x++) {
                    char c = Program.board[x, y] ? FULL_BLOCK_CHAR : EMPTY_BLOCK_CHAR;
 
                    
                    builder.Append(c);
                    builder.Append(c);
                }
                builder.Append('\n');
            }
 
            
            Console.SetCursorPosition(0, 0);
            Console.Write (builder.ToString());
        }
 
        
        private static void updateBoard()
        {
            
            bool[,] newBoard = new bool[Program.width, Program.height];
 
            for (var y = 0; y < Program.height; y++) {
                for (var x = 0; x < Program.width; x++) {
                    var n = countLiveNeighbors(x, y);
                    var c = Program.board[x, y];
 
                    
                    
                    newBoard[x, y] = c && (n == 2 || n == 3) || !c && n == 3;
                }
            }
 
            
            Program.board = newBoard;
        }
 
        
        private static int countLiveNeighbors(int x, int y)
        {
            
            int value = 0;
 
            
            for (var j = -1; j <= 1; j++) {
                
                if (!Program.loopEdges && y + j < 0 || y + j >= Program.height) {
                    continue;
                }
 
                
                int k = (y + j + Program.height) % Program.height;
 
                for (var i = -1; i <= 1; i++) {
                    
                    if (!Program.loopEdges && x + i < 0 || x + i >= Program.width) {
                        continue;
                    }
 
                    
                    int h = (x + i + Program.width) % Program.width;
 
                    
                    value += Program.board[h, k] ? 1 : 0;
                }
            }
 
            
            return value - (Program.board[x, y] ? 1 : 0);
        }
    }
}








Fortran:  PROGRAM LIFE_2D
   IMPLICIT NONE
 
   INTEGER, PARAMETER :: gridsize = 10
   LOGICAL :: cells(0:gridsize+1,0:gridsize+1) = .FALSE.
   INTEGER :: i, j, generation=0
   REAL :: rnums(gridsize,gridsize)
 
 
 
 
 
 
    cells(3:5,3:5) = .TRUE. ; cells(6:8,6:8) = .TRUE.                      
 
 
 
   
   CALL Drawgen(cells(1:gridsize, 1:gridsize), generation)
   DO generation = 1, 8
      CALL NextgenV2(cells)
      CALL Drawgen(cells(1:gridsize, 1:gridsize), generation)
   END DO
 
 CONTAINS
 
   SUBROUTINE Drawgen(cells, gen)
     LOGICAL, INTENT(IN OUT) :: cells(:,:)
     INTEGER, INTENT(IN) :: gen
 
     WRITE(*, "(A,I0)") "Generation ", gen 
     DO i = 1, SIZE(cells,1)
        DO j = 1, SIZE(cells,2)
           IF (cells(i,j)) THEN
              WRITE(*, "(A)", ADVANCE = "NO") "#"
           ELSE
              WRITE(*, "(A)", ADVANCE = "NO") " "
           END IF
        END DO
        WRITE(*,*)
     END DO
     WRITE(*,*)
   END SUBROUTINE Drawgen
 
  SUBROUTINE Nextgen(cells)
     LOGICAL, INTENT(IN OUT) :: cells(0:,0:)
     LOGICAL :: buffer(0:SIZE(cells, 1)-1, 0:SIZE(cells, 2)-1)
     INTEGER :: neighbours, i, j
   
     buffer = cells   
     DO j = 1, SIZE(cells, 2)-2
        DO i = 1, SIZE(cells, 1)-2
          if(buffer(i, j)) then
            neighbours = sum(count(buffer(i-1:i+1, j-1:j+1), 1)) - 1
          else
            neighbours = sum(count(buffer(i-1:i+1, j-1:j+1), 1))
          end if
  
          SELECT CASE(neighbours)
            CASE (0:1, 4:8)
               cells(i,j) = .FALSE.
  
            CASE (2)
               
  
            CASE (3)
               cells(i,j) = .TRUE.
          END SELECT
          
        END DO
     END DO
  END SUBROUTINE Nextgen








  PURE SUBROUTINE NextgenV2(cells)
     LOGICAL, INTENT(IN OUT) :: cells(:,:)
     INTEGER(KIND=1) :: buffer(1:SIZE(cells, 1)-2,1:SIZE(cells, 2)-2)
     INTEGER :: gridsize, i, j
     
     gridsize=SIZE(cells, 1)
     buffer=0
     
     DO j=-1, 1
        DO i=-1,1
           IF(i==0 .AND. j==0) CYCLE
           WHERE(cells(i+2:gridsize-i-1,j+2:gridsize-j-1)) buffer=buffer+1
        END DO
     END DO

     WHERE(buffer<2 .or. buffer>3) cells(2:gridsize-1,2:gridsize-1) = .FALSE.
     WHERE(buffer==3) cells(2:gridsize-1,2:gridsize-1) = .TRUE.
  END SUBROUTINE NextgenV2

 END PROGRAM LIFE_2D