golife

package module
v0.1.1-0...-1cadeb4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2020 License: MIT Imports: 10 Imported by: 0

README

Golife - A Game of Life project

Go Report Card Codacy Badge HitCount PR's Welcome

Simulate John Conway's Game of Life in your terminal. Interface is powered by tcell.

Demo

asciicast

Features

  • Supports the standard Game of Life rules as well as other variations such as HighLife, Amoeba, and others (see rules).
  • It can simulate preset patterns in the RLE, Life 1.06, or Plaintext formats (thanks to life).
  • Supports simulation speed increment of decrement.
  • Supports stepping through the simulation, one generation at a time.
  • Supports themes (limited for now).

Installation

If you have Go installed, you can use the following command to install Golife:

$ go get github.com/ayoisaiah/golife/cmd/...

Alternatively, precompiled binaries are available for Linux, Windows, and macOS here (only for amd64).

Usage

Basic usage:

$ golife

See demo

Preset patterns

RLE, Life 1.06 and Plaintext patterns are supported by Golife. For example, here's a huge list of Game of Life patterns in the RLE format.

You can load a preset pattern from a file (example):

$ golife --file gosperglidergun.cells

See demo

Or you can load a preset pattern from a URL:

$ golife --url "http://copy.sh/life/examples/spider.rle" --input-format rle

See demo

Rules

The default Game of Life rules are as follows:

  • Any live cell with two or three live neighbours survives.
  • Any dead cell with three live neighbours becomes a live cell.
  • All other live cells die in the next generation. Similarly, all other dead cells stay dead.

However, Golife supports other variants as well. You can list them all using the following command:

$ golife rules
Default:
Survives: [2 3], Born: [3]

DayAndNight:
Survives: [3 5 6 7 8], Born: [3 6 7 8]

Coral:
Survives: [4 5 6 7 8], Born: [3]

2x2:
Survives: [1 2 5], Born: [3 6]

34Life:
Survives: [3 4], Born: [3 4]

Amoeba:
Survives: [1 3 5 8], Born: [3 5 7]

Assimilation:
Survives: [4 5 6 7], Born: [3 4 5]
...

Select a rule:

$ golife --rule Amoeba

See demo

Themes

List available themes.

$ golife themes
WhiteOnBlack
BlackOnWhite

Select a theme:

$ golife --theme BlackOnWhite

See demo

Help
$ golife --help

Credits

Golife relies heavily on other open source software listed below:

Contribute

Bug reports, or pull requests are much welcome!

Licence

