tetris

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: GPL-3.0 Imports: 9 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 maps each Tetrimino type to its rotation behavior definition. The offsets are used to adjust piece position during rotation to implement kicks according to the Super Rotation System (SRS).

When rotating clockwise (right), the offsets are applied in order. When rotating counter-clockwise (left), the offsets are applied in reverse order. As an example, when rotating the I Tetrimino clockwise from North to East (clockwise) the first offset added to its position is (2, -1). If this position is not valid, the next offset (0, -1) is tried, and so on.

Functions

func WithRandSource

func WithRandSource(rand *rand.Rand) func(*NextQueue)

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) GetLinesCleared

func (a Action) GetLinesCleared() int

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 position in the game matrix using X (horizontal) and Y (vertical) coordinates. The origin (0,0) is the top-left of a matrix (within any buffer zone that exists), with positive X going right and positive Y going down.

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

DefaultMatrix creates a new Matrix with a height of 40 and a width of 10.

func NewMatrix

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

NewMatrix creates a new Matrix with the given height and width. It returns an error if the height is less than 20 to allow for a buffer zone of 20 lines.

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(skyline int, opts ...func(*NextQueue)) *NextQueue

NewNextQueue creates a new NextQueue of Tetriminos.

func (*NextQueue) GetElements

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

GetElements returns the Tetriminos in the queue.

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

type RotationCompass [4]RotationOffsets

RotationCompass defines rotation offset data for all four possible orientations of a Tetrimino (North, East, South, West). This implements the Super Rotation System (SRS) which allows pieces to "kick" off walls and other blocks when rotating.

type RotationOffsets

type RotationOffsets []*Coordinate

RotationOffsets contains a sequence of offset coordinates to try when rotating a Tetrimino. When rotating, these offsets are tried in order until a valid position is found. If no valid position is found using any offset, the rotation fails. This is a key component of the Super Rotation System (SRS).

type Scoring

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

Scoring is a scoring system for Tetris. It keeps track of the current level, total score, and lines cleared. It also has options to increase the level, end the game on max level, and end the game on max lines.

func NewScoring

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

NewScoring creates a new scoring system.

func (*Scoring) AddHardDrop

func (s *Scoring) AddHardDrop(lines int)

AddHardDrop adds points for a hard drop.

func (*Scoring) AddSoftDrop

func (s *Scoring) AddSoftDrop(lines int)

AddSoftDrop adds points for a soft drop.

func (*Scoring) LastClearedLines

func (s *Scoring) LastClearedLines() int

LastClearedLines returns the number of lines cleared in the most recent action.

func (*Scoring) Level

func (s *Scoring) Level() int

Level returns the current level.

func (*Scoring) Lines

func (s *Scoring) Lines() int

Lines returns the total lines cleared.

func (*Scoring) ProcessAction

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

func (*Scoring) Total

func (s *Scoring) Total() int

Total returns the total score.

type Tetrimino

type Tetrimino struct {
	// Value is the character identifier for the Tetrimino (I, O, T, S, Z, J, L).
	// This is used internally and may differ from the display representation.
	Value byte

	// Cells represents the shape of the Tetrimino as a 2D grid.
	// True indicates a Mino is present in the cell, false indicates empty space.
	Cells [][]bool

	// Position tracks the coordinate of the Tetrimino's top-left corner in the game matrix.
	// This serves as the reference point for all movement and rotation operations.
	Position Coordinate

	// CompassDirection tracks the current rotation state (0-3, representing North, East, South, West).
	CompassDirection int

	// RotationCompass defines the piece's rotation behavior according to the Super Rotation System (SRS).
	// It contains offset data for each rotation state to handle various kicks.
	RotationCompass RotationCompass
}

A Tetrimino is a geometric Tetris piece formed by connecting four square blocks (Minos) along their edges. Each Tetrimino has a unique shape, position, rotation state, and rotation behavior defined by the Super Rotation System (SRS).

func GetEmptyTetrimino

func GetEmptyTetrimino() *Tetrimino

GetEmptyTetrimino returns a tetrimino 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 are: 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). The Tetrminos are sorted by their value to ensure determinism in tests.

func (*Tetrimino) DeepCopy

func (t *Tetrimino) DeepCopy() *Tetrimino

DeepCopy creates a deep copy of the Tetrimino. This is useful when you need to modify a Tetrimino without affecting the original.

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) IsValid

func (t *Tetrimino) IsValid(matrix Matrix, checkBounds bool) bool

IsValid returns true if the given Tetrimino is within the bounds of the matrix and does not overlap with any occupied cells. If checkBounds is false, only overlap is checked. The Tetrimino being checked should not be in the Matrix yet.

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

Rotate rotates the Tetrimino clockwise or counter-clockwise. This does not modify the matrix. This will automatically use Super Rotation System (SRS). If no valid rotation is found, the Tetrimino will not be modified and an error will be returned.

Directories

Path Synopsis
modes

Jump to

Keyboard shortcuts

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