Esempio n. 1
0
        public void move(Pair to, Pair from)
        {
            pegBoard.movePeg(currentPlayer.color, to, from);

            if ((Math.Abs(to.row - from.row) == 1 &&
                 Math.Abs(to.col - from.col) == 1))
            {
                // Add disk
                diskBoard.placeDisk(currentPlayer, to, from);
            }
        }
Esempio n. 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);
        }