Created by Ayooluwa Isaiah and released under the terms of the MIT Licence.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Rule2x2 = Rule{
	Name:     "2x2",
	Survives: []int{1, 2, 5},
	Born:     []int{3, 6},
}
View Source
var Rule34Life = Rule{
	Name:     "34Life",
	Survives: []int{3, 4},
	Born:     []int{3, 4},
}
View Source
var RuleAmoeba = Rule{
	Name:     "Amoeba",
	Survives: []int{1, 3, 5, 8},
	Born:     []int{3, 5, 7},
}
View Source
var RuleAssimilation = Rule{
	Name:     "Assimilation",
	Survives: []int{4, 5, 6, 7},
	Born:     []int{3, 4, 5},
}
View Source
var RuleCoagulations = Rule{
	Name:     "Coagulations",
	Survives: []int{2, 3, 5, 6, 7, 8},
	Born:     []int{3, 7, 8},
}
View Source
var RuleCoral = Rule{
	Name:     "Coral",
	Survives: []int{4, 5, 6, 7, 8},
	Born:     []int{3},
}
View Source
var RuleDayAndNight = Rule{
	Name:     "DayAndNight",
	Survives: []int{3, 5, 6, 7, 8},
	Born:     []int{3, 6, 7, 8},
}
View Source
var RuleDefault = Rule{
	Name:     "Default",
	Survives: []int{2, 3},
	Born:     []int{3},
}
View Source
var RuleDiamoeba = Rule{
	Name:     "Diamoeba",
	Survives: []int{5, 6, 7, 8},
	Born:     []int{3, 5, 6, 7, 8},
}
View Source
var RuleFlakes = Rule{
	Name:     "Flakes",
	Survives: []int{0, 1, 2, 3, 4, 5, 6, 7, 8},
	Born:     []int{3},
}
View Source
var RuleGnarl = Rule{
	Name:     "Gnarl",
	Survives: []int{1},
	Born:     []int{1},
}
View Source
var RuleHighLife = Rule{
	Name:     "HighLife",
	Survives: []int{2, 3},
	Born:     []int{3, 6},
}
View Source
var RuleInverseLife = Rule{
	Name:     "InverseLife",
	Survives: []int{2, 3, 6, 7, 8},
	Born:     []int{0, 1, 2, 3, 4, 7, 8},
}
View Source
var RuleLongLife = Rule{
	Name:     "LongLife",
	Survives: []int{5},
	Born:     []int{3, 4, 5},
}
View Source
var RuleMaze = Rule{
	Name:     "Maze",
	Survives: []int{1, 2, 3, 4, 5},
	Born:     []int{3},
}
View Source
var RuleMazectric = Rule{
	Name:     "Mazectric",
	Survives: []int{1, 2, 3, 4},
	Born:     []int{3},
}
View Source
var RuleMove = Rule{
	Name:     "Move",
	Survives: []int{2, 4, 5},
	Born:     []int{3, 6, 8},
}
View Source
var RulePseudoLife = Rule{
	Name:     "PseudoLife",
	Survives: []int{2, 3, 8},
	Born:     []int{3, 5, 7},
}
View Source
var RuleReplicator = Rule{
	Name:     "Replicator",
	Survives: []int{1, 3, 5, 7},
	Born:     []int{1, 3, 5, 7},
}
View Source
var RuleSeeds = Rule{
	Name:     "Seeds",
	Survives: []int{},
	Born:     []int{2},
}
View Source
var RuleServiettes = Rule{
	Name:     "Serviettes",
	Survives: []int{},
	Born:     []int{2, 3, 4},
}
View Source
var RuleStains = Rule{
	Name:     "Stains",
	Survives: []int{2, 3, 5, 6, 7, 8},
	Born:     []int{3, 6, 7, 8},
}
View Source
var RuleWalledCities = Rule{
	Name:     "WalledCities",
	Survives: []int{2, 3, 4, 5},
	Born:     []int{4, 5, 6, 7, 8},
}
View Source
var ThemeBlackOnWhite = Theme{
	Name:       "BlackOnWhite",
	Background: tcell.NewRGBColor(255, 255, 255),
	Foreground: tcell.NewRGBColor(51, 51, 51),
	CellColor:  tcell.NewRGBColor(34, 34, 34),
}
View Source
var ThemeWhiteOnBlack = Theme{
	Name:       "WhiteOnBlack",
	Background: tcell.NewRGBColor(51, 51, 51),
	Foreground: tcell.NewRGBColor(255, 255, 255),
	CellColor:  tcell.NewRGBColor(250, 250, 250),
}

Functions

func ListRules

func ListRules()

func ListThemes

func ListThemes()

Types

type Board

type Board struct {
	Game *Game
	Grid [][]CellState
	Dimension
}

Board represents the game board

type CellState

type CellState int

CellState represents the state of a cell 0 is dead and 1 is alive

type Dimension

type Dimension struct {
	// contains filtered or unexported fields
}

Dimension represents the start coordinates as well as width and height of an entity

type Game

type Game struct {
	Screen        tcell.Screen
	PresetPattern *life.Pattern
	Header        *Header
	Board         *Board
	Paused        bool
	EdgeWrap      bool
	FPS           int
	Rule          Rule
	Theme
	Stats
}

Game represents a game of life simulation

func NewGame

func NewGame(c *cli.Context) (*Game, error)

NewGame creates and returns a new game instance

func (*Game) Draw

func (g *Game) Draw()

Draw paints the components of the Game on the screen

func (*Game) Init

func (g *Game) Init() error

Init sets the initial pattern of cells on the grid and resets the generation count

func (*Game) NextGen

func (g *Game) NextGen()

NextGen computes the next generation of cells in the grid

func (*Game) SelectRule

func (g *Game) SelectRule(name string)

SelectRule selects a rule from the rulelist The default is Conway's Game of Life

func (*Game) SelectTheme

func (g *Game) SelectTheme(name string)

SelectTheme sets the theme colours

func (*Game) Start

func (g *Game) Start() error

Start begins the game loop

func (*Game) Step

func (g *Game) Step()

Step pauses the game and displays the next generation

func (*Game) TogglePause

func (g *Game) TogglePause()

TogglePause pauses or plays the game

type Header struct {
	Game         *Game
	Instructions []string
	Dimension
}

Header represents the section where instructions and game statistics are displayed

type Rule

type Rule struct {
	Name     string
	Survives []int
	Born     []int
}

type Stats

type Stats struct {
	GenerationCount int
	Population      int
}

Stats is the current stats of the game

type Theme

type Theme struct {
	Name       string
	Background tcell.Color
	Foreground tcell.Color
	CellColor  tcell.Color
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL