Ejemplo n.º 1
0
 public void StartGame()
 {
     GameBoard.Print();
     while (!GameBoard.IsComplete())
     {
         if (Player1.Turn == GameBoard.Turn)
         {
             Player1.NextStep(GameBoard);
         }
         else
         {
             Player2.NextStep(GameBoard);
         }
         GameBoard.Print();
     }
 }
Ejemplo n.º 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));
        }