aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoop Kiefte <ikojba@gmail.com>2020-09-20 10:25:03 +0200
committerJoop Kiefte <ikojba@gmail.com>2020-09-20 10:25:03 +0200
commitc3da6b22c767d79970d98df181bf5eb89bf55b93 (patch)
tree9c1f0324c1f307a2a092c8f9d7b5c5796a431b64
parentde36bfd4aecb683c128fb4294de545317fc0edab (diff)
Add hold boxv1.0.0
-rw-r--r--main.go14
-rw-r--r--tris/core.go2
-rw-r--r--tris/hold.go23
3 files changed, 34 insertions, 5 deletions
diff --git a/main.go b/main.go
index 478d8f4..a07ca96 100644
--- a/main.go
+++ b/main.go
@@ -89,13 +89,15 @@ func main() {
sscore := fmt.Sprintf("score %d", score)
slines := fmt.Sprintf("lines %d", linescleared)
if !harddrop {
- fpos(0, 0, f.Add(p))
+ ppos(0, 0, "Hold (c)")
+ npos(3, 0, tris.HoldBox)
+ fpos(0, 10, f.Add(p))
var next tris.Field
b, next = b.Next(5)
- npos(0, 24, next)
- ppos(1, 32, sscore)
- ppos(3, 32, slevel)
- ppos(5, 32, slines)
+ npos(0, 34, next)
+ ppos(1, 42, sscore)
+ ppos(3, 42, slevel)
+ ppos(5, 42, slines)
key = GetKey(t)
}
switch key[0] {
@@ -119,6 +121,8 @@ func main() {
rot = (p.Rot + 1) % 4
case 'z':
rot = (p.Rot + 3) % 4
+ case 'c':
+ b, p = b.Swap(p)
case ' ':
if !harddrop {
dropfrom = p.Y
diff --git a/tris/core.go b/tris/core.go
index 6963294..089dca4 100644
--- a/tris/core.go
+++ b/tris/core.go
@@ -20,6 +20,7 @@ type Piece [4]uint16
// A table that represents each piece from https://tetris.fandom.com/wiki/SRS
// in binary starting with 1 top left going per row, here in hexadecimal shorthand.
var (
+ EmptyPiece = Piece{}
IPiece = Piece{0x0F00, 0x4444, 0x00F0, 0x2222}
JPiece = Piece{0x1700, 0x6220, 0x0740, 0x2230}
LPiece = Piece{0x4700, 0x2260, 0x0710, 0x3220}
@@ -130,6 +131,7 @@ func (b Bag) Randomize() Bag {
}
func (b Bag) Pick() (Bag, Placement) {
+ Swapped = false
if len(b) == 0 {
b = NewBag()
}
diff --git a/tris/hold.go b/tris/hold.go
new file mode 100644
index 0000000..6a82f5b
--- /dev/null
+++ b/tris/hold.go
@@ -0,0 +1,23 @@
+package tris
+
+var HoldPiece Piece
+
+var HoldBox Field
+
+var Swapped bool
+
+func (b Bag) Swap(p Placement) (Bag, Placement) {
+ if Swapped {
+ return b, p
+ }
+ var tempp Placement
+ if HoldPiece == EmptyPiece {
+ b, tempp = b.Pick()
+ HoldPiece = tempp.piece
+ }
+ p.piece, HoldPiece = HoldPiece, p.piece
+ Swapped = true
+ HoldBox, _ = Placement{piece: HoldPiece, X: 0, Y:0}.Field(2,4)
+ p.Rot, p.X, p.Y = 0, 3, -2
+ return b, p
+}