tetris

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Actions = ActionContainer{
	Unknown:         Action{actionUnknown},
	None:            Action{actionNone},
	Single:          Action{actionSingle},
	Double:          Action{actionDouble},
	Triple:          Action{actionTriple},
	Tetris:          Action{actionTetris},
	MiniTSpin:       Action{actionMiniTSpin},
	MiniTSpinSingle: Action{actionMiniTSpinSingle},
	TSpin:           Action{actionTSpin},
	TSpinSingle:     Action{actionTSpinSingle},
	TSpinDouble:     Action{actionTSpinDouble},
	TSpinTriple:     Action{actionTSpinTriple},
}

Actions is a global instance of ActionContainer that contains all possible values of type Action.

View Source
var RotationCompasses = map[byte]RotationCompass{
	'I': {
		{
			{X: -1, Y: 1}, {X: 0, Y: 1}, {X: -3, Y: 1}, {X: 0, Y: 3}, {X: -3, Y: 0},
		},
		{
			{X: 2, Y: -1}, {X: 0, Y: -1}, {X: 3, Y: -1}, {X: 0, Y: 0}, {X: 3, Y: -3},
		},
		{
			{X: -2, Y: 2}, {X: -3, Y: 2}, {X: 0, Y: 2}, {X: -3, Y: 0}, {X: 0, Y: 3},
		},
		{
			{X: 1, Y: -2}, {X: 3, Y: -2}, {X: 0, Y: -2}, {X: 3, Y: -3}, {X: 0, Y: 0},
		},
	},
	'O': {
		{
			{X: 0, Y: 0},
		},
		{
			{X: 0, Y: 0},
		},
		{
			{X: 0, Y: 0},
		},
		{
			{X: 0, Y: 0},
		},
	},

	'6': {
		{
			{X: 0, Y: 0}, {X: -1, Y: 0}, {X: -1, Y: 1}, {X: 0, Y: -2}, {X: -1, Y: -2},
		},
		{
			{X: 1, Y: 0}, {X: 0, Y: 0}, {X: 0, Y: -1}, {X: 1, Y: 2}, {X: 0, Y: 2},
		},
		{
			{X: -1, Y: 1}, {X: 0, Y: 1}, {X: 0, Y: 2}, {X: -1, Y: -1}, {X: 0, Y: -1},
		},
		{
			{X: 0, Y: -1}, {X: 1, Y: -1}, {X: 1, Y: -2}, {X: 0, Y: 1}, {X: 1, Y: 1},
		},
	},
}

RotationCompasses is a map of Tetrimino values to the coordinates used for rotation. Each slice should contain a coordinate for north, east, south, and west in that order. These are added to (clockwise) or subtracted from (counter-clockwise) the Tetrimino's position when rotating to ensure it rotates around the correct axis.

Functions

This section is empty.

Types

type Action

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

An Action performed by the user that corresponds to points.

func ParseAction

func ParseAction(a any) Action

ParseAction attempts to parse the given value into Action. It supports string, fmt.Stringer, int, int64, and int32. If the value is not a valid Action or the value is not a supported type, it will return the enums unknown value (unknownAction).

func (Action) EndsBackToBack

func (a Action) EndsBackToBack() (bool, error)

func (Action) GetPoints

func (a Action) GetPoints() int

func (Action) IsValid

func (a Action) IsValid() bool

func (Action) StartsBackToBack

func (a Action) StartsBackToBack() (bool, error)

func (Action) String

func (a Action) String() string

String returns the string representation of the Action.

type ActionContainer

type ActionContainer struct {
	Unknown         Action
	None            Action
	Single          Action
	Double          Action
	Triple          Action
	Tetris          Action
	MiniTSpin       Action
	MiniTSpinSingle Action
	TSpin           Action
	TSpinSingle     Action
	TSpinDouble     Action
	TSpinTriple     Action
}

type Coordinate

type Coordinate struct {
	X, Y int
}

Coordinate represents a point on a 2D plane.

type Fall

type Fall struct {
	DefaultInterval  time.Duration
	SoftDropInterval time.Duration
	IsSoftDrop       bool
}

func NewFall

func NewFall(level int) *Fall

func (*Fall) CalculateFallSpeeds

func (f *Fall) CalculateFallSpeeds(level int)

func (*Fall) ToggleSoftDrop

func (f *Fall) ToggleSoftDrop()

type Matrix

type Matrix [][]byte

Matrix represents the board of cells on which the game is played.

func DefaultMatrix

func DefaultMatrix() Matrix

func NewMatrix

func NewMatrix(height, width int) (Matrix, error)

NewMatrix creates a new Matrix with the given height and width.

func (*Matrix) AddTetrimino

func (m *Matrix) AddTetrimino(tet *Tetrimino) error

AddTetrimino adds the given Tetrimino to the Matrix. It returns an error if the Tetrimino is out of bounds or if the Tetrimino overlaps with an occupied mino.

func (*Matrix) DeepCopy

func (m *Matrix) DeepCopy() *Matrix

func (*Matrix) GetHeight

func (m *Matrix) GetHeight() int

GetHeight returns the height of the Matrix.

func (*Matrix) GetSkyline

func (m *Matrix) GetSkyline() int

GetSkyline returns the skyline; the highest row that the player can see.

func (*Matrix) GetVisible

func (m *Matrix) GetVisible() Matrix

