func (self *Node) connectRandomly(c common.Logger, allNodes []*Node, state *State) { minEdges := common.Norm(4, 2, 2, len(allNodes)-1) for len(self.Edges) < minEdges || !self.allReachable(c, state) { perm := rand.Perm(len(allNodes)) var randomNode *Node for _, index := range perm { suggested := allNodes[index] if suggested.Id != self.Id { if _, found := self.Edges[suggested.Id]; !found { randomNode = suggested break } } } self.Connect(randomNode, common.Norm(3, 1, 1, 5)) minEdges-- } }
/* RandomState creates a random state for the provided players. */ func RandomState(c common.Logger, players []PlayerId) (result *State) { result = NewState() size := common.Norm(len(players)*6, len(players), len(players)*4, len(players)*10) allNodes := make([]*Node, 0, size) for i := 0; i < size; i++ { node := RandomNode() result.Nodes[node.Id] = node allNodes = append(allNodes, node) } for _, node := range allNodes { node.connectRandomly(c, allNodes, result) } perm := rand.Perm(len(allNodes)) for index, playerId := range players { allNodes[perm[index]].Units[playerId] = 10 } return }
/* RandomNode returns a random node without connections. */ func RandomNode() (result *Node) { return NewNode(NodeId(common.RandomString(16)), common.Norm(50, 25, 10, 100)) }