Exemplo n.º 1
0
        public void MakeMoveShouldMakeWinningMoveForXRandomly()
        {
            //arrange
            //setup a mock for the initial object..
            Move[]       initialGameMoveArray = new Move[1];
            Mock <IGame> initialGame          = new Mock <IGame>();

            initialGame
            .Setup(a => a.MoveHistory)
            .Returns(initialGameMoveArray);

            TicTacToeFactoryMock
            .Setup(a => a.NewGame(initialGameMoveArray))
            .Returns(GameMock.Object);

            //setup the game mock that will drive the rest of our choices
            List <Move> legalMoves = new List <Move>()
            {
                Move.Northern,
                Move.Center,
                Move.Southern
            };

            GameMock
            .Setup(a => a.GetLegalMoves())
            .Returns(legalMoves);

            GameMock
            .Setup(a => a.GameIsOver)
            .Returns(true);

            GameMock
            .Setup(a => a.CurrentPlayer)
            .Returns(Player.X);

            var callCount = 0;

            GameMock
            .Setup(a => a.GameState)
            .Returns(() =>
            {
                callCount++;
                switch (callCount - 1)
                {
                case 0:
                    return(GameState.OWin);

                case 1:
                    return(GameState.XWin);

                default:
                    return(GameState.Tie);
                }
            });

            //setup up our random mock
            RandomMock
            .Setup(a => a.Next(1))
            .Returns(0);

            //act
            BruteForce.MakeMove(initialGame.Object);

            //assert

            //random was called
            RandomMock.Verify(a => a.Next(1), Times.Once());

            //the center move was made
            GameMock
            .Verify(a => a.Move(Move.Center));
        }
Exemplo n.º 2
0
        public void FindMoveResultsShouldMakeCopyOfGame()
        {
            //arrange
            //setup a mock for the initial object..
            Move[]       initialGameMoveArray = new Move[1];
            Mock <IGame> initialGame          = new Mock <IGame>(MockBehavior.Strict);

            initialGame
            .Setup(a => a.MoveHistory)
            .Returns(initialGameMoveArray);

            TicTacToeFactoryMock
            .Setup(a => a.NewGame(initialGameMoveArray))
            .Returns(GameMock.Object);

            //store the last move made...
            //we need this to test correctly
            Move?lastMoveMade = null;

            GameMock
            .Setup(a => a.Move(It.IsAny <Move>()))
            .Callback <Move>(a => lastMoveMade = a);

            var firstLegalMoves = new Move[]
            {
                Move.Northern,
                Move.Southern
            };
            var secondLegalMoves = new Move[]
            {
                Move.NorthEast,
                Move.NorthWest,
                Move.Western,
                Move.Center
            };
            var thirdLegalMoves = new Move[]
            {
                Move.Eastern,
                Move.SouthEast,
                Move.SouthWest
            };

            //setup the game mock that will drive the rest of our choices
            GameMock
            .Setup(a => a.GetLegalMoves())
            .Returns(() =>
            {
                if (lastMoveMade.HasValue == false)
                {
                    return(firstLegalMoves);
                }
                else if (lastMoveMade.Value == Move.Northern)
                {
                    return(secondLegalMoves);
                }
                else if (lastMoveMade.Value == Move.Southern)
                {
                    return(thirdLegalMoves);
                }

                throw new Exception("test borked");
            });

            //the game is in play for the first two moves...
            GameMock
            .Setup(a => a.GameIsOver)
            .Returns(() =>
            {
                switch (lastMoveMade)
                {
                case Move.Northern:
                case Move.Southern:
                    return(false);

                default:
                    return(true);
                }
            });

            GameMock
            .Setup(a => a.CurrentPlayer)
            .Returns(() =>
            {
                return(Player.X);
            });

            GameMock
            .Setup(a => a.GameState)
            .Returns(() =>
            {
                switch (lastMoveMade)
                {
                case Move.NorthEast:
                    return(GameState.Tie);

                case Move.NorthWest:
                    return(GameState.OWin);

                case Move.Western:
                    return(GameState.Tie);

                case Move.Center:
                    return(GameState.OWin);

                case Move.Eastern:
                    return(GameState.XWin);

                case Move.SouthEast:
                    return(GameState.OWin);

                case Move.SouthWest:
                    return(GameState.Tie);
                }

                throw new Exception();
            });

            //act
            var moveResults = BruteForce.FindMoveResults(initialGame.Object);

            //assert
            //at this point, this is all we need...
        }
Exemplo n.º 3
0
        public void MakeMoveShouldMakeCopyOfGame()
        {
            //arrange
            //setup a mock for the initial object..
            Move[]       initialGameMoveArray = new Move[1];
            Mock <IGame> initialGame          = new Mock <IGame>(MockBehavior.Strict);

            initialGame
            .Setup(a => a.MoveHistory)
            .Returns(initialGameMoveArray);

            initialGame
            .Setup(a => a.GameIsOver)
            .Returns(false);

            initialGame
            .Setup(a => a.Move(It.IsAny <Move>()));

            TicTacToeFactoryMock
            .Setup(a => a.NewGame(initialGameMoveArray))
            .Returns(GameMock.Object);

            //setup the game mock that will drive the rest of our choices
            List <Move> legalMoves = new List <Move>()
            {
                Move.Northern,
                Move.Center,
                Move.Southern
            };

            GameMock
            .Setup(a => a.GetLegalMoves())
            .Returns(legalMoves);

            GameMock
            .Setup(a => a.GameIsOver)
            .Returns(true);

            GameMock
            .Setup(a => a.CurrentPlayer)
            .Returns(Player.O);

            var callCount = 0;

            GameMock
            .Setup(a => a.GameState)
            .Returns(() =>
            {
                callCount++;
                switch (callCount - 1)
                {
                case 0:
                    return(GameState.XWin);

                case 1:
                    return(GameState.XWin);

                default:
                    return(GameState.XWin);
                }
            });

            //setup up our random mock
            RandomMock
            .Setup(a => a.Next(3))
            .Returns(0);

            //act
            BruteForce.MakeMove(initialGame.Object);

            //assert
            TicTacToeFactoryMock.Verify(a => a.NewGame(initialGameMoveArray), Times.Once());
        }