aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoop Kiefte <ikojba@gmail.com>2020-09-12 09:05:28 +0200
committerJoop Kiefte <ikojba@gmail.com>2020-09-12 09:05:28 +0200
commit3edb4c1779ae23dd976c067622f2aaee9ac98f5d (patch)
tree9289f2b2a674ab11bb6eed6dbead64810d567497
parentac3196415d5d87a745d339f1548573b0b5745c41 (diff)
Mostly playable now -- kicks still missing and problem with floor detection L piece
-rw-r--r--main.go19
-rw-r--r--tris/lines.go27
-rw-r--r--tris/move.go12
3 files changed, 50 insertions, 8 deletions
diff --git a/main.go b/main.go
index 19a170e..bd3ecea 100644
--- a/main.go
+++ b/main.go
@@ -22,18 +22,19 @@ func GetKey() []byte {
func main() {
var f tris.Field
- var floor, topout bool
+ var floor, topout, harddrop bool
var x, y int
var rot tris.Rotation
fmt.Print("\033[2J") // Clear screen
b := tris.NewBag()
b, p := b.Pick()
- t := time.Tick(time.Second / 60)
+ t := time.Tick(time.Second)
for {
x, y, rot = p.X, p.Y, p.Rot
fmt.Print("\033[0;0H") // Position to 0,0
fmt.Println(f.Add(p).String())
key := GetKey()
+ if harddrop { key[0] = ' ' }
switch key[0] {
case 27: // Escape, read the arrow key pressed
switch key[2] {
@@ -54,7 +55,7 @@ func main() {
case 'z':
rot = (p.Rot + 3)%4
case ' ':
- y = 20
+ harddrop = true
case 'q':
fmt.Println("...that was exciting!")
return
@@ -66,11 +67,21 @@ func main() {
case <-t:
y = p.Y + 1
default:
+ if harddrop {
+ y = p.Y + 1
+ }
}
p, floor, topout = p.Move(f, rot, x, y)
if floor && p.Lock.Add(tris.LockDelay).Before(time.Now()) {
- fmt.Print("\033[2J") // Clear screen
+ harddrop = false
+ var l int
f = f.Add(p)
+ l, f = f.Lines()
+ if l > 0 {
+ fmt.Println(l, " lines removed!")
+ time.Sleep(time.Second)
+ }
+ fmt.Print("\033[2J") // Clear screen
b, p = b.Pick()
}
if !floor {
diff --git a/tris/lines.go b/tris/lines.go
new file mode 100644
index 0000000..5cb6981
--- /dev/null
+++ b/tris/lines.go
@@ -0,0 +1,27 @@
+package tris
+
+func (f Field) Lines() (int, Field) {
+ full := [10]bool{true, true, true, true, true, true, true, true, true, true}
+ empty := [10]bool{false, false, false, false, false, false, false, false, false, false}
+ var n int
+ var nf [][10]bool
+
+ // count and collect
+ for _, line := range f {
+ if line == full {
+ n++
+ } else {
+ nf = append(nf, line)
+ }
+ }
+
+ // compress
+ for i := 0; i < 20; i++ {
+ if i < n {
+ f[i] = empty
+ } else {
+ f[i] = nf[i-n]
+ }
+ }
+ return n, f
+}
diff --git a/tris/move.go b/tris/move.go
index 0e081ef..29140b8 100644
--- a/tris/move.go
+++ b/tris/move.go
@@ -1,5 +1,11 @@
package tris
+func (p Placement) Floor(f Field) bool {
+ fp := p
+ fp.Y++
+ return fp.Collide(f)
+}
+
func (p Placement) Move(f Field, rot Rotation, x, y int) (np Placement, floor, topout bool) {
np = p
np.Rot = rot
@@ -9,10 +15,8 @@ func (p Placement) Move(f Field, rot Rotation, x, y int) (np Placement, floor, t
return np, false, false
}
np = p // last resort reset p
- fp := np
- fp.Y++
- if fp.Collide(f) && np.Y < 0 {
+ if np.Floor(f) && np.Y < 0 {
return np, true, true
}
- return np, fp.Collide(f), false
+ return np, np.Floor(f), false
}