示例#1
0
        private SingleTree ComputerPlaysToWin(SingleTree nodeToPlay)
        {
            SingleTree winnerNode = null;

            foreach (var child in nodeToPlay.GetChilds().Values)
            {
                if (!child.NodeWithoutChilds())
                {
                    winnerNode = child;

                    foreach (var grandParent in child.GetChilds().Values)
                    {
                        if (!(ComputerPlaysToWin(grandParent) != null || grandParent.NodeWithoutChilds()))
                        {
                            winnerNode = null;
                            break;
                        }
                    }

                    if (winnerNode != null)
                    {
                        break;
                    }
                }
            }
            return(winnerNode);
        }
示例#2
0
 public void AddSinlgeTreeInFile(SingleTree<StringId> singleTree)
 {
     var lines = _treeConverter.ConvertSingleTree(singleTree);
     lines.Insert(0, Environment.NewLine);
     lines.Add(Environment.NewLine);
     File.AppendAllLines(_pathToFile, lines);
 }
 public string ConvertToString(MultiTree<StringId> mainTree, SingleTree<StringId> minorTree)
 {
     var lines = _singleTreeConverter.ConvertAsSingleTrees(mainTree, minorTree);
     var stringBuilder = new StringBuilder();
     lines.ForEach(line => stringBuilder.AppendLine(line));
     return stringBuilder.ToString();
 }
示例#4
0
 public void RebuildSingleTree(object p)
 {
     SingleTree.RebuildTree(RootNr, IncludeFiles);
     if (TabbedNavTrees.SelectedNavTree != null)
     {
         TabbedNavTrees.SelectedNavTree.RebuildTree();
     }
 }
示例#5
0
        private SingleTree ComputerPlaysToGetLongestWord(SingleTree nodeToPlay)
        {
            SingleTree winnerNode = null;

            foreach (var child in nodeToPlay.GetChilds().Values)
            {
                if ((winnerNode == null) || child.MaximumLength() > winnerNode.MaximumLength())
                {
                    winnerNode = child;
                }
            }
            return(winnerNode);
        }
示例#6
0
        public string SelectComputerGameMode()
        {
            SingleTree gameTree = this._gameDictionary.GetLastNode(this._currentWord);

            if (gameTree != null)
            {
                SingleTree winner = this.ComputerPlaysToWin(gameTree);

                if (winner != null)
                {
                    this.CurrentWord += winner.TreeNodeLetter;
                }
                else
                {
                    this.CurrentWord += this.ComputerPlaysToGetLongestWord(gameTree).TreeNodeLetter;
                }
            }
            return(this.CurrentWord);
        }
 public SingleTreeResult(SingleTree<StringId> singleTree)
 {
     _singleTree = singleTree;
 }
 private void ProcessBuildingMinorTree()
 {
     _minorSingleTree = new SingleTree<StringId>(_factory.GetNode("Root", new Root()));
     ProcessBuildingTree(_minorSingleTree);
     _treeLogger.AddSinlgeTreeInFile(_minorSingleTree);
 }
 private void ProcessBuildingMainTree()
 {
     _mainSingleTree = new SingleTree<StringId>(_factory.GetNode("Root", new Root()));
     ProcessBuildingTree(_mainSingleTree);
     _treeLogger.AddSinlgeTreeInFile(_mainSingleTree);
     _mainMultiTree = new MultiTree<StringId>(_mainSingleTree);
     ProcessBuildingTreeFromConsole();
 }
 public ConsoleController()
 {
     _mainSingleTree = new SingleTree<StringId>(_factory.GetNode("Root", new Root()));
     _minorSingleTree = new SingleTree<StringId>(_factory.GetNode("Root", new Root()));
 }
        private void ProcessBuildingTree(SingleTree<StringId> tree)
        {
            Contract.Requires(tree != null);

            var ids = new HashSet<string>(new[] { "Root" });

            while (true)
            {
                DisplayInitialCommand();

                var inputLine = Console.ReadLine();
                var commands = inputLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                if (commands.Length != 3 && commands.Length != 1)
                {
                    _messages.Add("Not full set of commands");
                    continue;
                }

                var firstCommand = commands[0];

                if (commands.Length == 1)
                {
                    if (firstCommand == "end")
                    {
                        _messages.Add("Tree was successfully built");
                        break;
                    }

                    _messages.Add("There is not such command");
                    continue;
                }

                if (ids.Contains(firstCommand))
                {
                    _messages.Add("The such id already exists");
                    DisplayInitialCommand();
                    continue;
                }

                var secondCommand = commands[1];

                if (!NodeInfoFactory.Contains(secondCommand))
                {
                    _messages.Add(String.Format("There is not such type of NodeData like - {0}", secondCommand));
                    DisplayInitialCommand();
                    continue;
                }

                var thirdCommand = commands[2];

                if (!ids.Contains(thirdCommand))
                {
                    _messages.Add(String.Format("The such parent id like {0} does not exist", thirdCommand));
                    DisplayInitialCommand();
                    continue;
                }

                var parentNode = tree.GetById(new StringId(thirdCommand));
                var childNode = _factory.GetNode(firstCommand, NodeInfoFactory.GetNodeInfo(secondCommand));

                var parentTypeName = GetNodeClassName(parentNode);
                var childTypeName = GetNodeClassName(childNode);

                if (parentNode.CanContain(childNode))
                {
                    _messages.Add(string.Format("The Node {0} with type - {1} was added to {2} with type - {3}",
                        childNode.SingleNodeData.Id, childTypeName, parentNode.SingleNodeData.Id, parentTypeName));

                    parentNode.Add(childNode);
                    ids.Add(firstCommand);
                }
                else
                {
                    _messages.Add(string.Format("The NodeData with type {0} can not be added to the {1}", childTypeName, parentTypeName));
                }
            }
        }