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





Python: cell_size = 10
sample = 10 
play = False   
last_cell = 0

def setup():
    global grid, next_grid, rows, cols
    size(800, 500)

    rows = height / cell_size
    cols = width / cell_size
    grid = empty_grid()
    next_grid = empty_grid()
    randomize_grid()

    println("Press 'space' to start/stop")
    println("'e' to clear all cells")
    println("'b' demonstrate 'blinker'")
    println("'g' demonstrate glider")
    println("'r' to randomize grid")
    println("'+' and '-' to change speed")

def draw():
    background(0)
    for i in range(cols):
        x = i * cell_size
        for j in range(rows):
            y = j * cell_size
            current_state = grid[i][j]
            fill(255)
            noStroke()
            if current_state:
                rect(x, y, cell_size, cell_size)
            if play:
                ngbs_alive = calc_ngbs_alive(i, j)
                result = rule(current_state, ngbs_alive)
                next_grid[i][j] = result
                
    if play and frameCount % sample == 0 and not mousePressed:
        step()

def rule(current, ngbs):
    
    if ngbs < 2 or ngbs > 3:
        return 0  
    elif ngbs == 3:
        return 1  
    else:
        return current  

def calc_ngbs_alive(i, j):
    NEIGHBOURS = ((-1, 00), (01, 00),  
                  (-1, -1), (00, -1),
                  (01, -1), (-1, 01),
                  (00, 01), (01, 01))
    alive = 0
    for iv, jv in NEIGHBOURS:
        alive += grid[(i + iv) % cols][(j + jv) % rows]
    return alive

def empty_grid():
    grid = []
    for _ in range(cols):
        grid.append([0] * rows)
    return grid

def randomize_grid():
    from random import choice
    for i in range(cols):
        for j in range(rows):
            grid[i][j] = choice((0, 1))

def step():
    global grid, next_grid
    grid = next_grid
    next_grid = empty_grid()

def keyReleased():
    global grid, play, sample
    if key == "e":
        grid = empty_grid()
    if key == "r":
        randomize_grid()
    if key == "g":
         grid[10][10:13] = [0, 1, 0]       
         grid[11][10:13] = [0, 0, 1]       
         grid[12][10:13] = [1, 1, 1]       
    if key == "b":
         grid[10][10:13] = [0, 1, 0]       
         grid[11][10:13] = [0, 1, 0]       
         grid[12][10:13] = [0, 1, 0]               
    if key == " ":
        play = not play 
    if  str(key) in '+=':
        sample = max(sample - 1, 1);
    if key == '-':
        sample += 1

def mousePressed():
    paint()
    
def mouseDragged():
    paint()

def paint():
    global last_cell
    i, j = mouseX // cell_size, mouseY // cell_size
    p = j * cols + i
    if p != last_cell:
        last_cell = p
        grid[i][j] = (1, 0)[grid[i][j]]