diff options
author | Joop Kiefte <ikojba@gmail.com> | 2020-08-31 16:50:22 +0200 |
---|---|---|
committer | Joop Kiefte <ikojba@gmail.com> | 2020-08-31 16:50:22 +0200 |
commit | ee0d9e5d4453032b1cd81ec6636f08ee2dac1e3e (patch) | |
tree | 9c808b1124086c640a600a117f91220d58baa2ca | |
parent | 7bb7917c843f3d381f4406ef52da346518089f2f (diff) |
Refactor datastructures and game logic to tris library for generic usagev0.0.1
-rw-r--r-- | main.go | 62 | ||||
-rw-r--r-- | tris/core.go | 57 |
2 files changed, 60 insertions, 59 deletions
@@ -3,64 +3,9 @@ package main import ( "fmt" "github.com/pkg/term" + "git.kiefte.eu/lapingvino/clitris/tris" ) -type PieceType int - -const ( - IPiece PieceType = iota - JPiece - LPiece - SPiece - ZPiece - TPiece - OPiece -) - -type Point struct { - X, Y int -} - -type Piece struct { - Type PieceType - RelX int - RelY int - Layout []Point - Lock int -} - -type Bag []Piece - -type Field [20][10]bool - -func (f Field) String() (output string) { - var toprow [10]bool - var top bool - for _, row := range f { - top = !top - for i, block := range row { - if top { - toprow[i] = block - continue - } - switch { - case toprow[i] && block: - output += "\u2588" - case toprow[i] && !block: - output += "\u2580" - case !toprow[i] && block: - output += "\u2584" - default: - output += " " - } - } - if !top { - output += "\n" - } - } - return output -} - // GetKey() returns the key currently pressed. It always returns a 3 byte slice. Check first element for Escape for handling arrow keys // Because a defer would trigger too late and the Restore and Close are essential, separated in a function. func GetKey() []byte { @@ -74,10 +19,9 @@ func GetKey() []byte { } func main() { - var f Field + var f tris.Field fmt.Print("\033[2J") // Clear screen - p := Point{} - var oldp Point + var p, oldp tris.Point for { fmt.Print("\033[2J") // Clear screen f[oldp.Y][oldp.X] = false diff --git a/tris/core.go b/tris/core.go new file mode 100644 index 0000000..41d1beb --- /dev/null +++ b/tris/core.go @@ -0,0 +1,57 @@ +package tris + +type PieceType int + +const ( + IPiece PieceType = iota + JPiece + LPiece + SPiece + ZPiece + TPiece + OPiece +) + +type Point struct { + X, Y int +} + +type Piece struct { + Type PieceType + RelX int + RelY int + Layout []Point + Lock int +} + +type Bag []Piece + +type Field [20][10]bool + +func (f Field) String() (output string) { + var toprow [10]bool + var top bool + for _, row := range f { + top = !top + for i, block := range row { + if top { + toprow[i] = block + continue + } + switch { + case toprow[i] && block: + output += "\u2588" + case toprow[i] && !block: + output += "\u2580" + case !toprow[i] && block: + output += "\u2584" + default: + output += " " + } + } + if !top { + output += "\n" + } + } + return output +} |