public static ChanceTree Create(GameDefinition gd) { // First pass - count nodes. GlobalContext gc = new GlobalContext { GameDef = gd }; GameContext root = new GameContext { GameState = new GameState(gd), Global = gc }; ProcessGameContext(root); // Create the tree. int nodeCount = root.ChildCount + 1; ChanceTree ct = new ChanceTree(nodeCount); // Clear memory to ensure stable values in unused fields. ct.SetNodesMemory(0); // Second pass - initialize nodes. gc.Tree = ct; gc.NodeId = 0; gc.TotalCombCountOfLeaves = root.CombCountOfLeaves; root.CombCountOfLeaves = 0; ProcessGameContext(root); ct.Version.Description = String.Format("Chance tree (gamedef: {0})", gd.Name); return(ct); }
public ChanceTree Extract(ChanceTree ct, int position) { if (position >= ct.PlayersCount) { throw new ArgumentOutOfRangeException( string.Format("Position {0} is out of range (players count: {1})", position, ct.PlayersCount)); } _position = position; WalkUFTreePP <ChanceTree, Context> wt = new WalkUFTreePP <ChanceTree, Context>(); wt.OnNodeBegin = OnNodeBegin; _nodeCount = 0; _tempRoot = new TempNode { Card = -1, Probab = 1 }; wt.Walk(ct); ChanceTree newTree = new ChanceTree(_nodeCount + 1); // Reset memory to clear results. newTree.SetNodesMemory(0); _nodeCount = 0; CopyFromTemp(newTree, _tempRoot, 0); // Overwrite root position newTree.Nodes[0].Position = 1; newTree.Version.Description = String.Format("Player {0} chance tree from {1}", position, ct.Version.Description); return(newTree); }
void CreatePlayerCt(int[] cardCount) { int nodeCount = 1; int power = 1; for (int r = 0; r < cardCount.Length; ++r) { power *= cardCount[r]; nodeCount += power; } PlayerCt = new ChanceTree(nodeCount); PlayerCt.SetDepthsMemory(0); PlayerCt.SetNodesMemory(0); int n = 0; FillPlayerCt(0, cardCount, ref n); }
public ChanceTree ConvertToChanceTree() { int depth = PlayersCount * RoundsCount; int nodesCount = CountNodes <int> .Count(Root, Root as object); ChanceTree ct = new ChanceTree(nodesCount); ct.SetNodesMemory(0); // To clear results. SyncUniAndUF <int> .Sync(Root, Root as object, ct, SyncNodes); WalkUFTreePP <ChanceTree, WalkUFTreePPContext> wt = new WalkUFTreePP <ChanceTree, WalkUFTreePPContext>(); wt.OnNodeEnd = FinalizeChanceTree_OnNodeEnd; wt.Walk(ct); ct.PlayersCount = PlayersCount; ct.Version.Description = String.Format("Chance tree (MC:{0:0,0}, {1})", SamplesCount, SourceInfo); return(ct); }
public static ChanceTree FromTxt(TextReader r) { int ln = 0; int serializationFormat = int.Parse(TextDumpHelper.ReadTag(r, ref ln, "SeralizationFormat")); if (serializationFormat > SERIALIZATION_FORMAT) { throw new ApplicationException(String.Format("Line {0}: serialization format {1} is not supported by this version, max supported {2}", ln, serializationFormat, SERIALIZATION_FORMAT)); } string value; value = TextDumpHelper.ReadTag(r, ref ln, "Version"); StringReader sr = new StringReader(value); BdsVersion v; XmlSerializerExt.Deserialize(out v, sr); Int64 nodesCount = Int64.Parse(TextDumpHelper.ReadTag(r, ref ln, "NodesCount")); int roundsCount = int.Parse(TextDumpHelper.ReadTag(r, ref ln, "RoundsCount")); ChanceTree t = new ChanceTree(nodesCount); t.SetNodesMemory(0); // Clear memory to ensure zeros at unused fields t.Version.CopyFrom(v); char [] separators = new char[] { ' ', '\t' }; for (Int64 n = 0; n < nodesCount; ++n) { Int64 id = Int64.Parse(TextDumpHelper.ReadTag(r, ref ln, "Id")); if (id != n) { throw new ApplicationException(String.Format("Line {0}: wrong node id '{1}', expected '{2}'", ln, id, n)); } byte depth = byte.Parse(TextDumpHelper.ReadTag(r, ref ln, "D")); t.SetDepth(n, depth); t.Nodes[n].Position = int.Parse(TextDumpHelper.ReadTag(r, ref ln, "P")); t.Nodes[n].Card = int.Parse(TextDumpHelper.ReadTag(r, ref ln, "C")); t.Nodes[n].Probab = TextDumpHelper.BinStringToDouble(TextDumpHelper.ReadTag(r, ref ln, "Pr")); if (depth == t.PlayersCount * roundsCount) { double[] potShare = new double[t.PlayersCount]; UInt16[] activePlayerMasks = ActivePlayers.Get(t.PlayersCount, 2, t.PlayersCount); for (int a = 0; a < activePlayerMasks.Length; ++a) { value = TextDumpHelper.ReadTag(r, ref ln, "Ps"); string[] parts = value.Split(separators, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != t.PlayersCount + 1) { throw new ApplicationException( String.Format("Line {0}: wrong number of values: '{1}', expected '{2}'", ln, parts.Length, t.PlayersCount + 1)); } UInt16 activePlayers = UInt16.Parse(parts[0], NumberStyles.AllowHexSpecifier); for (int i = 1; i < parts.Length; ++i) { potShare[i - 1] = TextDumpHelper.BinStringToDouble(parts[i]); } t.Nodes[n].SetPotShare(activePlayers, potShare); } } } return(t); }