aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go51
-rw-r--r--tris/core.go12
-rw-r--r--tris/move.go18
3 files changed, 53 insertions, 28 deletions
diff --git a/main.go b/main.go
index a705b00..19a170e 100644
--- a/main.go
+++ b/main.go
@@ -22,43 +22,39 @@ func GetKey() []byte {
func main() {
var f tris.Field
+ var floor, topout 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)
- ds := time.NewTicker(time.Second)
for {
-// fmt.Print("\033[2J") // Clear screen
+ x, y, rot = p.X, p.Y, p.Rot
fmt.Print("\033[0;0H") // Position to 0,0
fmt.Println(f.Add(p).String())
- select {
- case <-ds.C:
- np, ok := p.Drop(f)
- if ok {
- p = np
- p.Lock = time.Now()
- }
- if time.Now().Sub(p.Lock) > tris.LockDelay {
- f = f.Add(p)
- b, p = b.Pick()
- }
- case <-t:
- }
key := GetKey()
switch key[0] {
case 27: // Escape, read the arrow key pressed
switch key[2] {
case 65: // Up
- p.Rot = (p.Rot + 1)%4
+ rot = (p.Rot + 1)%4
case 66: // Down
+ y = p.Y + 1
case 67: // Right
- p.X++
+ x = p.X + 1
case 68: // Left
- p.X--
+ x = p.X - 1
default:
fmt.Println("...escape, escape!")
return
}
+ case 'x':
+ rot = (p.Rot + 1)%4
+ case 'z':
+ rot = (p.Rot + 3)%4
+ case ' ':
+ y = 20
case 'q':
fmt.Println("...that was exciting!")
return
@@ -66,5 +62,24 @@ func main() {
fmt.Println("...never let an engineer pick the name of your software?")
return
}
+ select {
+ case <-t:
+ y = p.Y + 1
+ default:
+ }
+ 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
+ f = f.Add(p)
+ b, p = b.Pick()
+ }
+ if !floor {
+ p.Lock = time.Now()
+ }
+
+ if topout {
+ fmt.Println("GAME OVER")
+ return
+ }
}
}
diff --git a/tris/core.go b/tris/core.go
index af46311..0b5afff 100644
--- a/tris/core.go
+++ b/tris/core.go
@@ -44,19 +44,10 @@ type Placement struct {
Lock time.Time
}
-func (p Placement) Drop(f Field) (Placement, bool) {
- newp := p
- newp.Y++
- if !newp.Collide(f) {
- return newp, true
- }
- return p, false
-}
-
func (p Placement) Collide(f Field) bool { //TODO: Fix first piece collision bug
pf, ok := p.Field()
for x := 0; x < 9; x++ {
- for y := 0; y < 19; y++ {
+ for y := 0; y < 20; y++ {
if f[y][x] && pf[y][x] {
return true
}
@@ -130,6 +121,7 @@ func (f Field) Add(p Placement) Field {
fn[point.Y][point.X] = true
}
return fn
+
}
func (f Field) String() (output string) {
diff --git a/tris/move.go b/tris/move.go
new file mode 100644
index 0000000..0e081ef
--- /dev/null
+++ b/tris/move.go
@@ -0,0 +1,18 @@
+package tris
+
+func (p Placement) Move(f Field, rot Rotation, x, y int) (np Placement, floor, topout bool) {
+ np = p
+ np.Rot = rot
+ np.X = x
+ np.Y = y
+ if !np.Collide(f) { // free air
+ return np, false, false
+ }
+ np = p // last resort reset p
+ fp := np
+ fp.Y++
+ if fp.Collide(f) && np.Y < 0 {
+ return np, true, true
+ }
+ return np, fp.Collide(f), false
+}