Add code repository link to Tetris tutorials

This commit is contained in:
Trevor Slocum 2020-08-06 21:37:33 -07:00
parent f301c69ca3
commit 0ad0bf1598
2 changed files with 98 additions and 9 deletions

View File

@ -9,6 +9,13 @@ aliases:
This is the first part of a series of tutorials on creating a [terminal-based](https://en.wikipedia.org/wiki/Text-based_user_interface)
[Tetris](https://en.wikipedia.org/wiki/Tetris) clone with [Go](https://golang.org).
Code for this tutorial is available [here](https://gitlab.com/tslocum/terminal-tetris-tutorial/-/tree/master/part-1).
```bash
go get gitlab.com/tslocum/terminal-tetris-tutorial/part-1 # Download and install
~/go/bin/part-1 # Run
```
For a complete implementation of a Tetris clone in Go, see [netris](https://gitlab.com/tslocum/netris).
# Disclaimer
@ -38,7 +45,7 @@ Tetris is played on an [X-Y grid](https://en.wikipedia.org/wiki/Cartesian_coordi
so we will store minos as slices of points.
```
<! !>
<! !>
<! !>
<! 1,7 !>
<! !>
@ -47,7 +54,7 @@ so we will store minos as slices of points.
<! !>
<! 5,2 !>
<! !>
<!0,0 !>
<! 2,0 !>
<!==========!>
\/\/\/\/\/
```
@ -108,6 +115,35 @@ func (m Mino) String() string {
return b.String()
}
func (m Mino) Render() string {
var (
w, h = m.Size()
c = Point{0, h - 1}
b strings.Builder
)
for y := h - 1; y >= 0; y-- {
c.X = 0
c.Y = y
for x := 0; x < w; x++ {
if !m.HasPoint(Point{x, y}) {
continue
}
for i := x - c.X; i > 0; i-- {
b.WriteRune(' ')
}
b.WriteRune('X')
c.X = x + 1
}
b.WriteRune('\n')
}
return b.String()
}
{{< / highlight >}}
Origin returns a translated mino located at `0,0` and with positive coordinates
@ -169,6 +205,20 @@ XXX X
Flatten calculates the flattest side of a mino and returns a flattened mino.
{{< highlight go >}}
func (m Mino) Size() (int, int) {
var x, y int
for _, p := range m {
if p.X > x {
x = p.X
}
if p.Y > y {
y = p.Y
}
}
return x + 1, y + 1
}
func (m Mino) Flatten() Mino {
var (
w, h = m.Size()
@ -351,6 +401,10 @@ We generate minos for the rank below the requested rank and iterate over the
variations of each mino, saving and returning all unique variations.
{{< highlight go >}}
func monomino() Mino {
return Mino{{0, 0}}
}
func Generate(rank int) ([]Mino, error) {
switch {
case rank < 0:
@ -385,10 +439,6 @@ func Generate(rank int) ([]Mino, error) {
return minos, nil
}
}
func monomino() Mino {
return Mino{{0, 0}}
}
{{< / highlight >}}
# Stay tuned...

View File

@ -9,6 +9,13 @@ aliases:
This is the second part of a series of tutorials on creating a [terminal-based](https://en.wikipedia.org/wiki/Text-based_user_interface) [Tetris](https://en.wikipedia.org/wiki/Tetris) clone with [Go](https://golang.org).
Code for this tutorial is available [here](https://gitlab.com/tslocum/terminal-tetris-tutorial/-/tree/master/part-2).
```bash
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](https://gitlab.com/tslocum/netris).
# Disclaimer
@ -19,11 +26,43 @@ Rocket Nine Labs is in no way affiliated with Tetris Holding, LLC.
## Matrix
The matrix is typically 10 blocks wide and 20 blocks high.
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 coordinate on the matrix.
A block is an integer representing the contents of a single X-Y Point (see part 1) on the matrix.
We will use these blocks later to build tetrominos.
{{< highlight go >}}
type Block int
@ -49,7 +88,7 @@ The buffer is additional space above the visible playfield.
{{< highlight go >}}
type Matrix struct {
W int // Width
H in // Height
H int // Height
B int // Buffer height
M []Block // Contents