Documentation
¶
Index ¶
- Variables
- func WithRandSource(rand *rand.Rand) func(*NextQueue)
- type Action
- type ActionContainer
- type Coordinate
- type Fall
- type Matrix
- func (m *Matrix) AddTetrimino(tet *Tetrimino) error
- func (m *Matrix) DeepCopy() *Matrix
- func (m *Matrix) GetHeight() int
- func (m *Matrix) GetSkyline() int
- func (m *Matrix) GetVisible() Matrix
- func (m *Matrix) RemoveCompletedLines(tet *Tetrimino) Action
- func (m *Matrix) RemoveTetrimino(tet *Tetrimino) error
- type NextQueue
- type RotationCompass
- type RotationOffsets
- type Scoring
- type Tetrimino
- func (t *Tetrimino) DeepCopy() *Tetrimino
- func (t *Tetrimino) IsAboveSkyline(skyline int) bool
- func (t *Tetrimino) IsValid(matrix Matrix, checkBounds bool) bool
- func (t *Tetrimino) MoveDown(matrix Matrix) bool
- func (t *Tetrimino) MoveLeft(matrix Matrix) bool
- func (t *Tetrimino) MoveRight(matrix Matrix) bool
- func (t *Tetrimino) Rotate(matrix Matrix, clockwise bool) error
Constants ¶
This section is empty.
Variables ¶
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.
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 ¶
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 (Action) GetLinesCleared ¶
func (a Action) GetLinesCleared() int
func (Action) StartsBackToBack ¶
type ActionContainer ¶
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 ¶
func (*Fall) CalculateFallSpeeds ¶
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 ¶
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 ¶
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) GetSkyline ¶
GetSkyline returns the skyline; the highest row that the player can see.
func (*Matrix) GetVisible ¶
GetVisible returns the Matrix without the buffer zone at the top (ie. the visible portion of the Matrix).
func (*Matrix) RemoveCompletedLines ¶
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 ¶
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 ¶
NewNextQueue creates a new NextQueue of Tetriminos.
func (*NextQueue) GetElements ¶
GetElements returns the Tetriminos in the queue.
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 ¶
AddHardDrop adds points for a hard drop.
func (*Scoring) AddSoftDrop ¶
AddSoftDrop adds points for a soft drop.
func (*Scoring) LastClearedLines ¶
LastClearedLines returns the number of lines cleared in the most recent action.
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 ¶
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 ¶
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 ¶
IsAboveSkyline returns true if the entire Tetrimino is above the skyline. This can be helpful when checking for Lock Out.
func (*Tetrimino) IsValid ¶
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 ¶
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 ¶
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 ¶
MoveRight moves the tetrimino right one column. This does not modify the matrix. If the tetrimino cannot move right false will be returned.