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);
        }
    }
}







COBOL: identification division.
program-id. game-of-life-program.
data division.
working-storage section.
01  grid.
    05 cell-table.
        10 row occurs 5 times.
            15 cell pic x value space occurs 5 times.
    05 next-gen-cell-table.
        10 next-gen-row occurs 5 times.
            15 next-gen-cell pic x occurs 5 times.
01  counters.
    05 generation pic 9.
    05 current-row pic 9.
    05 current-cell pic 9.
    05 living-neighbours pic 9.
    05 neighbour-row pic 9.
    05 neighbour-cell pic 9.
    05 check-row pic s9.
    05 check-cell pic s9.
procedure division.
control-paragraph.
    perform blinker-paragraph varying current-cell from 2 by 1
    until current-cell is greater than 4.
    perform show-grid-paragraph through life-paragraph
    varying generation from 0 by 1
    until generation is greater than 2.
    stop run.
blinker-paragraph.
    move '#' to cell(3,current-cell).
show-grid-paragraph.
    display 'GENERATION ' generation ':'.
    display '   +---+'.
    perform show-row-paragraph varying current-row from 2 by 1
    until current-row is greater than 4.
    display '   +---+'.
    display ''.
life-paragraph.
    perform update-row-paragraph varying current-row from 2 by 1
    until current-row is greater than 4.
    move next-gen-cell-table to cell-table.
show-row-paragraph.
    display '   |' with no advancing.
    perform show-cell-paragraph varying current-cell from 2 by 1
    until current-cell is greater than 4.
    display '|'.
show-cell-paragraph.
    display cell(current-row,current-cell) with no advancing.
update-row-paragraph.
    perform update-cell-paragraph varying current-cell from 2 by 1
    until current-cell is greater than 4.
update-cell-paragraph.
    move 0 to living-neighbours.
    perform check-row-paragraph varying check-row from -1 by 1
    until check-row is greater than 1.
    evaluate living-neighbours,
        when 2 move cell(current-row,current-cell) to next-gen-cell(current-row,current-cell),
        when 3 move '#' to next-gen-cell(current-row,current-cell),
        when other move space to next-gen-cell(current-row,current-cell),
    end-evaluate.
check-row-paragraph.
    add check-row to current-row giving neighbour-row.
    perform check-cell-paragraph varying check-cell from -1 by 1
    until check-cell is greater than 1.
check-cell-paragraph.
    add check-cell to current-cell giving neighbour-cell.
    if cell(neighbour-row,neighbour-cell) is equal to '#',
    and check-cell is not equal to zero or check-row is not equal to zero,
    then add 1 to living-neighbours.