コード例 #1
0
        /// <summary>
        /// Called when the start button is pressed <para/>
        /// Initialises the <see cref="mcts"/> tree search object and instantiates the root node <para/>
        /// Also creates as many starting nodes as the user specified
        /// </summary>
        public void StartButtonPressed()
        {
            //Create an empty board instance, which will have whatever game the user chooses assigned to it
            Board board;

            //Assign whatever game board the user has chosen to the board instance
            switch (HashUIController.GetGameChoice)
            {
            case 0:
                board             = new TTTBoard();
                displayBoardModel = false;
                break;

            case 1:
                board             = new C4Board();
                displayBoardModel = true;

                //Create a C4 Board GameObject and obtain a reference to its BoardModelController Component
                GameObject boardModel = Instantiate(Resources.Load("C4 Board", typeof(GameObject))) as GameObject;
                boardModelController = boardModel.GetComponent <BoardModelController>();
                boardModelController.Initialise();
                break;

            case 2:
                board             = new OthelloBoard();
                displayBoardModel = false;
                break;

            default:
                throw new System.Exception("Unknown game type index has been input");
            }

            mcts = new TreeSearch <Node>(board);

            //Calculate the position of the root node and add an object for it to the scene
            Vector3    rootNodePosition = BoardToPosition(mcts.Root.GameBoard);
            GameObject rootNode         = Instantiate(Resources.Load("HashNode"), rootNodePosition, Quaternion.identity) as GameObject;

            rootNode.transform.parent = transform;
            rootNode.GetComponent <HashNode>().AddNode(null, mcts.Root, false);
            rootNode.GetComponent <HashNode>().Initialise(rootNodePosition);

            //Add the root node to the position and object map
            nodePositionMap.Add(rootNodePosition, rootNode);
            nodeObjectMap.Add(mcts.Root, rootNode);

            //Create the amount of starting nodes specified by the user
            for (int i = 0; i < HashUIController.GetStartingNodeInput(); i++)
            {
                PerformStep(true);
            }

            //Swap out the current menu panels
            HashUIController.SetMenuPanelActive(false);
            HashUIController.SetNavigationPanelActive(true);
        }
コード例 #2
0
        public void GetPossibleMovesOneFullColumn()
        {
            C4Board board = new C4Board();

            for (int i = 0; i < board.Height; i++)
            {
                board.MakeMove(new C4Move(3));
            }
            Assert.AreEqual(board.Width - 1, board.PossibleMoves().Count);
        }
コード例 #3
0
        public void NoWinnerTest()
        {
            C4Board board = new C4Board();

            //Make a move, if there is a winner, the winner flag will be set, but there is no winner, so it shouldn't
            board.MakeMove(new C4Move(1));

            //Check that the winner flag has not been set, as there is no winner
            Assert.AreEqual(-1, board.Winner);
        }
コード例 #4
0
        void Start()
        {
            //Initialise LineDraw and enable background running
            LineDraw.Lines = new List <ColoredLine>();
            Application.runInBackground = true;

            //Initialise the game board and display
            board = new C4Board();
            modelController.Initialise();
            modelController.SetBoard(board);
        }
コード例 #5
0
        public void GetPossibleMovesFullBoard()
        {
            C4Board board = new C4Board();

            for (int y = 0; y < board.Height; y++)
            {
                for (int x = 0; x < board.Width; x++)
                {
                    board.MakeMove(new C4Move(x));
                }
            }
            Assert.AreEqual(0, board.PossibleMoves().Count);
        }
コード例 #6
0
        public void MakeMoveInFullColumn()
        {
            C4Board board = new C4Board();

            //Fill a column up, so that any future moves in the column should cause an InvalidMoveException
            for (int i = 0; i < board.Height; i++)
            {
                board.MakeMove(new C4Move(1));
            }

            //Attempt to make a move in the full column, which should throw an InvalidMoveException
            Assert.Throws <InvalidMoveException>(() => board.MakeMove(new C4Move(1)));
        }
コード例 #7
0
        public void MakeMoveTest()
        {
            //Create a new board and make a move in it
            C4Board board = new C4Board();

            board.MakeMove(new C4Move(2));

            //Check that the move was made correctly
            Assert.AreEqual(1, board.GetCell(2, 0));

            //Make a move on the board for the second player
            board.MakeMove(new C4Move(2));

            //Check that the move was made correctly
            Assert.AreEqual(2, board.GetCell(2, 1));
        }
