Task: Conway's Game of Life




Go: package main

import (
	"bytes"
	"fmt"
	"math/rand"
	"time"
)

type Field struct {
	s    [][]bool
	w, h int
}

func NewField(w, h int) Field {
	s := make([][]bool, h)
	for i := range s {
		s[i] = make([]bool, w)
	}
	return Field{s: s, w: w, h: h}
}

func (f Field) Set(x, y int, b bool) {
	f.s[y][x] = b
}

func (f Field) Next(x, y int) bool {
	on := 0
	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			if f.State(x+i, y+j) && !(j == 0 && i == 0) {
				on++
			}
		}
	}
	return on == 3 || on == 2 && f.State(x, y)
}

func (f Field) State(x, y int) bool {
	for y < 0 {
		y += f.h
	}
	for x < 0 {
		x += f.w
	}
	return f.s[y%f.h][x%f.w]
}

type Life struct {
	w, h int
	a, b Field
}

func NewLife(w, h int) *Life {
	a := NewField(w, h)
	for i := 0; i < (w * h / 2); i++ {
		a.Set(rand.Intn(w), rand.Intn(h), true)
	}
	return &Life{
		a: a,
		b: NewField(w, h),
		w: w, h: h,
	}
}

func (l *Life) Step() {
	for y := 0; y < l.h; y++ {
		for x := 0; x < l.w; x++ {
			l.b.Set(x, y, l.a.Next(x, y))
		}
	}
	l.a, l.b = l.b, l.a
}

func (l *Life) String() string {
	var buf bytes.Buffer
	for y := 0; y < l.h; y++ {
		for x := 0; x < l.w; x++ {
			b := byte(' ')
			if l.a.State(x, y) {
				b = '*'
			}
			buf.WriteByte(b)
		}
		buf.WriteByte('\n')
	}
	return buf.String()
}

func main() {
	l := NewLife(80, 15)
	for i := 0; i < 300; i++ {
		l.Step()
		fmt.Print("\x0c")
		fmt.Println(l)
		time.Sleep(time.Second / 30)
	}
}








Ada: WITH Ada.Text_IO;  USE Ada.Text_IO;
 
PROCEDURE Life IS
   SUBTYPE Cell IS Natural RANGE 0 .. 1;

   TYPE Petri_Dish IS ARRAY (Positive RANGE <>, Positive RANGE <>) OF Cell;

   PROCEDURE Step (Gen : IN OUT Petri_Dish) IS
      Above       : ARRAY (Gen'Range (2)) OF Cell := (OTHERS => 0);
      Left, This  : Cell;
   BEGIN
      FOR I IN Gen'First (1) + 1 .. Gen'Last (1) - 1 LOOP
         Left := 0;
         FOR J IN Gen'First (2) + 1 .. Gen'Last (2) - 1 LOOP
            This := (CASE Above (J - 1) + Above (J) + Above (J + 1) +
                          Left			    + Gen (I, J + 1) +
                          Gen (I + 1, J - 1) + Gen (I + 1, J) 	+ Gen (I + 1, J + 1) IS
                        WHEN 2      => Gen (I, J),
                        WHEN 3      => 1,
                        WHEN OTHERS => 0);
            Above (J - 1):= Left;
            Left         := Gen (I, J);
            Gen (I, J) 	 := This;
         END LOOP;
         Above (Above'Last - 1) := Left;
      END LOOP;
   END Step;
 
   PROCEDURE Put (Gen : Petri_Dish) IS
   BEGIN
      FOR I IN Gen'Range (1) LOOP
         FOR J IN Gen'Range (2) LOOP
            Put ( if Gen (I, J) = 0 then " " else "#");
         END LOOP;
         New_Line;
      END LOOP;
   END Put;
 
   Blinker : Petri_Dish := (2 .. 4 => (0, 0, 1, 0, 0), 1 | 5 => (0, 0, 0, 0, 0));
   Glider  : Petri_Dish (1..6,1..11):= (2 => (3 => 1, others => 0),
                                        3 => (4 => 1, others => 0),
                                        4 => (2|3|4=>1, others => 0),
                                        others => (others => 0));
   PROCEDURE Put_And_Step_Generation (N : Positive; Name : String; P : IN OUT Petri_Dish) IS
   BEGIN
      FOR Generation IN 1 .. N LOOP
         Put_Line (Name & Generation'Img);
         Put (P);
         Step (P);
      END LOOP;
   END Put_And_Step_Generation;

BEGIN
   Put_And_Step_Generation (3, "Blinker", Blinker);
   Put_And_Step_Generation (5, "Glider", Glider);
END Life;