game

package
v0.0.0-...-300b5c6 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2017 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Constants

View Source
const BoardSide = 4

Variables

This section is empty.

Functions

This section is empty.

Types

type Board

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

func (*Board) ColorTest

func (b *Board) ColorTest()

ColorTest fills the board with all "legal" values for testing purposes.

func (Board) HasMovesLeft

func (b Board) HasMovesLeft() bool

HasMovesLeft returns true if the board can be played (ie. not in an endgame situation).

func (Board) IsFull

func (b Board) IsFull() bool

IsFull returns true if the board has a value > 0 in every tile.

func (Board) Moves

func (b Board) Moves() int

func (*Board) PlaceRandom

func (b *Board) PlaceRandom() error

PlaceRandom places a 2 or a 4 (10% chance) in a random empty tile.

func (*Board) Reset

func (b *Board) Reset()

func (Board) Score

func (b Board) Score() int

func (*Board) Shift

func (b *Board) Shift(dir Direction) bool
Shift pushes all tiles in the given direction until they either merge, reach

the border, or reach a tile with a different value.

If no movement occured, Shift returns false.

The algorithm is quite naive and was taken from C++ code I wrote at a 4 hours hackathon years ago, it iterates over all cells to merge/displace them and does this BoardSide times to ensure no gap is left between tiles. To avoid collapsing a whole row (eg. 2 2 4 8 -> 16 instead of 0 4 4 8) each tile that resulted from a merge is marked as "frozen" and will be skipped for the next iterations.

func (Board) String

func (b Board) String() string

String returns a human-readable version of the Board.

func (Board) Won

func (b Board) Won() bool

Won returns true if board holds a winning game, meaning the player reached 2048 (it's the name of the game).

type Direction

type Direction int
const (
	DirRight Direction = iota
	DirDown  Direction = iota
	DirLeft  Direction = iota
	DirUp    Direction = iota
)

Jump to

Keyboard shortcuts

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