Exemplo n.º 1
0
        //for test purposes:
        //--------------------------------------------------------------


        private void CreateTestTree()
        {
            dvonnTree = new PositionTree();
            Node a = new Node();
            Node b = new Node();
            Node c = new Node();
            Node d = new Node();
            Node e = new Node();
            Node f = new Node();
            Node g = new Node();
            Node h = new Node();
            Node i = new Node();
            Node j = new Node();
            Node k = new Node();
            Node l = new Node();
            Node m = new Node();
            Node n = new Node();
            Node o = new Node();
            Node p = new Node();
            Node q = new Node();
            Node r = new Node();
            Node s = new Node();

            dvonnTree.root = a;
            dvonnTree.InsertChild(b, a);
            dvonnTree.InsertChild(c, a);
            dvonnTree.InsertChild(d, a);

            dvonnTree.InsertChild(e, b);
            dvonnTree.InsertChild(f, b);

            dvonnTree.InsertChild(g, c);
            dvonnTree.InsertChild(h, c);

            dvonnTree.InsertChild(i, d);
            dvonnTree.InsertChild(j, d);

            dvonnTree.InsertChild(k, e);
            dvonnTree.InsertChild(l, e);

            dvonnTree.InsertChild(m, f);

            dvonnTree.InsertChild(n, g);
            dvonnTree.InsertChild(o, g);

            dvonnTree.InsertChild(p, h);

            dvonnTree.InsertChild(q, i);

            dvonnTree.InsertChild(r, j);
            dvonnTree.InsertChild(s, j);

            dvonnTree.InsertChild(new Node(), k);
            dvonnTree.InsertChild(new Node(), k);

            dvonnTree.InsertChild(new Node(), l);
            dvonnTree.InsertChild(new Node(), l);
            dvonnTree.InsertChild(new Node(), l);

            dvonnTree.InsertChild(new Node(), m);

            dvonnTree.InsertChild(new Node(), n);

            dvonnTree.InsertChild(new Node(), o);
            dvonnTree.InsertChild(new Node(), o);

            dvonnTree.InsertChild(new Node(), p);

            dvonnTree.InsertChild(new Node(), q);

            dvonnTree.InsertChild(new Node(), r);
            dvonnTree.InsertChild(new Node(), r);

            dvonnTree.InsertChild(new Node(), s);
        }
Exemplo n.º 2
0
        private bool BranchEndpoints(int maxEndPoints)
        {
            int         newNodeCounter      = 0;
            int         gameOverMoveCounter = 0;
            bool        gameOver            = false;
            List <Node> allLeaves           = dvonnTree.GetOuterLeaves();

            //Console.WriteLine();
            //Console.WriteLine("AI: Branching begun at depth " + depthCounter);
            //Console.WriteLine("AI: Color to move: " + tempPlayerToMove.ToString());
            //Console.WriteLine("AI: Endpoints found: " + allLeaves.Count);

            foreach (Node endPoint in allLeaves)
            {
                //if endpoint is already a 'gameOverMove' the move will be copied as the only child
                if (endPoint.move != null && endPoint.move.isGameOverMove)
                {
                    Move gameOverMove = new Move(true, endPoint.move.whiteScore, endPoint.move.blackScore);
                    gameOverMove.gameOverMoveDepth = endPoint.move.gameOverMoveDepth;
                    dvonnTree.InsertChild(new Node(gameOverMove), endPoint);
                    newNodeCounter++;
                    gameOverMoveCounter++;
                    if (gameOverMoveCounter == allLeaves.Count)
                    {
                        Console.WriteLine();
                        Console.WriteLine("AI: stopping branching. All endpoints are 'game over moves'.");
                        gameOver = true;
                    }
                    continue;
                }

                //Checks for dvonn collapse and saves it to endpoint position.
                aiBoard.ReceivePosition(endPoint.resultingPosition);
                if (endPoint == dvonnTree.root)
                {
                    aiBoard.CheckDvonnCollapse(null, false);
                }
                else
                {
                    aiBoard.CheckDvonnCollapse(endPoint.move, false);
                }
                endPoint.resultingPosition = aiBoard.SendPosition();

                List <Move> playerLegalMoves   = aiBoard.FindLegalMoves(tempPlayerToMove);
                List <Move> opponentLegalMoves = aiBoard.FindLegalMoves(tempPlayerToMove.ToOpposite());

                //Check if neither players have any legal moves, if yes, a game over move is created
                if (playerLegalMoves.Count == 0 && opponentLegalMoves.Count == 0)
                {
                    int  whiteScore   = endPoint.resultingPosition.GetScore(PieceID.White);
                    int  blackScore   = endPoint.resultingPosition.GetScore(PieceID.Black);
                    Move gameOverMove = new Move(true, whiteScore, blackScore);
                    gameOverMove.gameOverMoveDepth = endPoint.depth + 1;
                    dvonnTree.InsertChild(new Node(gameOverMove), endPoint);
                    newNodeCounter++;
                    continue;
                }

                //Check if moving player has no legal moves, if yes, a pass move is inserted in the tree
                if (playerLegalMoves.Count == 0)
                {
                    Move passMove = new Move(tempPlayerToMove, true);
                    dvonnTree.InsertChild(new Node(passMove, endPoint.resultingPosition), endPoint);
                    newNodeCounter++;
                    continue;
                }

                foreach (Move move in playerLegalMoves)
                {
                    Position newPosition = new Position();
                    newPosition.Copy(endPoint.resultingPosition);
                    newPosition.MakeMove(move);

                    dvonnTree.InsertChild(new Node(move, newPosition), endPoint);
                    newNodeCounter++;
                }
            }

            Console.WriteLine("AI: Branching for depth level " + depthCounter + " is complete.");
            Console.WriteLine("AI: added total number of nodes: " + newNodeCounter);

            tempPlayerToMove = tempPlayerToMove.ToOpposite();
            depthCounter++;

            return(newNodeCounter == 0 || newNodeCounter > maxEndPoints || gameOver);
        }