Esempio n. 1
0
        public void PieceMoustActuallyMove()
        {
            // Given
            Piece             rook     = new Rook(Colour.White);
            StartSquare       a1       = new StartSquare("a1");
            DestinationSquare a1AsWell = new DestinationSquare("a1");

            long gameId = 101;
            Game target = new Game(gameId);

            target.Board.PutPieceOn(a1, rook);

            // When
            MakeMove command = new MakeMove(gameId, new Move(rook.Code, a1, a1AsWell));
            Action   act     = () =>
            {
                target.MakeMove(command);
            };

            // Then
            var ex = Assert.ThrowsException <BusinessRuleViolationException>(act);

            Assert.AreEqual("Rule Violations: 1 violations have been detected.", ex.Message);
            Assert.AreEqual(1, ex.Violations.Count());
            Assert.IsTrue(ex.Violations.Any(v => v.ViolationMessage == "The start square cannot be the same as the destination square."));
        }
Esempio n. 2
0
        public void MakeMoveRaisesEvent()
        {
            // Given
            Piece             rook = new Rook(Colour.White);
            StartSquare       a1   = new StartSquare("a1");
            DestinationSquare a4   = new DestinationSquare("a4");

            long gameId = 101;
            Game target = new Game(gameId);

            target.Board.PutPieceOn(a1, rook);

            // When
            MakeMove command = new MakeMove(gameId, new Move(rook.Code, a1, a4));

            target.MakeMove(command);

            // Then
            DomainEvent result = target.Events.Single();

            Assert.IsInstanceOfType(result, typeof(MoveMade));
            MoveMade movemade = (MoveMade)result;

            Assert.AreEqual(gameId, movemade.GameId);
            Assert.AreEqual('R', movemade.PieceCode);
            Assert.AreEqual("a1", movemade.StartSquare);
            Assert.AreEqual("a4", movemade.DestinationSquare);
        }
        public static MakeMove FromDTO(long id, DTO.MakeMove dtoCommand)
        {
            string notation = dtoCommand.Notation;

            PieceCode         pieceCode;
            StartSquare       start;
            DestinationSquare destination;

            if ("RNBQK".Contains(notation.First()))
            {
                Enum.TryParse(dtoCommand.Notation.Substring(0, 1), out pieceCode);
                start       = new StartSquare(dtoCommand.Notation.Substring(1, 2));
                destination = new DestinationSquare(dtoCommand.Notation.Substring(4, 2));
            }
            else
            {
                pieceCode   = PieceCode.None;
                start       = new StartSquare(dtoCommand.Notation.Substring(0, 2));
                destination = new DestinationSquare(dtoCommand.Notation.Substring(3, 2));
            }

            var move   = new Move(pieceCode, start, destination);
            var result = new MakeMove(id, move);

            return(result);
        }
Esempio n. 4
0
        public void IsNOVerticalMove()
        {
            StartSquare       b2 = new StartSquare("b2");
            DestinationSquare d3 = new DestinationSquare("d3");
            Move move            = new Move(PieceCode.R, b2, d3);

            bool result = move.IsVerticalMove();

            Assert.AreEqual(false, result);
        }
Esempio n. 5
0
        public void IsVerticalMove()
        {
            StartSquare       b2 = new StartSquare("b2");
            DestinationSquare b7 = new DestinationSquare("b7");
            Move move            = new Move(PieceCode.R, b2, b7);

            bool result = move.IsVerticalMove();

            Assert.AreEqual(true, result);
        }
Esempio n. 6
0
        public void IsHorizontalMove()
        {
            StartSquare       b2 = new StartSquare("b2");
            DestinationSquare d2 = new DestinationSquare("d2");
            Move move            = new Move(PieceCode.R, b2, d2);

            bool result = move.IsHorizontalMove();

            Assert.AreEqual(true, result);
        }
Esempio n. 7
0
        public void AMoveHasAPieceStartAndDestination()
        {
            StartSquare       e1 = new StartSquare("e1");
            DestinationSquare d2 = new DestinationSquare("d2");

            Move move = new Move(PieceCode.K, e1, d2);

            Assert.AreEqual(PieceCode.K, move.PieceCode);
            Assert.AreEqual(e1, move.StartSquare);
            Assert.AreEqual(d2, move.DestinationSquare);
        }
        public void PieceMustAcuallyMove()
        {
            Piece             rook = new Rook(Colour.Black);
            StartSquare       d5   = new StartSquare("d5");
            DestinationSquare d8   = new DestinationSquare("d8");
            Move move = new Move(rook.Code, d5, d8);

            BusinessRule target = new PieceMustActuallyMove(move);

            IEnumerable <BusinessRuleViolation> violations = target.CheckRule();

            Assert.IsNotNull(violations);
            Assert.IsFalse(violations.Any());
        }
        public void PieceMustAcuallyMove_ButDoesNot()
        {
            Piece             rook     = new Rook(Colour.Black);
            StartSquare       d5       = new StartSquare("d5");
            DestinationSquare d5AsWell = new DestinationSquare("d5");
            Move move = new Move(rook.Code, d5, d5AsWell);

            BusinessRule target = new PieceMustActuallyMove(move);

            IEnumerable <BusinessRuleViolation> violations = target.CheckRule();

            Assert.AreEqual(1, violations.Count());
            Assert.IsTrue(violations.Any(v => v.ViolationMessage ==
                                         "The start square cannot be the same as the destination square."));
        }
Esempio n. 10
0
        public void MakeMoveMovesPieceOnBoard()
        {
            // Given
            Piece             rook = new Rook(Colour.White);
            StartSquare       a1   = new StartSquare("a1");
            DestinationSquare a4   = new DestinationSquare("a4");

            Game target = new Game(1);

            target.Board.PutPieceOn(a1, rook);

            // When
            MakeMove command = new MakeMove(1, new Move(rook.Code, a1, a4));

            target.MakeMove(command);

            // Then
            Assert.IsTrue(target.Board.IsEmptyAt(a1), "rook should not be on a1");
            Assert.IsTrue(target.Board.HasThisPieceOn(a4, rook.Code), "rook should be on a4");
        }
        public void ThenItCanMoveToAllSAndNotToAllS(string go, string nogo, Table table)
        {
            foreach (TableRow row in table.Rows)
            {
                foreach (var file in "abcdefgh")
                {
                    string            expectedResult = row[file.ToString()];
                    int               rank           = int.Parse(row[0]);
                    DestinationSquare destination    = new DestinationSquare(file, rank);
                    Move              move           = new Move(_piece.Code, _startSquare, destination);

                    if (expectedResult == go)
                    {
                        bool actualResult = _piece.IsValidMove(move);
                        Assert.AreEqual(true, actualResult, $"it should be allowd to go to {file}{rank}");
                    }
                    else if (expectedResult == nogo)
                    {
                        bool actualResult = _piece.IsValidMove(move);
                        Assert.AreEqual(false, actualResult, $"it should not be allowd to go to {file}{rank}");
                    }
                }
            }
        }