Commit a146ed29 authored by ceddral's avatar ceddral
Browse files

board: keep track of free spaces

free means adjacent to another piece but not containing a piece
parent 4e1699a2
......@@ -66,6 +66,7 @@ type Board struct {
maxx int // max exclusive
maxy int
m *sync.Map
free *sync.Map
}
func NewPlace(p *Piece, rot Rotation) Place {
......@@ -73,7 +74,7 @@ func NewPlace(p *Piece, rot Rotation) Place {
}
func NewBoard(g *Game) (board *Board) {
board = &Board{0, 0, 1, 1, &sync.Map{}}
board = &Board{0, 0, 1, 1, &sync.Map{}, &sync.Map{}}
board.Set(0, 0, NewPlace(g.drawPiece(), 0))
return
}
......@@ -89,6 +90,14 @@ func (b *Board) Set(x, y int, p Place) {
if b.maxx <= x + 1 { b.maxx = x + 2 }
if b.maxy <= y + 1 { b.maxy = y + 2 }
b.m.Store(Coord{x, y}, p)
b.free.Delete(Coord{x, y})
cs := getAdjacentCoords(x, y)
for _, c := range cs {
_, ok := b.Get(c.x, c.y)
if !ok {
b.free.Store(Coord{c.x, c.y}, struct{}{})
}
}
}
func (b *Board) Get(x, y int) (Place, bool) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment