public void CountCapturableByPiece_ThereIsEnemyPieceAround_CapturableCount()
        {
            var target = new CheckersBoard(8);
            var piece  = target.GetSquare(1, 2).CurrentPiece;

            Assert.IsTrue(target.MovePiece(new CheckersMove(piece, target.GetSquare(2, 3))));
            Assert.AreEqual(0, target.CountCapturableByPiece(piece));

            Assert.IsTrue(target.MovePiece(new CheckersMove(piece, target.GetSquare(3, 4))));
            Assert.AreEqual(0, target.CountCapturableByPiece(piece));

            var enemyPiece = target.GetSquare(4, 5).CurrentPiece;

            Assert.AreEqual(1, target.CountCapturableByPiece(enemyPiece));
            Assert.AreEqual(0, target.CountCapturableByPiece(piece));

            Assert.IsTrue(target.MovePiece(new CheckersMove(enemyPiece, target.GetSquare(2, 3))));
            Assert.AreEqual(0, target.CountCapturableByPiece(enemyPiece));

            var otherPiece = target.GetSquare(2, 1).CurrentPiece;

            Assert.IsTrue(target.MovePiece(new CheckersMove(otherPiece, target.GetSquare(1, 2))));
            Assert.AreEqual(1, target.CountCapturableByPiece(otherPiece));
            Assert.AreEqual(0, target.CountCapturableByPiece(enemyPiece));
        }
        public void Evaluate_ChromosomeForwardMoveAndCanCaptureAnotherOne_Fitness2()
        {
            var board  = new CheckersBoard(8);
            var target = new CheckersFitness(board);
            var move   = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(3, 2)
            }, new CheckersSquare(4, 3));

            Assert.IsTrue(board.MovePiece(move));

            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerTwo)
            {
                CurrentSquare = new CheckersSquare(6, 5)
            }, new CheckersSquare(5, 4));
            Assert.IsTrue(board.MovePiece(move));

            var chromosome = new CheckersChromosome(2, 8);

            chromosome.Moves.Clear();
            chromosome.Moves.Add(new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(4, 3)
            }, new CheckersSquare(6, 5)));
            target.Update(chromosome);

            Assert.AreEqual(2, target.Evaluate(chromosome));
        }
        public void CountCapturableByPiece_ThereIsTwoEnemyPieceAround_CapturableCountTwo()
        {
            var target = new CheckersBoard(8);
            var piece  = target.GetSquare(3, 2).CurrentPiece;

            Assert.AreEqual(0, target.CountCapturableByPiece(piece));

            var enemyPiece1 = target.GetSquare(6, 5).CurrentPiece;

            Assert.AreEqual(0, target.CountCapturableByPiece(piece));
            Assert.AreEqual(0, target.CountCapturableByPiece(enemyPiece1));
            Assert.IsTrue(target.MovePiece(new CheckersMove(enemyPiece1, target.GetSquare(5, 4))));
            Assert.AreEqual(0, target.CountCapturableByPiece(piece));
            Assert.AreEqual(0, target.CountCapturableByPiece(enemyPiece1));
            Assert.IsTrue(target.MovePiece(new CheckersMove(enemyPiece1, target.GetSquare(4, 3))));
            Assert.AreEqual(1, target.CountCapturableByPiece(piece));
            Assert.AreEqual(0, target.CountCapturableByPiece(enemyPiece1));

            var enemyPiece2 = target.GetSquare(4, 5).CurrentPiece;

            Assert.AreEqual(1, target.CountCapturableByPiece(piece));
            Assert.AreEqual(0, target.CountCapturableByPiece(enemyPiece2));
            Assert.IsTrue(target.MovePiece(new CheckersMove(enemyPiece2, target.GetSquare(3, 4))));
            Assert.AreEqual(1, target.CountCapturableByPiece(piece));
            Assert.AreEqual(0, target.CountCapturableByPiece(enemyPiece2));
            Assert.IsTrue(target.MovePiece(new CheckersMove(enemyPiece2, target.GetSquare(2, 3))));
            Assert.AreEqual(2, target.CountCapturableByPiece(piece));
            Assert.AreEqual(0, target.CountCapturableByPiece(enemyPiece2));
            Assert.AreEqual(0, target.CountCapturableByPiece(enemyPiece1));
        }
