Ejemplo n.º 1
0
        public KnowledgeTree GenerateKnowledgeTree(Board board)
        {
            TreeNodeNumber = 1;
            var seenCache     = new Dictionary <Board, Tuple <int, Board> >();
            var knowledgeTree = new KnowledgeTree();

            FillKnowledgeTree(knowledgeTree, board, seenCache);

            return(knowledgeTree);
        }
Ejemplo n.º 2
0
        void FillKnowledgeTree(KnowledgeTree knowledgeTree, Board board, Dictionary <Board, Tuple <int, Board> > seenCache)
        {
            knowledgeTree.Board  = board.ToString();
            knowledgeTree.Number = TreeNodeNumber++;

            var knowledge = Knowledge[board.Template.Value][board.ColorCount][board];

            if (knowledge.Exists())
            {
                if (knowledge.Reason != "good swap")
                {
                    knowledgeTree.Note = knowledge.Reason;
                }
                else
                {
                    Tuple <int, Board> seenBoard;
                    if (seenCache.TryGetValue(board, out seenBoard))
                    {
                        knowledgeTree.Note = "same as #" + seenBoard.Item1;
                        return;
                    }

                    var f         = knowledge.Board.FindPermutation(board);
                    var colors    = Board.ApplyMapping(f, new[] { knowledge.ColorPair.Item1, knowledge.ColorPair.Item2 }).ToList();
                    var colorPair = new Tuple <int, int>(colors[0], colors[1]);

                    foreach (var swap in knowledge.Swaps)
                    {
                        var tempBoard = board.Clone();
                        var moves     = swap.Item2.Select(m => MetaKnowledge.MapMove(f, m)).ToList();
                        tempBoard.DoMoveCombination(moves);

                        var childKnowledgeTree = knowledgeTree.AddChild(swap);
                        childKnowledgeTree.ColorPair = colorPair;
                        FillKnowledgeTree(childKnowledgeTree, tempBoard, seenCache);
                    }
                }
            }
            else
            {
                throw new Exception("something went wrong in building search tree");
            }

            seenCache[board] = new Tuple <int, Board>(knowledgeTree.Number, board);
        }