GetVisible returns the Matrix without the buffer zone at the top (ie. the visible portion of the Matrix).

func (*Matrix) RemoveCompletedLines

func (m *Matrix) RemoveCompletedLines(tet *Tetrimino) Action

RemoveCompletedLines checks each row that the given Tetrimino occupies and removes any completed lines from the Matrix. It returns an Action to be used for calculating the score.

func (*Matrix) RemoveTetrimino

func (m *Matrix) RemoveTetrimino(tet *Tetrimino) error

RemoveTetrimino removes the given Tetrimino from the Matrix. It returns an error if the Tetrimino is out of bounds or if the Tetrimino is not found in the Matrix.

type NextQueue

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

NextQueue is a collection of up to 14 Tetriminos that are drawn from randomly. The queue is refilled when it has less than 7 Tetriminos.

func NewNextQueue

func NewNextQueue(startLine int) *NextQueue

NewNextQueue creates a new NextQueue of Tetriminos.

func (*NextQueue) GetElements

func (nq *NextQueue) GetElements() []Tetrimino

func (*NextQueue) Next

func (nq *NextQueue) Next() *Tetrimino

Next returns the next Tetrimino, removing it from the queue and refilling if necessary. This applies the skyline value (provided in NewNextQueue) to the Tetriminos Y axis.

type RotationCompass added in v0.1.2

type RotationCompass [4]RotationSet

A RotationCompass contains a RotationSet corresponding to each of the four compass directions in the order N, E, S, W. These compass directions represent the four rotations of a Tetrimino.

type RotationSet added in v0.1.2

type RotationSet []*Coordinate

A RotationSet contains coordinates to be used for a single rotation/compass direction. If the first coordinate cannot be used the next will be attempted. This continues until there are no more coordinates to fall back on (in which case rotation is not possible). This is part of the Super Rotation System (SRS).

type Scoring

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

func NewScoring

func NewScoring(
	level, maxLevel int,
	increaseLevel, endOnMaxLevel bool,
	maxLines int,
	endOnMaxLines bool) (*Scoring, error)

func (*Scoring) AddHardDrop

func (s *Scoring) AddHardDrop(lines int)

func (*Scoring) AddSoftDrop

func (s *Scoring) AddSoftDrop(lines int)

func (*Scoring) Level

func (s *Scoring) Level() int

func (*Scoring) Lines

func (s *Scoring) Lines() int

func (*Scoring) ProcessAction

func (s *Scoring) ProcessAction(a Action) (bool, error)

func (*Scoring) Total

func (s *Scoring) Total() int

type Tetrimino

type Tetrimino struct {
	// The value of the Tetrimino. This is the character that will be used to represent the Tetrimino in the matrix.
	Value byte
	Minos [][]bool   // A 2D slice of cells that make up the Tetrimino. True means the mino is occupied by a Mino.
	Pos   Coordinate // The top left mino of the Tetrimino. Used as a reference point for movement and rotation.

	CompassDirection int // The index of the current rotation in the RotationCompass (ie. North, South, East, West).
	RotationCompass  RotationCompass
}

A Tetrimino is a geometric Tetris shape formed by four Minos connected along their sides.

func GetEmptyTetrimino

func GetEmptyTetrimino() *Tetrimino

GetEmptyTetrimino returns a tetrimino with no minos or value. To be used for the starting (empty) hold.

func GetTetrimino

func GetTetrimino(value byte) (*Tetrimino, error)

GetTetrimino returns the Tetrmino with the given value. Valid values include: I, O, T, S, Z, J, L.

func GetValidTetriminos

func GetValidTetriminos() []Tetrimino

GetValidTetriminos returns a slice containing all seven valid Tetriminos (I, O, T, S, Z, J, L).

func (*Tetrimino) DeepCopy

func (t *Tetrimino) DeepCopy() *Tetrimino

func (*Tetrimino) IsAboveSkyline

func (t *Tetrimino) IsAboveSkyline(skyline int) bool

IsAboveSkyline returns true if the entire Tetrimino is above the skyline. This can be helpful when checking for Lock Out.

func (*Tetrimino) IsOverlapping

func (t *Tetrimino) IsOverlapping(matrix Matrix) bool

IsOverlapping checks whether the Tetrimino would be overlapping with an occupied Mino if it were on the Matrix. The Tetrmino should not yet be added to the Matrix, otherwise this will always return true as a Tetrimino is always overlapping with itself.

func (*Tetrimino) MoveDown

func (t *Tetrimino) MoveDown(matrix Matrix) bool

MoveDown moves the tetrimino down one row. This does not modify the matrix. If the tetrimino cannot move down, it will not be modified and false will be returned.

func (*Tetrimino) MoveLeft

func (t *Tetrimino) MoveLeft(matrix Matrix) bool

MoveLeft moves the tetrimino left one column. This does not modify the matrix. If the tetrimino cannot move left false will be returned.

func (*Tetrimino) MoveRight

func (t *Tetrimino) MoveRight(matrix Matrix) bool

MoveRight moves the tetrimino right one column. This does not modify the matrix. If the tetrimino cannot move right false will be returned.

func (*Tetrimino) Rotate

func (t *Tetrimino) Rotate(matrix Matrix, clockwise bool) error

Directories

Path Synopsis
modes

Jump to

Keyboard shortcuts

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