Documentation ¶
Index ¶
- Constants
- type Board
- func (b *Board) ColorTest()
- func (b Board) HasMovesLeft() bool
- func (b Board) IsFull() bool
- func (b Board) Moves() int
- func (b *Board) PlaceRandom() error
- func (b *Board) Reset()
- func (b Board) Score() int
- func (b *Board) Shift(dir Direction) bool
- func (b Board) String() string
- func (b Board) Won() bool
- type Direction
Constants ¶
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 ¶
HasMovesLeft returns true if the board can be played (ie. not in an endgame situation).
func (*Board) PlaceRandom ¶
PlaceRandom places a 2 or a 4 (10% chance) in a random empty tile.
func (*Board) Shift ¶
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.