aboutsummaryrefslogtreecommitdiff
path: root/tris/core.go
blob: 089dca4f69c65396dc96b23e2d3b0c9ba66951b8 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package tris

import (
	"math/rand"
	"time"
)

type Point struct {
	X, Y int
}

// Add adds two points together
func (p Point) Add(q Point) Point {
	return Point{X: p.X+q.X, Y: p.Y+q.Y}
}

// A piece contains a value per rotation
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 (
	EmptyPiece = Piece{}
	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
)

// a placement is the combination of a piece and where it exactly is on the board
// we use this both for the actual piece and for checking hypothetical positions
type Placement struct {
	piece Piece
	X     int
	Y     int
	Rot   Rotation
	Lock  time.Time
}

// Collide checks if any squares of the Placement and the Field overlap
func (p Placement) Collide(f Field) bool {
	pf, ok := p.Field(f.H(), f.W())
	for x := 0; x < f.W(); x++ {
		for y := 0; y < f.H(); y++ {
			if f[y][x] > 0 && pf[y][x] > 0 {
				return true
			}
		}
	}
	return !ok
}

// Field translates the Placement into a Field format
// When it gets out of the board, we return that to the caller with a bool
func (p Placement) Field(h, w int) (Field, bool) {
	f := NewField(h, w)
	ok := true
	for _, point := range p.Points() {
		if point.X < 0 || point.X >= f.W() || point.Y >= f.H() {
			ok = false
			continue
		}
		if point.Y < 0 { // above the playing field we count the piece as on the board
			continue
		}
		f[point.Y][point.X] = p.Color()
	}
	return f, ok
}

func (p Placement) Color() int {
	switch p.piece {
	case IPiece:
		return 36
	case JPiece:
		return 34
	case LPiece:
		return 37
	case OPiece:
		return 33
	case SPiece:
		return 32
	case TPiece:
		return 35
	case ZPiece:
		return 31
	}
	return 0
}

// Points transforms the piece to a list of coordinates
func (p Placement) Points() []Point {
	piece := p.piece[p.Rot]
	var points []Point
	x := []int{3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0}
	y := []int{0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3}
	for bm, i := uint16(0x8000), 0; i < 16; bm, i = bm >> 1, i + 1 {
		if piece&bm == bm {
			points = append(points, Point{X: p.X + x[i], Y: p.Y + y[i]})
		}
	}
	return points
}

type Bag []Piece

var LockDelay = time.Second/2

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

func NewBag() Bag {
	b := ReferenceBag.Randomize()
	return b
}

func (b Bag) Randomize() Bag {
	rand.Shuffle(len(b), func(i, j int) { b[i], b[j] = b[j], b[i] })
	return b
}

func (b Bag) Pick() (Bag, Placement) {
	Swapped = false
	if len(b) == 0 {
		b = NewBag()
	}
	piece := Placement{piece: b[0], X: 3, Y: -2, Lock: time.Now()}
	b = b[1:]
	return b, piece
}

type Field [][]int

func NewField(h, w int) Field {
	f := make(Field, h)
	for i := range f {
		f[i] = make([]int, w)
	}
	return f
}

func (f Field) H() int {
	return len(f)
}

func (f Field) W() int {
	if f.H() == 0 {
		return 0
	}
	return len(f[0])
}

// Add merges the Field and Piece together
func (f Field) Add(p Placement) Field {
	fn := NewField(f.H(), f.W())
	for i, row := range fn {
		for j := range row {
			fn[i][j] = f[i][j]
		}
	}
	for _, point := range p.Points() {
		if point.Y < 0 || point.Y >= f.H() || point.X < 0 || point.X >= f.W() {
			continue
		}
		fn[point.Y][point.X] = p.Color()
	}
	return fn

}

func (f Field) String() (output string) {
	for _, row := range f {
		for _, block := range row {
			if block != 0 {
				output += "\u2588\u2589"
			} else {
				output += "\u2591\u2591"
			}
		}
		output += "|\n"
	}
	return output
}