示例#4
0
        public void MovePiece_ValidMove_True()
        {
            var target = new CheckersBoard(8);

            // Move to occupied square to right side.
            var move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(3, 2)
            }, new CheckersSquare(4, 3));

            Assert.IsTrue(target.MovePiece(move));

            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerTwo)
            {
                CurrentSquare = new CheckersSquare(6, 5)
            }, new CheckersSquare(5, 4));
            Assert.IsTrue(target.MovePiece(move));

            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(4, 3)
            }, new CheckersSquare(6, 5));
            Assert.IsTrue(target.MovePiece(move));

            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerTwo)
            {
                CurrentSquare = new CheckersSquare(5, 6)
            }, new CheckersSquare(7, 4));
            Assert.IsTrue(target.MovePiece(move));
        }
        public void CountCapturableByPiece_ThereIsEnemyPieceAroundButCannotBeCaptured_Zero()
        {
            var target = new CheckersBoard(8);
            var piece  = target.GetSquare(1, 2).CurrentPiece;

            Assert.IsTrue(target.MovePiece(new CheckersMove(piece, target.GetSquare(2, 3))));
            Assert.AreEqual(0, target.CountCapturableByPiece(piece));

            Assert.IsTrue(target.MovePiece(new CheckersMove(piece, target.GetSquare(3, 4))));
            Assert.AreEqual(0, target.CountCapturableByPiece(piece));

            Assert.IsFalse(target.MovePiece(new CheckersMove(piece, target.GetSquare(4, 5))));
            Assert.AreEqual(0, target.CountCapturableByPiece(piece));
        }
        public void CountPieceChancesToBeCaptured_CanAndCannotCapture_CapturedCount()
        {
            var target = new CheckersBoard(8);
            var piece  = target.GetSquare(1, 2).CurrentPiece;

            Assert.IsTrue(target.MovePiece(new CheckersMove(piece, target.GetSquare(2, 3))));
            Assert.AreEqual(0, target.CountPieceChancesToBeCaptured(piece));

            Assert.IsTrue(target.MovePiece(new CheckersMove(piece, target.GetSquare(3, 4))));
            Assert.AreEqual(2, target.CountPieceChancesToBeCaptured(piece));

            var enemyPiece = target.GetSquare(4, 5).CurrentPiece;

            Assert.AreEqual(0, target.CountPieceChancesToBeCaptured(enemyPiece));

            Assert.IsFalse(target.MovePiece(new CheckersMove(piece, target.GetSquare(4, 5))));
            Assert.AreEqual(2, target.CountPieceChancesToBeCaptured(piece));
        }
        public void MovePiece_NullMove_Exception()
        {
            var target = new CheckersBoard(10);

            ExceptionAssert.IsThrowing(new ArgumentNullException("move"), () =>
            {
                target.MovePiece(null);
            });
        }
示例#8
0
        public void MovePiece_NullMove_Exception()
        {
            var target = new CheckersBoard(10);

            var actual = Assert.Catch <ArgumentNullException>(() =>
            {
                target.MovePiece(null);
            });

            Assert.AreEqual("move", actual.ParamName);
        }
        public void MovePiece_InvalidMove_False()
        {
            var target = new CheckersBoard(8);

            // Horizontal move.
            var move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(1, 0)
            }, new CheckersSquare(3, 0));

            Assert.IsFalse(target.MovePiece(move));

            // Vertical move.
            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(1, 0)
            }, new CheckersSquare(1, 2));
            Assert.IsFalse(target.MovePiece(move));

            // Back move.
            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(2, 3)
            }, new CheckersSquare(1, 2));
            Assert.IsFalse(target.MovePiece(move));

            // Move to occupied square to right side.
            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(1, 2)
            }, new CheckersSquare(2, 3));
            Assert.IsTrue(target.MovePiece(move));
            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(2, 3)
            }, new CheckersSquare(3, 4));
            Assert.IsTrue(target.MovePiece(move));
            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(3, 4)
            }, new CheckersSquare(4, 5));                                                                                                               // Occupied.
            Assert.IsFalse(target.MovePiece(move));

            // Move to occupied square to left side.
            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(7, 2)
            }, new CheckersSquare(6, 3));
            Assert.IsTrue(target.MovePiece(move));
            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(6, 3)
            }, new CheckersSquare(5, 4));
            Assert.IsTrue(target.MovePiece(move));
            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(5, 4)
            }, new CheckersSquare(6, 5));                                                                                                                           // Occupied.
            Assert.IsFalse(target.MovePiece(move));

            // Move more than 1 square not capturing.
            move = new CheckersMove(new CheckersPiece(CheckersPlayer.PlayerOne)
            {
                CurrentSquare = new CheckersSquare(1, 2)
            }, new CheckersSquare(3, 4));
            Assert.IsFalse(target.MovePiece(move));
        }