Browse Source

Update textris draft

master
Trevor Slocum 3 years ago
parent
commit
48abd89324
  1. 83
      content/post/tetris-1.md

83
content/post/tetris-1.md

@ -23,53 +23,17 @@ Rocket Nine Labs is in no way affiliated with Tetris Holding, LLC. @@ -23,53 +23,17 @@ Rocket Nine Labs is in no way affiliated with Tetris Holding, LLC.
# Minos
Game pieces are called "Minos" because they are [polyominos](https://en.wikipedia.org/wiki/Polyomino).
Tetris is normally played using the seven one-sided [terominos](https://en.wikipedia.org/wiki/Tetromino):
I
████
O
██
██
T
███
J
███
L
███
S
██
██
Z
██
██
This tutorial focuses on the seven one-sided [terominos](https://en.wikipedia.org/wiki/Tetromino), where each piece has four blocks.
The number of blocks a mino has is also known as its rank.
| I | O | T | J | L | S | Z |
| --- | --- | --- | --- | --- | --- | --- |
| <pre><br>████</pre> | <pre>██<br>██</pre> | <pre><br>███</pre> | <pre><br>███</pre> | <pre><br>███</pre> | <pre> ██<br>██</pre> | <pre>██<br> ██</pre> |
## Data model
Tetris is played on an [X-Y grid](https://en.wikipedia.org/wiki/Cartesian_coordinate_system).
We will store Minos as slices of points (coordinates):
We will store Minos as slices of points:
```go
type Point struct {
@ -81,17 +45,30 @@ func (p Point) Rotate180() Point { return Point{-p.X, -p.Y} } @@ -81,17 +45,30 @@ func (p Point) Rotate180() Point { return Point{-p.X, -p.Y} }
func (p Point) Rotate270() Point { return Point{-p.Y, p.X} }
func (p Point) Reflect() Point { return Point{-p.X, p.Y} }
// Neighborhood returns the Von Neumann neighborhood of a point
func (p Point) Neighborhood() Mino {
return Mino{
{p.X - 1, p.Y},
{p.X, p.Y - 1},
{p.X + 1, p.Y},
{p.X, p.Y + 1}}
}
type Mino []Point
minoT := Mino{{0, 0}, {1, 0}, {2, 0}, {1, 1}}
var minoT = Mino{{0, 0}, {1, 0}, {2, 0}, {1, 1}}
```
## Generation
We could hard-code each piece into our game. Or, we could procedurally generate them, allowing us to play with any rank (size) of mino.
Instead of hard-coding each piece into our game, let's procedurally generate them.
This allows us to play with any size of mino.
### Comparing and sorting
To compare the minos more easily, we will use their string representation.
We sort the coordinates before printing so we can identify duplicate minos.
To compare minos more easily, we will use their string representation.
We will define a String method which sorts the coordinates before printing.
This allows us to compare duplicate minos just by checking their string values.
```go
func (m Mino) Len() int { return len(m) }
@ -123,7 +100,6 @@ func (m Mino) String() string { @@ -123,7 +100,6 @@ func (m Mino) String() string {
```
Origin returns a translated mino located at 0,0 and with positive coordinates only.
This is also to identify duplicate minos.
```go
func (m Mino) minCoords() (int, int) {
@ -155,8 +131,9 @@ func (m Mino) Origin() Mino { @@ -155,8 +131,9 @@ func (m Mino) Origin() Mino {
}
```
Another transformation is applied not only to identify duplicate minos, but also to retrieve their initial rotation, as [pieces spawn flat-side down](https://tetris.wiki/Super_Rotation_System).
The flattest side is calculated and a rotated mino is returned.
Another transformation is applied not only to help identify duplicate minos, but also to retrieve their initial rotation, as [pieces should spawn flat-side down](https://tetris.wiki/Super_Rotation_System).
The flattest side is calculated and a flattened mino is returned.
```go
func (m Mino) Flatten() Mino {
@ -211,7 +188,7 @@ func (m Mino) Flatten() Mino { @@ -211,7 +188,7 @@ func (m Mino) Flatten() Mino {
}
```
Variations returns all other rotations of a mino.
Variations returns the three other rotations of a mino.
```go
func (m Mino) Variations() []Mino {
@ -257,6 +234,8 @@ func (m Mino) Canonical() Mino { @@ -257,6 +234,8 @@ func (m Mino) Canonical() Mino {
}
```
### Generating new minos
WIP
```go
@ -349,7 +328,7 @@ To retrieve the contents of a point, we calculate its index by multiplying the Y @@ -349,7 +328,7 @@ To retrieve the contents of a point, we calculate its index by multiplying the Y
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
}

Loading…
Cancel
Save