예제 #1
0
 public void KnightNot()
 {
     var from = new Position(3, 3);
     var s = new ChessState();
     var m = new KnightMoveTemplate();
     Assert.IsFalse(m.CanApply(from, new Position(2, 2), s));
 }
예제 #2
0
        public IPiece this[Position index]
        {
            get
            {
                try
                {
                    return this[index.File, index.Rank];
                }
                catch
                {
                    throw new ArgumentOutOfRangeException("index");
                }
            }

            set
            {
                try
                {
                    this[index.File, index.Rank] = value;
                }
                catch
                {
                    throw new ArgumentOutOfRangeException("index");
                }
            }
        }
예제 #3
0
 public void KnightDownLeft()
 {
     var from = new Position(3, 3);
     var s = new ChessState();
     var m = new KnightMoveTemplate();
     Assert.IsTrue(m.CanApply(from, new Position(2, 1), s));
     Assert.IsTrue(m.CanApply(from, new Position(1, 2), s));
 }
예제 #4
0
 public void KnightUpRight()
 {
     var from = new Position(3, 3);
     var s = new ChessState();
     var m = new KnightMoveTemplate();
     Assert.IsTrue(m.CanApply(from, new Position(4, 5), s));
     Assert.IsTrue(m.CanApply(from, new Position(5, 4), s));
 }
예제 #5
0
 public void KnightCanSkip()
 {
     var from = new Position(3, 3);
     var s = new ChessState();
     s[new Position(3, 4)] = new ChessPiece();
     var m = new KnightMoveTemplate();
     Assert.IsTrue(m.CanApply(from, new Position(4, 5), s));
 }
예제 #6
0
        public void MoveTest()
        {
            var p = new Piece();
            var pos = new Position(1, 1);
            p.Move(pos);

            Assert.AreEqual(1, p.PathLength);
            Assert.AreEqual(pos, p.Pos);
        }
예제 #7
0
 public void PromotionWrongType()
 {
     var from = new Position("a7");
     var s = new ChessState();
     var piece = new ChessPiece { Player = new Player(1) };
     s[from] = piece;
     var m = new Promotion(new PawnMoveTemplate(), new[] { PieceTypes.Knight, PieceTypes.Bishop, PieceTypes.Rook, PieceTypes.Queen });
     Assert.IsFalse(m.CanApply(from, new Position("a8"), s, PieceTypes.Pawn));
 }
예제 #8
0
        public void GetPossibleMove()
        {
            var pos = new Position(1, 1);
            var s = new ChessState();
            var p = new ChessPiece();
            p.Move(new Position(2, 2));
            p.PossibleMoves.Add(new MockTemplate());

            Assert.IsNotNull(p.GetPossibleMove(pos, s, PieceTypes.Pawn));
        }
예제 #9
0
        public void NewEmptyPiece()
        {
            var s = new GameState(5, 5);
            var pos = new Position(3, 3);
            s[pos] = new Piece();

            s.NewEmptyPiece(pos);

            Assert.IsTrue(s[pos].IsEmpty);
        }
예제 #10
0
        public void UnmoveTest()
        {
            var to = new Position(1, 1);
            var p = new Piece();
            p.Move(to);
            p.Unmove();

            Assert.AreEqual(0, p.PathLength);
            Assert.AreEqual(null, p.Pos);
        }
예제 #11
0
        public override bool CanApply(Position from, Position to, IGameState state)
        {
            for (int rank = Math.Min(from.Rank, to.Rank) + 1; rank < Math.Max(from.Rank, to.Rank); ++rank)
                Path.Add(new Position(from.File, rank));

            for (int file = Math.Min(from.File, to.File) + 1; file < Math.Max(from.File, to.File); ++file)
                Path.Add(new Position(file, from.Rank));

            return base.CanApply(from, to, state);
        }
예제 #12
0
        public void CanNotAttack()
        {
            var pos = new Position(1, 1);
            var s = new ChessState();
            var p = new ChessPiece();
            p.Move(new Position(2, 2));
            p.PossibleMoves.Add(new MockTemplate(null, false));

            Assert.IsFalse(p.CanAttack(pos, s));
        }
