Commit 4f8f178e authored by ceddral's avatar ceddral
Browse files

board: refactor: make everything take *Place instead of Place

prepare for render getting an optional *Place parameter.
optional parameter must be pointer to be able to detect
whether it is present.
for consistency make everything take pointers
parent 9f59ab99
......@@ -88,8 +88,8 @@ type Board struct {
free *sync.Map
}
func NewPlace(p *Piece, rot Rotation) Place {
return Place{p, rot, make(map[byte]*GamePlayer), make(map[byte]Token)}
func NewPlace(p *Piece, rot Rotation) *Place {
return &Place{p, rot, make(map[byte]*GamePlayer), make(map[byte]Token)}
}
func NewBoard(g *Game) (board *Board) {
......@@ -103,7 +103,7 @@ type Coord struct {
y int
}
func (b *Board) Set(x, y int, p Place) {
func (b *Board) Set(x, y int, p *Place) {
if x <= b.minx { b.minx = x - 1 }
if y <= b.miny { b.miny = y - 1}
if b.maxx <= x + 1 { b.maxx = x + 2 }
......@@ -119,24 +119,28 @@ func (b *Board) Set(x, y int, p Place) {
}
}
func (b *Board) Get(x, y int) (Place, bool) {
func (b *Board) Get(x, y int) (*Place, bool) {
ip, ok := b.m.Load(Coord{x, y})
p := Place{}
p := &Place{}
if ok {
p = ip.(Place)
p = ip.(*Place)
}
return p, ok
}
func (b *Board) GetWritable(x, y int) (Place, bool) {
func (b *Board) GetWritable(x, y int) (*Place, bool) {
pro, ok := b.Get(x, y)
p := pro
if ok {
p.fieldIdToPlayer = make(map[byte]*GamePlayer)
copyMapByteGamePlayer(p.fieldIdToPlayer, pro.fieldIdToPlayer)
p.fieldIdToToken = make(map[byte]Token)
copyMapByteToken(p.fieldIdToToken, pro.fieldIdToToken)
if !ok {
return nil, ok
}
p := &Place{
piece: pro.piece,
rot: pro.rot,
fieldIdToPlayer: make(map[byte]*GamePlayer),
fieldIdToToken: make(map[byte]Token),
}
copyMapByteGamePlayer(p.fieldIdToPlayer, pro.fieldIdToPlayer)
copyMapByteToken(p.fieldIdToToken, pro.fieldIdToToken)
return p, ok
}
......@@ -217,10 +221,10 @@ func getAdjacentCoords(x, y int) []Coord {
return r
}
func (b *Board) GetNeighbours(x, y int) ([]Coord, []Place) {
func (b *Board) GetNeighbours(x, y int) ([]Coord, []*Place) {
cs := getAdjacentCoords(x, y)
rc := make([]Coord, 0, 4)
rp := make([]Place, 0, 4)
rp := make([]*Place, 0, 4)
for _, c := range cs {
p, ok := b.Get(c.x, c.y)
if ok {
......
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