feat: add game/shape_queue.go
This commit is contained in:
parent
6e42dbce4f
commit
5c4503268f
1 changed files with 43 additions and 0 deletions
43
game/shape_queue.go
Normal file
43
game/shape_queue.go
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
package game
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ShapeQueue struct {
|
||||||
|
shapes []ShapeType
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewShapeQueue() *ShapeQueue {
|
||||||
|
sq := &ShapeQueue{
|
||||||
|
shapes: []ShapeType{},
|
||||||
|
}
|
||||||
|
sq.Generate()
|
||||||
|
return sq
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sq *ShapeQueue) Generate() {
|
||||||
|
types := []ShapeType{
|
||||||
|
SHAPE_I,
|
||||||
|
SHAPE_J,
|
||||||
|
SHAPE_L,
|
||||||
|
SHAPE_O,
|
||||||
|
SHAPE_S,
|
||||||
|
SHAPE_T,
|
||||||
|
SHAPE_Z,
|
||||||
|
}
|
||||||
|
rand.Shuffle(len(types), func(i int, j int) {
|
||||||
|
types[i], types[j] = types[j], types[i]
|
||||||
|
})
|
||||||
|
|
||||||
|
sq.shapes = append(sq.shapes, types...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sq *ShapeQueue) Next() Shape {
|
||||||
|
if len(sq.shapes) < 1 {
|
||||||
|
sq.Generate()
|
||||||
|
}
|
||||||
|
t := sq.shapes[0]
|
||||||
|
sq.shapes = sq.shapes[1:]
|
||||||
|
return NewShape(t)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue