/// <summary>
        /// Selects best first move using minimax
        /// </summary>
        /// <param name="args">command-line arguments</param>
        static void Main(string[] args)
        {
            // build and mark the tree with minimax scores
            MinimaxTree <char> tree = BuildTree();

            //Attach scores to each of the nodes in the tree
            Minimax(tree.Root, true);

            // find child node with maximum score
            IList <MinimaxTreeNode <char> > children     = tree.Root.Children; //Save the children of the root
            MinimaxTreeNode <char>          maxChildNode = children[0];        //Max child node is the first child (Assuming that the left choice is the best)

            //Just in case we are wrong we check here if the minimax score is right and the maxchild is the greatest, if not, then reassign it
            for (int i = 1; i < children.Count; i++)
            {
                if (children[i].MinimaxScore > maxChildNode.MinimaxScore)
                {
                    maxChildNode = children[i];
                }
            }

            // print best move
            Console.WriteLine("Best move is to char " + maxChildNode.Value);


            Console.ReadLine();
        }
Esempio n. 2
0
        /// <summary>
        /// Selects best first move using minimax
        /// </summary>
        /// <param name="args">command-line arguments</param>
        static void Main(string[] args)
        {
            // build and mark the tree with minimax scores
            MinimaxTree <char> tree = BuildTree();

            InitializeLeafScores();
            bool maximizing = true;

            Minimax(tree.Root, maximizing);

            // find optimal minimax path
            StringBuilder optimalPath = new StringBuilder();

            optimalPath.Append("Optimal Path: ");

            MinimaxTreeNode <char> currentNode = tree.Root;

            optimalPath.Append($"[{currentNode.Value}={currentNode.MinimaxScore}]");

            MinimaxTreeNode <char> nextNode = GetBestChild(currentNode, maximizing);

            while (nextNode != null)
            {
                currentNode = nextNode;
                optimalPath.Append($"=>[{currentNode.Value}], [{currentNode.Value}={currentNode.MinimaxScore}]");
                maximizing = !maximizing;
                nextNode   = GetBestChild(currentNode, maximizing);
            }

            // print best move
            Console.WriteLine(optimalPath.ToString());

            Console.WriteLine();
        }
        /// <summary>
        /// Builds the tree
        /// </summary>
        /// <returns>tree</returns>
        static MinimaxTree <char> BuildTree()
        {
            MinimaxTree <char>     tree  = new MinimaxTree <char>('A');
            MinimaxTreeNode <char> bNode = new MinimaxTreeNode <char>('B', tree.Root);

            tree.AddNode(bNode);
            MinimaxTreeNode <char> cNode = new MinimaxTreeNode <char>('C', tree.Root);

            tree.AddNode(cNode);
            MinimaxTreeNode <char> dNode = new MinimaxTreeNode <char>('D', tree.Root);

            tree.AddNode(dNode);
            MinimaxTreeNode <char> eNode = new MinimaxTreeNode <char>('E', bNode);

            tree.AddNode(eNode);
            MinimaxTreeNode <char> fNode = new MinimaxTreeNode <char>('F', bNode);

            tree.AddNode(fNode);
            MinimaxTreeNode <char> gNode = new MinimaxTreeNode <char>('G', bNode);

            tree.AddNode(gNode);
            MinimaxTreeNode <char> hNode = new MinimaxTreeNode <char>('H', cNode);

            tree.AddNode(hNode);
            MinimaxTreeNode <char> iNode = new MinimaxTreeNode <char>('I', cNode);

            tree.AddNode(iNode);
            MinimaxTreeNode <char> jNode = new MinimaxTreeNode <char>('J', dNode);

            tree.AddNode(jNode);
            MinimaxTreeNode <char> kNode = new MinimaxTreeNode <char>('K', dNode);

            tree.AddNode(kNode);
            MinimaxTreeNode <char> lNode = new MinimaxTreeNode <char>('L', eNode);

            tree.AddNode(lNode);
            MinimaxTreeNode <char> mNode = new MinimaxTreeNode <char>('M', eNode);

            tree.AddNode(mNode);
            MinimaxTreeNode <char> nNode = new MinimaxTreeNode <char>('N', fNode);

            tree.AddNode(nNode);
            MinimaxTreeNode <char> oNode = new MinimaxTreeNode <char>('O', fNode);

            tree.AddNode(oNode);
            MinimaxTreeNode <char> pNode = new MinimaxTreeNode <char>('P', gNode);

            tree.AddNode(pNode);
            MinimaxTreeNode <char> qNode = new MinimaxTreeNode <char>('Q', gNode);

            tree.AddNode(qNode);
            MinimaxTreeNode <char> rNode = new MinimaxTreeNode <char>('R', hNode);

            tree.AddNode(rNode);
            MinimaxTreeNode <char> sNode = new MinimaxTreeNode <char>('S', hNode);

            tree.AddNode(sNode);
            MinimaxTreeNode <char> tNode = new MinimaxTreeNode <char>('T', iNode);

            tree.AddNode(tNode);
            MinimaxTreeNode <char> uNode = new MinimaxTreeNode <char>('U', iNode);

            tree.AddNode(uNode);
            MinimaxTreeNode <char> vNode = new MinimaxTreeNode <char>('V', jNode);

            tree.AddNode(vNode);
            MinimaxTreeNode <char> wNode = new MinimaxTreeNode <char>('W', jNode);

            tree.AddNode(wNode);
            MinimaxTreeNode <char> xNode = new MinimaxTreeNode <char>('X', kNode);

            tree.AddNode(xNode);
            MinimaxTreeNode <char> yNode = new MinimaxTreeNode <char>('Y', kNode);

            tree.AddNode(yNode);
            return(tree);
        }