diff options
author | Joop Kiefte <ikojba@gmail.com> | 2020-09-07 01:34:57 +0200 |
---|---|---|
committer | Joop Kiefte <ikojba@gmail.com> | 2020-09-07 01:34:57 +0200 |
commit | 295f19e88a7a7ebd1de019b091a362324a369809 (patch) | |
tree | 6833e4832e44b6ede9c780c429fa36f28effef02 /tris/core.go | |
parent | 749ecb0e4ed365be186ad3ee65c8a04634ac247b (diff) |
Working demov0.0.3
Diffstat (limited to 'tris/core.go')
-rw-r--r-- | tris/core.go | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/tris/core.go b/tris/core.go index 3121adf..c5d0ea2 100644 --- a/tris/core.go +++ b/tris/core.go @@ -1,5 +1,10 @@ package tris +import ( + "math/rand" + "time" +) + type Point struct { X, Y int } @@ -32,29 +37,28 @@ type Placement struct { X int Y int Rot Rotation - Lock int + Lock time.Time } -func (p *Placement) Drop(f Field) bool { +func (p Placement) Drop(f Field) (Placement, bool) { newp := p newp.Y++ if !newp.Collide(f) { - p = newp - return true + return newp, true } - return false + return p, false } -func (p Placement) Collide(f Field) bool { +func (p Placement) Collide(f Field) bool { //TODO: Fix first piece collision bug 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 true } } } - return ok + return !ok } func (p Placement) Field() (Field, bool) { @@ -88,26 +92,27 @@ func (p Placement) Points() []Point { type Bag []Piece -var LockDelay = 30 +var LockDelay = time.Second/2 var ReferenceBag = Bag{IPiece, JPiece, LPiece, OPiece, SPiece, TPiece, ZPiece} -func NewBag() *Bag { +func NewBag() Bag { b := ReferenceBag.Randomize() - return &b + return b } -func (b Bag) Randomize() Bag { //TODO: implement randomizer +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() Placement { - if len(*b) == 0 { +func (b Bag) Pick() (Bag, Placement) { + if len(b) == 0 { b = NewBag() } - piece := Placement{piece: (*b)[0], X: 3, Y: -2, Lock: LockDelay} - *b = (*b)[1:] - return piece + piece := Placement{piece: b[0], X: 3, Y: -2, Lock: time.Now()} + b = b[1:] + return b, piece } type Field [20][10]bool |