コード例 #1
0
        public void AssignCellOwnerTest()
        {
            // make sure that a cell can be assigned as either computer or human
            // also be sure that only cell within range may be set
            // many TDD purists argue these are all separate tests

            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            ticTacToeGame.IdentifyCellOwner(0, 0).ShouldBe(CellOwners.Open); // _ticTacToeCells should now be initialized to open

            // act
            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(0, 1, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(0, 11, CellOwners.Computer); // should not do anything
            ticTacToeGame.AssignCellOwner(0, -1, CellOwners.Computer); // should not do anything

            // assert
            ticTacToeGame.IdentifyCellOwner(0, 0).ShouldBe(CellOwners.Human);
            ticTacToeGame.IdentifyCellOwner(0, 1).ShouldBe(CellOwners.Computer);
            ticTacToeGame.IdentifyCellOwner(0, 2).ShouldBe(CellOwners.Open);
            ticTacToeGame.IdentifyCellOwner(0, 11).ShouldBe(CellOwners.Error); // out of bounds
            ticTacToeGame.IdentifyCellOwner(0, -1).ShouldBe(CellOwners.Error);
            ticTacToeGame.IdentifyCellOwner(0, 3).ShouldBe(CellOwners.Open);
            ticTacToeGame.IdentifyCellOwner(3, 0).ShouldBe(CellOwners.Open);

            ticTacToeGame.CheckForWinner().ShouldBeFalse(); // no winner yet
            ticTacToeGame.Winner.ShouldBe(CellOwners.Open); // winner property should now be Open
            ticTacToeGame.IdentifyWinner().ShouldBeEmpty(); // there is no winner
        }
コード例 #2
0
        public void NoWinnerFound()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            // act

            /*
             *      X O X
             *      X O X
             *      O X O
             */
            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(0, 1, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(0, 2, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(2, 0, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(2, 1, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(2, 2, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(1, 0, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 1, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(1, 2, CellOwners.Human);

            // assert
            ticTacToeGame.CheckForWinner().ShouldBeFalse(); // no winner
            ticTacToeGame.Winner.ShouldBe(CellOwners.Open); // winner property should be Open
            ticTacToeGame.IdentifyWinner().ShouldBeEmpty(); // there is no winner
        }
コード例 #3
0
        public void AutoPlayComputerWinnerTest()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            // act
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(0, 1, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(0, 3, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(1, 0, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(1, 2, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(1, 3, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(3, 4, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(4, 1, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(4, 2, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(4, 3, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();

            // assert
            ticTacToeGame.CheckForWinner().ShouldBeTrue();
            ticTacToeGame.Winner.ShouldBe(CellOwners.Computer);
        }
コード例 #4
0
        public void WinnerHuman()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 0, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(0, 1, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 1, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(0, 2, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 2, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(0, 3, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 3, CellOwners.Computer);
            ticTacToeGame.CheckForWinner().ShouldBeFalse(); // no winner --- yet

            ticTacToeGame.AssignCellOwner(0, 4, CellOwners.Human);
            ticTacToeGame.CheckForWinner().ShouldBeTrue();
            ticTacToeGame.Winner.ShouldBe(CellOwners.Human); // winner property should be Human
            ticTacToeGame.IdentifyWinner().ShouldBe(ticTacToeGame.PlayerName);

            ticTacToeGame.PlayerName = ""; // should not happen, but it could!
            ticTacToeGame.IdentifyWinner().ShouldBe("Unnamed Human");
        }
コード例 #5
0
        public void WinnerComputer()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            // act

            /*
             *      X O X
             *        O X
             *        O
             */
            ticTacToeGame.AssignCellOwner(0, 3, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(0, 2, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 1, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(1, 2, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(3, 3, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(1, 4, CellOwners.Human);
            ticTacToeGame.CheckForWinner().ShouldBeFalse(); // no winner --- yet

            ticTacToeGame.AssignCellOwner(4, 4, CellOwners.Computer);
            ticTacToeGame.CheckForWinner().ShouldBeTrue();
            ticTacToeGame.Winner.ShouldBe(CellOwners.Computer);
            ticTacToeGame.IdentifyWinner().ShouldBe("Computer");
        }
コード例 #6
0
        public void WinnerComputer()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            // act
            ticTacToeGame.AssignCellOwner(0, 2, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(1, 1, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(2, 0, CellOwners.Computer);

            // assert
            ticTacToeGame.CheckForWinner().ShouldBeTrue(); // winner !
            ticTacToeGame.Winner.ShouldBe(CellOwners.Computer);
            ticTacToeGame.IdentifyWinner().ShouldBe("Computer");
        }
コード例 #7
0
        public void WinnerHuman()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            // act
            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 1, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(2, 2, CellOwners.Human);

            // assert
            ticTacToeGame.CheckForWinner().ShouldBeTrue(); // winner !
            ticTacToeGame.Winner.ShouldBe(CellOwners.Human);
            ticTacToeGame.IdentifyWinner().ShouldBe("Unnamed Human");
        }
コード例 #8
0
        public void IdentifyWinnerHumanTest()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            // act
            ticTacToeGame.PlayerName = "Prof Reynolds";
            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 1, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(2, 2, CellOwners.Human);
            ticTacToeGame.CheckForWinner();

            // assert
            ticTacToeGame.IdentifyWinner().ShouldBe("Prof Reynolds");
        }
コード例 #9
0
        public void AutoPlayComputerWinnerTest()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            // act
            ticTacToeGame.AutoPlayComputer();                      // 1,1 first good move
            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();                      // 0,2 third in good move list because second good move blocked by human
            ticTacToeGame.AssignCellOwner(2, 0, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();                      // 1,0 to block winning move from human
            ticTacToeGame.AssignCellOwner(1, 0, CellOwners.Human); // this overwrites the previous move by the computer???
            ticTacToeGame.AutoPlayComputer();                      // 2,2 fifth in good move list

            // assert
            ticTacToeGame.CheckForWinner().ShouldBeTrue();
            ticTacToeGame.Winner.ShouldBe(CellOwners.Computer);
        }
コード例 #10
0
        public void AutoPlayComputerTest()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            // act
            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(2, 2, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(2, 0, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer();
            ticTacToeGame.AssignCellOwner(1, 0, CellOwners.Human);

            // assert
            ticTacToeGame.CheckForWinner().ShouldBeTrue();
            ticTacToeGame.Winner.ShouldBe(CellOwners.Human);
            ticTacToeGame.IdentifyWinner().ShouldBe("Human");
        }
コード例 #11
0
        public void AssignCellOwnerTest()
        {
            // make sure that a cell can be assigned as either computer or human
            // also be sure that only cell within range may be set
            // many TDD purists argue these are all separate tests

            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            // act and assert - assign a cell and check that it is changed
            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Human);
            ticTacToeGame.IdentifyCellOwner(0, 0).ShouldBe(CellOwners.Human);

            // act and assert - assign a cell as a computer
            ticTacToeGame.AssignCellOwner(0, 1, CellOwners.Computer);
            ticTacToeGame.IdentifyCellOwner(0, 1).ShouldBe(CellOwners.Computer);

            // act and assert - no fault when bad assignment is attempted
            ticTacToeGame.AssignCellOwner(0, 11, CellOwners.Computer); // should not do anything
            ticTacToeGame.AssignCellOwner(0, -1, CellOwners.Computer); // should not do anything
        }
コード例 #12
0
        public void AutoPlayComputerWinnerTest()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.ResetGrid();

            // act
            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer(); // 0-4
            ticTacToeGame.AssignCellOwner(4, 4, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer(); // 4-0
            ticTacToeGame.AssignCellOwner(2, 0, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer(); // 1-1
            ticTacToeGame.AssignCellOwner(2, 1, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer(); // 1-3
            ticTacToeGame.AssignCellOwner(2, 4, CellOwners.Human);
            ticTacToeGame.AutoPlayComputer(); // 3-1
            ticTacToeGame.CheckForWinner();   // 1-3

            // assert
            ticTacToeGame.CheckForWinner().ShouldBeTrue();
            ticTacToeGame.Winner.ShouldBe(CellOwners.Computer);
        }
コード例 #13
0
        public void WinnerComputer()
        {
            // arrange
            var ticTacToeGame = new TicTacToeGame();

            ticTacToeGame.GridSize = 5; //this code was added so it would test the 5x5 version
            ticTacToeGame.ResetGrid();
            ticTacToeGame.AssignCellOwner(1, 0, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(0, 0, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 1, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(0, 1, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 2, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(0, 2, CellOwners.Human);
            ticTacToeGame.AssignCellOwner(1, 3, CellOwners.Computer);
            ticTacToeGame.AssignCellOwner(0, 3, CellOwners.Human);
            ticTacToeGame.CheckForWinner().ShouldBeFalse(); // no winner --- yet
            ticTacToeGame.AssignCellOwner(1, 4, CellOwners.Computer);
            ticTacToeGame.CheckForWinner().ShouldBeTrue();
            ticTacToeGame.Winner.ShouldBe(CellOwners.Computer);
            ticTacToeGame.IdentifyWinner().ShouldBe("Computer");
        }