예제 #13
0
        public void IsUnderAttackEmpty()
        {
            var s = new ChessState();
            var players = new List<Player> { new Player(1), new Player(2) };
            var from = new Position(1, 1);
            var to = new Position(2, 2);
            s[from] = ChessPieceFactory.CreatePiece('K', new EnglishFENPresentation(), players);
            s[from].Move(from);

            Assert.IsTrue(s.IsUnderAttack(to, players[1]));
        }
예제 #14
0
        public void AddMove()
        {
            var g    = new MockGame();
            var from = new Position(1, 1);
            var to   = new Position(2, 2);
            var m    = new ConcreteMove(from, to);

            g.AddMove(m);

            Assert.AreEqual(1, g.MovesCount);
        }
예제 #15
0
 public static int FileDistance(Position from, Position to)
 {
     try
     {
         return Math.Abs(from.File - to.File);
     }
     catch
     {
         throw new ArgumentNullException("File of a position is undefined.", new Exception());
     }
 }
예제 #16
0
        public void EnPassantFar()
        {
            var from = new Position("c5");
            var to = new Position("a3");
            var s = new ChessState();
            s.EnPassant = to;
            s[from] = new ChessPiece { Player = new Player(2) };
            var m = new EnPassantTemplate();

            Assert.IsFalse(m.CanApply(from, to, s));
        }
예제 #17
0
        public void EnPassantSetting()
        {
            var from = new Position("a2");
            var to = new Position("a4");
            var s = new ChessState();
            s[from] = new ChessPiece { Player = new Player(1) };
            var m = new PawnConcreteMove(from, to);

            m.Apply(s);

            Assert.AreEqual(new Position("a3"), s.EnPassant);
        }
예제 #18
0
        public void IsKingOpen()
        {
            var s = new ChessState();
            var players = new List<Player> {new Player(1), new Player(2)};
            var from = new Position(1, 1);
            var to = new Position(3, 3);
            s[from] = ChessPieceFactory.CreatePiece('K', new EnglishFENPresentation(), players);
            s[to] = ChessPieceFactory.CreatePiece('q', new EnglishFENPresentation(), players);
            s[from].Move(from);
            s[to].Move(to);

            Assert.IsTrue(s.IsKingOpen(players[0]));
        }
예제 #19
0
        public void NoPossibleMoves()
        {
            var s = new ChessState();
            var from = new Position(1, 1);
            var to = new Position(1, 3);
            var p = new ChessPiece();
            s[from] = p;
            s[from].Move(from);

            p.PossibleMoves.Add(new MockTemplate(1));

            Assert.IsNull(p.GetPossibleMove(to, s, PieceTypes.Pawn));
        }
예제 #20
0
        public void CaptureMove()
        {
            var s = new GameState(5, 5);
            var from = new Position(1, 1);
            var to = new Position(2, 2);
            s[from] = new Piece();
            var movingPiece = s[from];
            s[to] = new Piece();
            var m = new ConcreteMove(from, to);

            m.Apply(s);

            Assert.AreEqual(s[to], movingPiece);
        }
예제 #21
0
        public void PromotionApply()
        {
            var from = new Position("a7");
            var to   = new Position("a8");
            var piece = new ChessPiece { Type = PieceTypes.Pawn, Player = new Player(1) };
            var s = new ChessState();
                s[from] = piece;
            var m = new Promotion(new PawnMoveTemplate(), new[] { PieceTypes.Knight, PieceTypes.Bishop, PieceTypes.Rook, PieceTypes.Queen });
            var concretePromotion = m.Concretize(from, to, PieceTypes.Queen);

            concretePromotion.Apply(s);

            Assert.AreEqual(PieceTypes.Queen, piece.Type);
        }
예제 #22
0
        public void GetConcreteFromTemplate()
        {
            var s = new GameState(5, 5);
            var from = new Position(1, 1);
            var to = new Position(2, 2);
            s[from] = new Piece();
            var movingPiece = s[from];
            var m = new MockTemplate().Concretize(from, to);

            m.Apply(s);

            Assert.IsTrue(s[from].IsEmpty);
            Assert.AreNotEqual(s[from], movingPiece);
            Assert.AreEqual(s[to], movingPiece);
        }
예제 #23
0
        public void CanAttack()
        {
            var s = new ChessState();
            var from = new Position(1, 1);
            var to   = new Position(2, 2);
            var p = new ChessPiece();
            s[from] = p;
            s[from].Move(from);
            s[to] = new ChessPiece();
            s[to].Move(to);

            s[from].PossibleMoves.Add(new MockTemplate(null, true));

            Assert.IsTrue(p.CanAttack(to, s));
        }
