aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 6bc8ce01494b3b57efb4c172a883c16c0f06d9da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main

import (
	"fmt"
	"git.kiefte.eu/lapingvino/clitris/tris"
	"github.com/pkg/term"
	"time"
	"math/rand"
)

// 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 {
	t, _ := term.Open("/dev/tty")
	term.RawMode(t)
	key := make([]byte, 3)
	t.SetReadTimeout(time.Second / 1000)
	t.Read(key)
	t.Restore()
	t.Close()
	return key
}

func main() {
	rand.Seed(time.Now().UnixNano()) // to start with truly random pieces
	var f tris.Field // the playing field (10x20)
	var floor, topout, harddrop bool // state machine variables to check special situations

	// define outside of game loop to avoid accidental resets of position
	// x, y and rot are the values calculated to feed to Move
	// which then checks collisions and does the wall kicks
	var x, y int
	var rot tris.Rotation

	// init variable for pressed key - MUST be initialized 3 long to avoid crash, GetKey() does this
	key := GetKey()

	fmt.Print("\033[2J") // Clear screen
	b := tris.NewBag()
	b, p := b.Pick()
	t := time.Tick(time.Second)
	for {
		x, y, rot = p.X, p.Y, p.Rot
		if !harddrop {
			fmt.Print("\033[0;0H") // Position to 0,0
			fmt.Println(f.Add(p).String())
			key = GetKey()
		}
		switch key[0] {
		case 27: // Escape, read the arrow key pressed
			switch key[2] {
			case 65: // Up
				rot = (p.Rot + 1)%4
			case 66: // Down
				y = p.Y + 1
			case 67: // Right
				x = p.X + 1
			case 68: // Left
				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 ' ':
			harddrop = true
		case 'q':
			fmt.Println("...that was exciting!")
			return
		case 'Q':
			fmt.Println("...never let an engineer pick the name of your software?")
			return
		}
		select {
		case <-t:
			y = p.Y + 1
		default:
			if harddrop {
				y = p.Y + 1
			}
		}
		p, floor, topout = p.Move(f, rot, x, y) // Check if the piece can move, then do it and communicate back for housekeeping

		// Give some time before actually locking in to enable tucks
		// This code runs when the time is over
		if floor && p.Lock.Add(tris.LockDelay).Before(time.Now()) {
			harddrop = false
			var l int
			f = f.Add(p)
			l, f = f.Lines() // count and remove full lines
			if l > 0 {
				fmt.Println(l, "lines removed!")
				time.Sleep(time.Second)
			}
			fmt.Print("\033[2J") // Clear screen
			b, p = b.Pick() // ... and pick a new piece from the bag
		}
		// when not touching a piece or floor below yet, reset lock delay
		if !floor {
			p.Lock = time.Now()
		}

		if topout {
			fmt.Println("GAME OVER")
			return
		}
	}
}