aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoop Kiefte <ikojba@gmail.com>2020-09-06 23:52:02 +0200
committerJoop Kiefte <ikojba@gmail.com>2020-09-06 23:52:02 +0200
commite2bf8762f96dfdfef05ac22d9b2bdfa1ab926c59 (patch)
tree7234686c47d99a73c5dd6b42142105e5ca78961e
parentb949c46efbdeb5c76177f643d3082d69987e656e (diff)
Working field maths and collision
-rw-r--r--tris/core.go74
1 files changed, 71 insertions, 3 deletions
diff --git a/tris/core.go b/tris/core.go
index 07b4b64..3121adf 100644
--- a/tris/core.go
+++ b/tris/core.go
@@ -35,26 +35,94 @@ type Placement struct {
Lock int
}
+func (p *Placement) Drop(f Field) bool {
+ newp := p
+ newp.Y++
+ if !newp.Collide(f) {
+ p = newp
+ return true
+ }
+ return false
+}
+
+func (p Placement) Collide(f Field) bool {
+ pf, ok := p.Field()
+ for x := 0; x < 9; x++ {
+ for y := 0; y < 19; y++ {
+ if f[y][x] && pf[y][x] {
+ return false
+ }
+ }
+ }
+ return ok
+}
+
+func (p Placement) Field() (Field, bool) {
+ var f Field
+ ok := true
+ for _, point := range p.Points() {
+ if point.X < 0 || point.X > 9 || point.Y > 19 {
+ 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] = true
+ }
+ return f, ok
+}
+
+func (p Placement) Points() []Point {
+ piece := p.piece[p.Rot]
+ var points []Point
+ x := []int{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}
+ 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 = 30
var ReferenceBag = Bag{IPiece, JPiece, LPiece, OPiece, SPiece, TPiece, ZPiece}
+func NewBag() *Bag {
+ b := ReferenceBag.Randomize()
+ return &b
+}
+
func (b Bag) Randomize() Bag { //TODO: implement randomizer
return b
}
func (b *Bag) Pick() Placement {
if len(*b) == 0 {
- b = &ReferenceBag.Randomize()
+ b = NewBag()
}
- piece := Placement{piece: Bag[0], X: 3, Y: -2, Lock: LockDelay}
- *b = *b[1:]
+ piece := Placement{piece: (*b)[0], X: 3, Y: -2, Lock: LockDelay}
+ *b = (*b)[1:]
+ return piece
}
type Field [20][10]bool
+func (f Field) Add(p Placement) Field {
+ fn := f
+ for _, point := range p.Points() {
+ if point.Y < 0 || point.Y > 19 || point.X < 0 || point.X > 9 {
+ continue
+ }
+ fn[point.Y][point.X] = true
+ }
+ return fn
+}
+
func (f Field) String() (output string) {
var toprow [10]bool
var top bool