예제 #24
0
        public void MakeMoveIsStateChanged()
        {
            var g = new MockGame();
            var from = new Position(1, 1);
            var to = new Position(2, 2);
            var p = new Piece();
            var m = new ConcreteMove(from, to);
            g.State[from] = p;
            p.Move(from);
            g.AddMove(m);

            g.MakeMove();

            Assert.AreEqual(p, g.State[to]);
        }
예제 #25
0
        public void CastlingKingside()
        {
            var c = new CastlingTemplate(CastlingTemplate.CastlingType.Kingside);

            var from = new Position(5, 1);
            var to   = new Position(7, 1);
            var rook = new Position(8, 1);

            var s = new ChessState();
            s[rook] = new ChessPiece {Type = PieceTypes.Rook};
            s[rook].Move(rook);
            s[from] = new ChessPiece {Type = PieceTypes.King};
            s[from].Move(from);

            Assert.IsTrue(c.CanApply(from, to, s));
        }
예제 #26
0
        public void EnPassantCapture()
        {
            var enmy = new Position("a4");
            var from = new Position("b4");
            var to = new Position("a3");
            var s = new ChessState();
            s.EnPassant = to;
            s[from] = new ChessPiece { Player = new Player(2) };
            s[from].Move(from);
            s[enmy] = new ChessPiece();
            s[enmy].Move(enmy);
            var m = new EnPassantConcrete(from, to);

            Assert.IsFalse(s[enmy].IsEmpty);
            m.Apply(s);
            Assert.IsTrue(s[enmy].IsEmpty);
        }
예제 #27
0
        public void MakeMove()
        {
            var g = new MockGame();
            var from = new Position(1, 1);
            var to = new Position(2, 2);
            var p  = new Piece();
            var m = new ConcreteMove(from, to);
            g.State[from] = p;
            p.Move(from);
            g.AddMove(m);

            bool result = g.MakeMove();

            Assert.AreEqual(1, g.MovesCount);
            Assert.AreEqual(1, g.CurrentMove);
            Assert.IsTrue(result);
        }
예제 #28
0
        public void CastlingBarrierQ()
        {
            var c = new CastlingTemplate(CastlingTemplate.CastlingType.Queenside);

            var from = new Position(5, 1);
            var to = new Position(3, 1);
            var rook = new Position(1, 1);
            var enmy = new Position(2, 1);
            var white = new Player(1);

            var s = new ChessState();
            s[rook] = new ChessPiece { Type = PieceTypes.Rook, Player = white };
            s[rook].Move(rook);
            s[from] = new ChessPiece { Type = PieceTypes.King, Player = white };
            s[from].Move(from);
            s[enmy] = new ChessPiece { Type = PieceTypes.Rook, Player = white };
            s[enmy].Move(enmy);

            Assert.IsFalse(c.CanApply(from, to, s));
        }
예제 #29
0
        public override bool CanApply(Position from, Position to, IGameState state)
        {
            if (Position.FileDistance(from, to) == Position.RankDistance(from, to))
            {
                int file, rank, maxFile;

                // left to right
                if (from.File < to.File)
                {
                    file = from.File + 1;
                    maxFile = to.File;

                    // bottom to top
                    if (from.Rank < to.Rank)
                        for (rank = from.Rank + 1; file < maxFile; ++file, ++rank)
                            Path.Add(new Position(file, rank));
                    // top to bottom
                    else
                        for (rank = from.Rank - 1; file < maxFile; ++file, --rank)
                            Path.Add(new Position(file, rank));
                }
                // right to left
                else
                {
                    file = to.File + 1;
                    maxFile = from.File;

                    // bottom to up
                    if (from.Rank < to.Rank)
                        for (rank = to.Rank - 1; file < maxFile; ++file, --rank)
                            Path.Add(new Position(file, rank));
                    // up to bottom
                    else
                        for (rank = to.Rank + 1; file < maxFile; ++file, ++rank)
                            Path.Add(new Position(file, rank));
                }
                return base.CanApply(from, to, state);
            }

            return false;
        }
예제 #30
0
 public virtual void NewEmptyPiece(Position pos)
 {
     var p = new Piece {IsEmpty = true};
     this[pos] = p;
     p.Move(pos);
 }