aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: fb8d91308c64cbf4b0ae0c8b2fc11cc2e883fde6 (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
package main

import (
	"fmt"
	"github.com/pkg/term"
)

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
}

func main() {
	var f Field
	fmt.Print("\033[2J") // Clear screen
	p := Point{}
	var oldp Point
	for {
		fmt.Print("\033[2J") // Clear screen
		f[oldp.Y][oldp.X] = false
		oldp = p
		f[p.Y][p.X] = true
		fmt.Print("\033[0;0H") // Position to 0,0
		fmt.Println(f.String())
		t, _ := term.Open("/dev/tty")
		term.RawMode(t)
		key := make([]byte, 3)
		t.Read(key)
		t.Restore()
		t.Close()
		switch key[0] {
		case 27: // Escape, read the arrow key pressed
			switch key[2] {
			case 65: // Up
				p.Y = (p.Y + 20 - 1)%20
			case 66: // Down
				p.Y = (p.Y + 20 + 1)%20
			case 67: // Right
				p.X = (p.X + 10 + 1)%10
			case 68: // Left
				p.X = (p.X + 10 - 1)%10
			default:
				fmt.Println("...escape, escape!")
				return
			}
		case 'q':
			fmt.Println("...that was exciting!")
			return
		default:
			if key[0] != 0 {
				fmt.Print(string(key[0]))
			}
		}
	}
}