rocket9labs.com/content/post/tetris-2.md

3.4 KiB

title categories draft aliases
Terminal-based Tetris - Part 2: The matrix
tutorial
true
/post/textris-2/

This is the second part of a series of tutorials on creating a terminal-based Tetris clone with Go.

The code for this tutorial is available on GitLab.

go get gitlab.com/tslocum/terminal-tetris-tutorial/part-2 # Download and install
~/go/bin/part-2 # Run

For a complete implementation of a Tetris clone in Go, see netris.

Disclaimer

Tetris is a registered trademark of the Tetris Holding, LLC.

Rocket Nine Labs is in no way affiliated with Tetris Holding, LLC.

Matrix

In this part of the series, we will learn what the matrix is and how we could implement it in Go.

The matrix is a playfield which holds tetrominos. It is typically 10 blocks wide and 20 blocks high.

Initially empty, tetrominos will appear just outside of view at the top of the matrix, and will fall into place one by one.

<!          !>      <!          !>
<!          !>      <!          !>
<!          !>      <!          !>
<!          !>      <!          !>
<!   3,15   !>      <!   X      !>
<!          !>      <!          !>
<!          !>      <!          !>
<!          !>      <!          !>
<!          !>      <!          !>
<!          !>      <!          !>
<!          !>      <!          !>
<!          !>      <!          !>
<! 1,7      !>      <! X        !>
<!          !>      <!          !>
<!    4,5   !>      <!    X     !>
<!          !>      <!          !>
<!          !>      <!          !>
<!     5,2  !>      <!     X    !>
<!          !>      <!          !>
<!  2,0     !>      <!  X       !>
<!==========!>      <!==========!>
  \/\/\/\/\/          \/\/\/\/\/

Example coordinate positions and blocks in 10x20 playfield

Matrix data model

A block is an integer representing the contents of a single X-Y Point (see part 1) on the matrix. We will use four of these blocks to build each tetromino.

{{< highlight go >}} type Block int

const ( BlockNone Block = iota BlockSolidBlue BlockSolidCyan BlockSolidRed BlockSolidYellow BlockSolidMagenta BlockSolidGreen BlockSolidOrange ) {{< / highlight >}}

The matrix will be stored as a slice of blocks. The zero-value of Block is a blank space.

The matrix has a width, height and buffer height. The buffer is additional space above the visible playfield.

{{< highlight go >}} type Matrix struct { W int // Width H int // Height B int // Buffer height

M []Block // Contents

}

func NewMatrix(w int, h int, b int) Matrix { m := Matrix{ W: w, H: h, B: b, M: make([]Block, w(h+b)), }

return &m

} {{< / highlight >}}

To retrieve the contents of a point, we calculate its index by multiplying the Y coordinate with the matrix width and adding the X coordinate.

{{< highlight go >}} func I(x int, y int, w int) int { if x < 0 || x >= w || y < 0 { log.Panicf("failed to retrieve index for %d,%d width %d: invalid coordinates", x, y, w) }

return (y * w) + x

}

func (m *Matrix) Block(x int, y int) Block { if y >= m.H+m.B { log.Panicf("failed to retrieve block at %d,%d: invalid y coordinate", x, y) }

index := I(x, y, m.W)
return m.M[index]

} {{< / highlight >}}