Пример #1
0
        /*/// <summary> moveAI
         * /// calls AI and returns (to) and (from)
         * /// </summary>
         * public PieceMove moveAI()
         * {
         *  PieceMove aiMove = currentPlayer.getAIMove(currentPlayer, pegBoard, diskBoard);
         *  move(aiMove.to, aiMove.from);
         *  return aiMove;
         * }*/

        /// <summary> moveAI
        /// calls AI and returns (to) and (from)
        /// </summary>
        public PieceMove moveAI(int treeDepth)
        {
            PieceMove aiMove = currentPlayer.getAIMove(currentPlayer, pegBoard, diskBoard, treeDepth);

            if (currentPlayer.playerType == Player.TypeOfPlayer.AI)
            {
                move(aiMove.to, aiMove.from);
            }

            return(aiMove);
        }
Пример #2
0
        /// <summary>
        /// Populates each child node pushing every possible move
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        private List <TreeNode> PopulateTree(TreeNode parent)
        {
            List <TreeNode> children = new List <TreeNode>();

            //loop through each peg
            //foreach (Peg peg in parent._pegBoard.pegList)
            ParallelOptions op = new ParallelOptions();

            op.MaxDegreeOfParallelism = 40;

            Parallel.ForEach(parent._pegBoard.pegList, op, peg =>
            {
                //if the peg is the same color as this node
                if (peg.color == parent._currentColor)
                {
                    //loop through all possible moves
                    foreach (Pair pair in _moveSet)
                    {
                        //if the move keeps us on the board, and it is a valid move
                        if (peg.pos.row + pair.row >= 0 &&
                            peg.pos.row + pair.row < _pegBoard.size &&
                            peg.pos.col + pair.col >= 0 &&
                            peg.pos.col + pair.col < _pegBoard.size &&
                            isValid(new Pair(peg.pos.row + pair.row, peg.pos.col + pair.col), peg.pos, parent._pegBoard, parent._currentColor))
                        {
                            //create a new pegBoard and make the move on the new board
                            PegBoard pegBoard   = new PegBoard(parent._pegBoard);
                            DiskBoard diskBoard = new DiskBoard(parent._diskBoard);

                            PieceMove pm = new PieceMove(new Pair(peg.pos.row + pair.row, peg.pos.col + pair.col), peg.pos);

                            pegBoard.movePeg(parent._currentColor, pm.to, pm.from);
                            if ((Math.Abs(pair.row) == 1 &&
                                 Math.Abs(pair.col) == 1))
                            {
                                diskBoard.placeDisk(parent._currentColor, pm.to, pm.from);
                            }


                            // change the diskBoard

                            //add this new node as a child node
                            lock (this)
                            {
                                children.Add(new TreeNode(parent._currentColor, pegBoard, diskBoard, parent._minimaxValue, new Pair(peg.pos.row + pair.row, peg.pos.col + pair.col), peg.pos, parent._levelInTree + 1, parent._aiColor, parent._movesFromWin, parent._maxTreeDepth, parent._middlePegHeuristic));
                            }
                        }
                    }
                }
            });

            return(children);
        }
Пример #3
0
        /// <summary>
        /// Private Constructor used to initialize child node
        /// </summary>
        /// <param name="currentColor"></param> ---> will switch color
        /// <param name="pegBoard"></param>
        /// <param name="diskBoard"></param>
        /// <param name="minimaxValue"></param> ---> will switch min/max
        /// <param name="to"></param>
        /// <param name="from"></param>
        /// <param name="levelInTree"></param>
        /// <param name="aiColor"></param>
        private TreeNode(Player.Color currentColor, PegBoard pegBoard, DiskBoard diskBoard, MiniMax minimaxValue, Pair to, Pair from, int levelInTree, Player.Color aiColor, int movesFromWin, int maxTreeDepth, double middlePegHeuristic)
        {
            this._pegBoard           = pegBoard;
            this._diskBoard          = diskBoard;
            this._moveMade           = new PieceMove();
            this._moveMade.to        = to;
            this._moveMade.from      = from;
            this._levelInTree        = levelInTree;
            this._aiColor            = aiColor;
            this._movesFromWin       = movesFromWin - 1;
            this._maxTreeDepth       = maxTreeDepth;
            this._middlePegHeuristic = middlePegHeuristic;

            if (maxTreeDepth == 1)
            {
                this._difficultyLevel = Difficulty.Easy;
            }
            else if (maxTreeDepth == 3)
            {
                this._difficultyLevel = Difficulty.Medium;
            }
            else
            {
                this._difficultyLevel = Difficulty.Hard;
            }

            // Switch colors
            if (currentColor == Player.Color.White)
            {
                this._currentColor = Player.Color.Black;
            }
            else
            {
                this._currentColor = Player.Color.White;
            }

            // Switch minimaxValue
            if (minimaxValue == MiniMax.Max)
            {
                this._minimaxValue = MiniMax.Min;
            }
            else
            {
                this._minimaxValue = MiniMax.Max;
            }
        }
Пример #4
0
        public PieceMove getAIMove(Player currentPlayer, PegBoard pegBoard, DiskBoard diskBoard, int treeDepth)
        {
            PieceMove move         = null;
            TreeNode  root         = new TreeNode(currentPlayer, pegBoard, diskBoard, TreeNode.MiniMax.Min, treeDepth);
            int       selectedMove = root.AlphaBeta(root, int.MinValue, int.MaxValue);

            foreach (TreeNode node in root.children)
            {
                if (node.heuristicValue < selectedMove)
                {
                    selectedMove = node.heuristicValue;
                    move         = node.moveMade;
                }
            }
            if (move == null)
            {
                move = root.children.ElementAt(0).moveMade;
            }

            return(move);
        }