gameofwar

package
v0.0.0-...-adc953a Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

=========================================================================== > Game of Life and War ___________________________________________________________________________

Grid Square Types

0 = Neutral 1 = Player 1 2 = Player 2 3 = Red 4 = Pink [5 - 10] = Red, in different

Credits

- Joshua Fan - The Idea for the "Game of War" - Golang.org - Code Snippets for Implementation of Conway's Game of Life - John Horton Conway - For inventing the "Game of Life" ___________________________________________________________________________ ===========================================================================

Index

Constants

View Source
const (
	MIN_GAME_SPEED = 1
	MAX_GAME_SPEED = 2
)

Variables

This section is empty.

Functions

func Abundance

func Abundance(neighborhood map[uint8]uint8) ([]uint8, uint8)

WhoEatsMe returns the color that consumes the specified color, if nearby. If will only return a 0 if it passes through all of the cases.

func WhoEatsMe(myColor uint8) uint8 {
	switch myColor {
	case 0:
		return 3 // empty  <- red
	case 1:
		return 2 // 1 <- 2
	case 2:
		return 1 // 2 <- 1
	case 3:
		return 4
	case 4:
		return 5
	case 5:
		return 6
	case 6:
		return 3
	}
	return 0 // empty eaten by empty
	// uint8(rand.Intn(5))
}

Abundance the color(s), which corresponds to a value [1-3], and the number of counted neighbors of that color [0-8]. Since there could be a tie for the highest count, multiple colors are returned in an array.

Types

type Field

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

Field represents a two-dimensional field of cells.

func NewField

func NewField(w, h int) *Field

NewField returns an empty field of the specified width and height.

func (*Field) Next

func (f *Field) Next(x, y int) uint8

Next returns the state of the specified cell at the next time step.

func (*Field) Set

func (f *Field) Set(x, y int, b uint8)

Set sets the state of the specified cell to the given value.

func (*Field) WhatIs

func (f *Field) WhatIs(x, y int) uint8

WhatsIs reports the number that is at the specified cell.

type GameInstance

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

GameInstance contains all the information that would be found in the game: including the map, game state, players, and settings.

func NewGameInstance

func NewGameInstance(w, h int) *GameInstance

NewGameInstance initializes a fresh game, only asking for a map size. All of the other settings start at default.

func (*GameInstance) ChangeAt

func (g *GameInstance) ChangeAt(x, y int, val uint8)

func (*GameInstance) DropBomb

func (g *GameInstance) DropBomb(x, y int) bool

DropBomb takes a player number (1 or 2), and a position on the grid. If that action is allowed, then it will do that action

func (*GameInstance) FreshGameBoard

func (g *GameInstance) FreshGameBoard()

func (*GameInstance) LifeStateMessage

func (g *GameInstance) LifeStateMessage() []byte

LifeStateMessage returns an encoded Json message, ready to be sent.

func (*GameInstance) LifeUpdate

func (g *GameInstance) LifeUpdate()

func (*GameInstance) RandomizeGameBoard

func (g *GameInstance) RandomizeGameBoard(numberToMake int)

ResetGameInstance will randomly create player squares. The amount of player squares to make is specified by the "numberToMake".

type GridState

type GridState struct {
	GridState string
}

type Life

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

Life stores the state of a round of Conway's Game of Life.

func NewLife

func NewLife(w, h int) *Life

NewLife returns a new Life game state with a random initial state.

func (*Life) AlterAt

func (l *Life) AlterAt(x, y int, val uint8)

AlterAt changes the value at a specific position of the field.

func (*Life) Step

func (l *Life) Step()

Step advances the game by one instant, recomputing and updating all cells.

func (*Life) String

func (l *Life) String() string

String returns the game board as a string.

type Neighborhood

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

Neighborhood stores 2 useful maps: 1. The cell values based on location, 2. The total number of cells found nearby, based on a specific cell value.

func NewMooreNeighborhood

func NewMooreNeighborhood(f *Field, x, y int) *Neighborhood

NewMooreNeighborhood returns a neighborhood of cells that include the 4 cardinal directions and the 4 diagonals, for a total of 8 nearby cells.

func (*Neighborhood) Count

func (n *Neighborhood) Count(cellValue uint8) uint8

Count returns the total number of cells in the neighborhood that contain the specified value.

func (*Neighborhood) East

func (n *Neighborhood) East() uint8

East returns the value at coordinate (+1, +0) relative to center.

func (*Neighborhood) North

func (n *Neighborhood) North() uint8

North returns the value at coordinate (+0, -1) relative to center.

func (*Neighborhood) NorthEast

func (n *Neighborhood) NorthEast() uint8

NorthEast returns the value at coordinate (+1, -1) relative to center.

func (*Neighborhood) NorthWest

func (n *Neighborhood) NorthWest() uint8

NorthWest returns the value at coordinate (-1, -1) relative to center.

func (*Neighborhood) South

func (n *Neighborhood) South() uint8

South returns the value at coordinate (+0, +1) relative to center.

func (*Neighborhood) SouthEast

func (n *Neighborhood) SouthEast() uint8

SouthEast returns the value at coordinate (+1, +1) relative to center.

func (*Neighborhood) SouthWest

func (n *Neighborhood) SouthWest() uint8

SouthWest returns the value at coordinate (+1, -1) relative to center.

func (*Neighborhood) West

func (n *Neighborhood) West() uint8

West returns the value at coordinate (-1, +0) relative to center.

Jump to

Keyboard shortcuts

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