Example #1
0
        private static KeyValuePair <int, KeyValuePair <int, int> > BestOptionOfSubtree(GameBoard gameBoard)
        {
            var bestOption = new KeyValuePair <int, KeyValuePair <int, int> >(-2, new KeyValuePair <int, int>());

            foreach (var emptySlot in gameBoard.EmptySlots)
            {
                var tempBoard = (GameBoard)gameBoard.Clone();
                tempBoard.Step(emptySlot);
                if (tempBoard.IsComplete())
                {
                    return(new KeyValuePair <int, KeyValuePair <int, int> >(tempBoard.Winner, emptySlot));
                }

                var option = BestOptionOfSubtree(tempBoard).Key;
                if (option == gameBoard.Turn)
                {
                    return(new KeyValuePair <int, KeyValuePair <int, int> >(option, emptySlot));
                }

                if (bestOption.Key == -gameBoard.Turn || bestOption.Key == -2)
                {
                    bestOption = new KeyValuePair <int, KeyValuePair <int, int> >(option, emptySlot);
                }
            }

            return(bestOption);
        }
Example #2
0
        private KeyValuePair <double, KeyValuePair <int, int> > BestOptionOfSubtreeWithCutsAndRestriction(GameBoard gameBoard, int maxDepth, int currentDepth, double parentBest)
        {
            var currentMultiplier = 1.0 / currentDepth;
            var ownBest           = -gameBoard.Turn * (double.MaxValue - 1) * currentMultiplier;
            var bestSlot          = new KeyValuePair <int, int>();

            if (gameBoard.EmptySlots.Count != 0)
            {
                bestSlot = gameBoard.EmptySlots[0];
            }

            if (gameBoard.IsComplete() || currentDepth == maxDepth)
            {
                var evaluation = EvaluateGameBoardWithRestriction(gameBoard) * currentMultiplier;
                if (gameBoard.Turn * ownBest < gameBoard.Turn * evaluation)
                {
                    ownBest = evaluation;
                }
            }
            else
            {
                foreach (var emptySlot in gameBoard.EmptySlots)
                {
                    if (!IsSlotInBounds(emptySlot, gameBoard))
                    {
                        continue;
                    }
                    var tempBoard = (GameBoard)gameBoard.Clone();
                    tempBoard.Step(emptySlot);
                    var evaluation = BestOptionOfSubtreeWithCutsAndRestriction(tempBoard, maxDepth, currentDepth + 1, ownBest);
                    if (gameBoard.Turn * ownBest < gameBoard.Turn * evaluation.Key)
                    {
                        ownBest  = evaluation.Key;
                        bestSlot = emptySlot;
                    }
                    if (gameBoard.Turn * ownBest >= gameBoard.Turn * parentBest)
                    {
                        return(new KeyValuePair <double, KeyValuePair <int, int> >(ownBest, bestSlot));
                    }
                }
            }

            return(new KeyValuePair <double, KeyValuePair <int, int> >(ownBest, bestSlot));
        }