aboutsummaryrefslogtreecommitdiff
path: root/tris/core.go
blob: 07b4b644cba212ed40803b797a7eb05113f06bf0 (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
package tris

type Point struct {
	X, Y int
}

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 (
	IPiece = Piece{0x0F00, 0x4444, 0x00F0, 0x2222}
	JPiece = Piece{0x1700, 0x6220, 0x0740, 0x2230}
	LPiece = Piece{0x4700, 0x2260, 0x0710, 0x3220}
	OPiece = Piece{0x6600, 0x6600, 0x6600, 0x6600}
	SPiece = Piece{0x6300, 0x2640, 0x0630, 0x1320}
	TPiece = Piece{0x2700, 0x2620, 0x0720, 0x2320}
	ZPiece = Piece{0x3600, 0x4620, 0x0360, 0x2310}
)

type Rotation int

const (
	Spawn Rotation = iota
	Clockwise
	Flip
	CounterClockwise
)

type Placement struct {
	piece Piece
	X     int
	Y     int
	Rot   Rotation
	Lock  int
}

type Bag []Piece

var LockDelay = 30

var ReferenceBag = Bag{IPiece, JPiece, LPiece, OPiece, SPiece, TPiece, ZPiece}

func (b Bag) Randomize() Bag { //TODO: implement randomizer
	return b
}

func (b *Bag) Pick() Placement {
	if len(*b) == 0 {
		b = &ReferenceBag.Randomize()
	}
	piece := Placement{piece: Bag[0], X: 3, Y: -2, Lock: LockDelay}
	*b = *b[1:]
}

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
}