// boardFromWire converts the wire representation to the board func boardFromWire(wire botapi.Board) (*Board, error) { b := EmptyBoard(int(wire.Width()), int(wire.Height())) b.Round = int(wire.Round()) bots, err := wire.Robots() if err != nil { return b, err } for i := 0; i < bots.Len(); i++ { bot := bots.At(i) loc := Loc{ X: int(bot.X()), Y: int(bot.Y()), } b.cells[b.cellIndex(loc)] = robotFromWire(bot) } return b, nil }
func convertBoard(wire botapi.Board) (b *Board, playerBots []*Robot, err error) { w, h := int(wire.Width()), int(wire.Height()) cells := make([]*Robot, w*h) rows := make([][]*Robot, h) for y := range rows { rows[y] = cells[y*w : (y+1)*w] } robots, err := wire.Robots() if err != nil { return nil, nil, err } playerBots = make([]*Robot, 0, robots.Len()) for i, n := 0, robots.Len(); i < n; i++ { r := robots.At(i) // TODO(light): check for negative (x,y) rr := &Robot{ ID: r.Id(), Loc: Loc{int(r.X()), int(r.Y())}, Health: int(r.Health()), } switch r.Faction() { case botapi.Faction_mine: rr.Faction = MyFaction playerBots = append(playerBots, rr) case botapi.Faction_opponent: fallthrough default: rr.Faction = OpponentFaction } rows[rr.Loc.Y][rr.Loc.X] = rr } return &Board{ Round: int(wire.Round()), Cells: rows, }, playerBots, nil }