コード例 #8
0
        public void CreateBoardTest()
        {
            C4Board board = new C4Board();

            //Check that the current player is player 1
            Assert.AreEqual(1, board.CurrentPlayer);

            //Ensure that the created board is empty
            for (int y = 0; y < board.Height; y++)
            {
                for (int x = 0; x < board.Width; x++)
                {
                    Assert.AreEqual(0, board.GetCell(x, y));
                }
            }

            //Ensure that the winner value is - 1
            Assert.AreEqual(-1, board.Winner);
        }
コード例 #9
0
        public void DrawTest()
        {
            //Create a full board with no winner
            C4Board board = new C4Board();

            //Make moves until the board is full and there are no winners
            for (int y = 0; y < board.Height; y++)
            {
                board.MakeMove(new C4Move(0));
                board.MakeMove(new C4Move(2));
                board.MakeMove(new C4Move(1));
                board.MakeMove(new C4Move(3));
                board.MakeMove(new C4Move(4));
                board.MakeMove(new C4Move(6));
                board.MakeMove(new C4Move(5));
            }

            //Check that the game has ended in a draw
            Assert.AreEqual(0, board.Winner);
        }
コード例 #10
0
        public void DuplicateTest()
        {
            //Create a new board and make a move in it
            C4Board boardA = new C4Board();

            boardA.MakeMove(new C4Move(3));

            //Duplicate the board and store it in a new board instance
            C4Board boardB = (C4Board)boardA.Duplicate();

            //Ensure the move made before duplication is present in both boards
            Assert.AreEqual(1, boardA.GetCell(3, 0));
            Assert.AreEqual(1, boardB.GetCell(3, 0));

            //These two board instances should share no memory, lets prove it by making moves in each of them and checking the other
            boardA.MakeMove(new C4Move(6));
            Assert.AreEqual(2, boardA.GetCell(6, 0));
            Assert.AreEqual(0, boardB.GetCell(6, 0));

            boardB.MakeMove(new C4Move(3));
            Assert.AreEqual(0, boardA.GetCell(3, 1));
            Assert.AreEqual(2, boardB.GetCell(3, 1));
        }
コード例 #11
0
        public void GetPossibleMovesEmptyBoard()
        {
            C4Board board = new C4Board();

            Assert.AreEqual(board.Width, board.PossibleMoves().Count);
        }
コード例 #12
0
        public void SimulateTest()
        {
            C4Board board = new C4Board();

            board.SimulateUntilEnd();
        }
コード例 #13
0
        /// <summary>
        /// Called when the start/stop button is pressed <para/>
        /// If MCTS is not running, then it will be started <para/>
        /// If MCTS is running, this will make it finish early
        /// </summary>
        public void StartStopButtonPressed()
        {
            //Starts or ends MCTS depending on when the button is pressed
            if (mcts == null)
            {
                //Create an empty board instance, which will have whatever game the user chooses assigned to it
                Board board;

                //Assign whatever game board the user has chosen to the board instance
                switch (TreeUIController.GetGameChoice)
                {
                case 0:
                    displayBoardModel = false;
                    board             = new TTTBoard();
                    break;

                case 1:
                    displayBoardModel = true;
                    board             = new C4Board();

                    //Create a C4 Board GameObject and obtain a reference to its BoardModelController Component
                    GameObject boardModel = Instantiate(Resources.Load("C4 Board", typeof(GameObject))) as GameObject;
                    boardModelController = boardModel.GetComponent <BoardModelController>();
                    boardModelController.Initialise();
                    break;

                case 2:
                    displayBoardModel = false;
                    board             = new OthelloBoard();
                    break;

                default:
                    throw new System.Exception("Unknown game type index has been input");
                }

                //Assign whatever visualisation type the user has chosen
                switch (TreeUIController.GetVisualisationChoice)
                {
                case 0:
                    visualisationType = VisualisationType.Standard3D;
                    break;

                case 1:
                    visualisationType = VisualisationType.Disk2D;
                    break;

                case 2:
                    visualisationType = VisualisationType.Cone;
                    break;

                default:
                    throw new System.Exception("Unknown visualisation type: encountered");
                }

                //Initialise MCTS on the given game board
                mcts = new TreeSearch <NodeObject>(board);

                //Obtain the time to run mcts for from the input user amount
                timeToRunFor = TreeUIController.GetTimeToRunInput;
                timeLeft     = timeToRunFor;

                //Run mcts asyncronously
                RunMCTS(mcts);
                TreeUIController.StartButtonPressed();
            }
            else
            {
                //Stop the MCTS early
                mcts.Finish();
            }
        }