Example #1
0
 public void BeforeTests()
 {
     _board = new Board(8);
     _rook = new Rook(PlayerColor.White);
     _king = new King(PlayerColor.Black);
     _referee = new Referee(new MoveAttemptHandler());
 }
Example #2
0
 public static Piece GetType(string s, ChessColor color)
 {
     Piece p;
     switch (s)
     {
         case "Bishop":
             p = new Bishop(color);
             break;
         case "King":
             p = new King(color);
             break;
         case "Knight":
             p = new Knight(color);
             break;
         case "Pawn":
             p = new Pawn(color);
             break;
         case "Queen":
             p = new Queen(color);
             break;
         case "Rook":
             p = new Rook(color);
             break;
         default:
             throw new Exception("Piece unknown : " + s);
     }
     return p;
 }
Example #3
0
        public void Whether_Rook_CantMoveToIncorrectSquare_On_CanMoveTo()
        {
            var board = new Board();
            var rook = new Rook(new Square(4, 4), board, PieceColor.White);

            Assert.That(rook.CanMoveTo(new Square(5, 5)), Is.False);
        }
Example #4
0
        public void Whether_Rook_CantMoveOutsideTheBoard_On_CanMoveTo()
        {
            var board = new Board();
            var rook = new Rook(new Square(1, 1), board, PieceColor.White);

            Assert.That(rook.PossibleMoves().Count() == 14);
            Assert.That(rook.CanMoveTo(new Square(-1, 1)), Is.False);
        }
Example #5
0
        public void Whether_Rook_CantJumpOverAnotherPiece_On_CanMoveTo(PieceColor pieceColor)
        {
            var board = new Board();
            var rook = new Rook(new Square(4, 4), board, PieceColor.White);
            var knight = new Knight(new Square(4, 5), board, pieceColor);

            Assert.That(rook.CanMoveTo(new Square(4, 6)), Is.False);
        }
Example #6
0
        public void Whether_Rook_CantMoveToOccupiedSquare_On_CanMoveTo()
        {
            var board = new Board();
            var rook = new Rook(new Square(1, 1), board, PieceColor.White);
            var anotherKnight = new Knight(new Square(1, 6), board, PieceColor.White);

            Assert.That(rook.CanMoveTo(new Square(1, 6)), Is.False);
        }
Example #7
0
        public void Whether_King_CantEscapeFromRookOnTheSameRank_On_CanMoveTo()
        {
            var board = new Board();
            var king = new King(new Square(4, 4), board, PieceColor.White);
            var bishop = new Rook(new Square(4, 7), board, PieceColor.Black);

            Assert.That(king.CanMoveTo(new Square(4, 3)), Is.False);
        }
Example #8
0
        public void Whether_Rook_CanMoveToPossibleSquare_On_CanMoveTo(int offsetX, int offsetY)
        {
            var board = new Board();
            var rook = new Rook(new Square(4, 4), board, PieceColor.White);

            Assert.That(rook.CanMoveTo(
                new Square(rook.Position.XPosition + offsetX,
                           rook.Position.YPosition + offsetY)));
        }
Example #9
0
        public override ChessPiece Clone(Board board)
        {
            var clone = new Rook(board, Color)
                {
                    MovingStrategy = MovingStrategy.Clone(board)
                };

            return clone;
        }
Example #10
0
        public void Whether_Rook_ReturnsCorrectMoves_On_PossibleMoves()
        {
            var board = new Board();
            var rook = new Rook(new Square(4, 4), board, PieceColor.White);

            var possibleMoves = rook.PossibleMoves();

            Assert.That(possibleMoves != null);
            Assert.That(possibleMoves.Count() == 14);
        }
Example #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PlayerChess"/> class.
        /// </summary>
        /// <param name="color">The piece color.</param>
        /// <param name="gameboard">The gameboard.</param>
        public PlayerChess(IPieceColor color, Gameboard gameboard)
            : base(color)
        {
            IPiece newPiece;
            int offset = 0;

            this.gameboard = gameboard;
            this.selectedPiece = null;

            if (color == IPieceColor.White)
            {
                offset = 7;
            }
            else if (color == IPieceColor.Black)
            {
                offset = 0;
            }

            for (int i = 0; i < 8; i++)
            {
                newPiece = new Pawn(color, gameboard, new Position(i, Math.Abs(offset - 1)));
                newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
                this.PieceList.Add(newPiece);
            }

            newPiece = new Rook(color, gameboard, new Position(0, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Rook(color, gameboard, new Position(7, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Knight(color, gameboard, new Position(1, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Knight(color, gameboard, new Position(6, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Bishop(color, gameboard, new Position(2, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Bishop(color, gameboard, new Position(5, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Queen(color, gameboard, new Position(3, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new King(color, gameboard, new Position(4, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            this.King = newPiece;
        }
Example #12
0
        public void TestFromFenValid()
        {
            const string FEN_STRING = "pQ4N1/3k3R/1r4n1/KbBbBppP/8/8/q7/7n";
            FENSerializer ser = new FENSerializer();
            Board expected = new Board(null);
            Board actual;

            // Setup
            expected['8', 'A'] = new Pawn('b');
            expected['8', 'B'] = new Queen('w');
            expected['8', 'G'] = new Knight('w');

            expected['7', 'D'] = new King('b');
            expected['7', 'H'] = new Rook('w');

            expected['6', 'B'] = new Rook('b');
            expected['6', 'G'] = new Knight('b');

            expected['5', 'A'] = new King('w');
            expected['5', 'B'] = new Bishop('b');
            expected['5', 'C'] = new Bishop('w');
            expected['5', 'D'] = new Bishop('b');
            expected['5', 'E'] = new Bishop('w');
            expected['5', 'F'] = new Pawn('b');
            expected['5', 'G'] = new Pawn('b');
            expected['5', 'H'] = new Pawn('w');

            expected['2', 'A'] = new Queen('b');
            expected['1', 'H'] = new Knight('b');

            // Act
            actual = ser.Deserialize(FEN_STRING);

            // Test
            for (int file = 0; file < Board.NUM_FILES; file++)
            {
                for (int row = 0; row < Board.NUM_ROWS; row++)
                {
                    Piece expectedPiece = expected[row, file];
                    Piece actualPiece = actual[row, file];
                    bool areEqual = Piece.AreEqual(expectedPiece, actualPiece);

                    Assert.IsTrue(areEqual);
                }
            }
        }
Example #13
0
        public void placeBlackFigures()
        {
            Figure[] pawns = new Figure[8];
            Figure[] otherFigures = new Figure[8];
            char currentChar = 'A';
            char charPositionPawn = 'A';
            for (int i = 0; i < 8; i++)
            {

                pawns[i] = new Pawn(charPositionPawn, 2, false);
                charPositionPawn++;


                if ((currentChar == 'A') || (currentChar == 'H'))
                {
                    otherFigures[i] = new Rook(currentChar, 1, false);
                }
                else if ((currentChar == 'B') || (currentChar == 'G'))
                {
                    otherFigures[i] = new Knight(currentChar, 1, false);
                }
                else if ((currentChar == 'C') || (currentChar == 'F'))
                {
                    otherFigures[i] = new Bishop(currentChar, 1, false);
                }
                else if (currentChar == 'E')
                {
                    otherFigures[i] = new King(currentChar, 1, false);
                }
                else
                {
                    otherFigures[i] = new Queen(currentChar, 1, false);

                }

                currentChar++;
            }
            for (int i = 0; i < 8; i++)
            {
                placeFigure(pawns[i]);
                placeFigure(otherFigures[i]);
            }


        }
Example #14
0
        public static void Main(String[] args)
        {
            //while (true)
            //{
            //int X = ShowMenu();
            //switch (X)
            //    {
            //        case 1:
            //        PlayGame(new CheckersFactory());
            //    break;
            //    case 2:
            //    PlayGame(new ChessFactory());
            //    break;
            //}
            //Position p = new Position(2, 3);
            //Pawn pp = new Pawn(FigureColor.White, FigureType.Pawn, p);

            //-------------------------Test figures
            //King is ok
            //Queen is ok
            //Rook is ok
            //Knight is ok
            //Bishop is ok
            //Pawn is ok

            Console.WriteLine("Create new rook at (5, 7)");
            Console.WriteLine("Valid moves are:");
            Rook myRook = new Rook(FigureColor.White, FigureType.Rook, new Position(5, 7));
            foreach (Position pos in myRook.ValidMoves)
            {
                Console.WriteLine("{0}, {1}", pos.X, pos.Y);
            }
            Console.WriteLine("Move to new position (5,5)");
            Console.WriteLine("New Valid moves are:");
            myRook.Move(new Position(5,5));
            foreach (Position pos in myRook.ValidMoves)
            {
                Console.WriteLine("{0}, {1}", pos.X, pos.Y);
            }

            //-------------------------Test figures
        }
Example #15
0
        public static void TestInEquality_OneIsNull()
        {
            Piece piece1 = new Rook(Player.White);
            Piece piece2 = null;
            Assert.AreNotEqual(piece1, piece2, "piece1 and piece2 are equal");
            Assert.False(piece1.Equals(piece2), "piece1.Equals(piece2) should be false");
            Assert.False(piece1 == piece2, "piece1 == piece2 should be false");
            Assert.False(piece2 == piece1, "piece2 == piece1 should be false");
            Assert.True(piece1 != piece2, "piece1 != piece2 should be True");
            Assert.True(piece2 != piece1, "piece2 != piece1 should be True");

            piece1 = null;
            piece2 = new Bishop(Player.Black);
            Assert.AreNotEqual(piece1, piece2, "piece1 and piece2 are equal");
            Assert.False(piece2.Equals(piece1), "piece1.Equals(piece2) should be false");
            Assert.False(piece1 == piece2, "piece1 == piece2 should be false");
            Assert.False(piece2 == piece1, "piece2 == piece1 should be false");
            Assert.True(piece1 != piece2, "piece1 != piece2 should be True");
            Assert.True(piece2 != piece1, "piece2 != piece1 should be True");
        }
            private static void GetPieceForFENChar(char fench,
								ChessSide
								whites,
								ChessSide
								blacks,
								int rank,
								int file,
								out ChessPiece
								piece)
            {
                ColorType color;
                ChessSide myside, oppside;
                if (Char.IsUpper (fench))
                  {
                      color = ColorType.WHITE;
                      myside = whites;
                      oppside = blacks;
                  }
                else
                  {
                      color = ColorType.BLACK;
                      myside = blacks;
                      oppside = whites;
                  }

                char ch = Char.ToLower (fench);
                switch (ch)
                  {
                  case 'p':
                      piece = new Pawn (color, rank, file,
                                myside, oppside);
                      break;
                  case 'k':
                      piece = new King (color, rank, file,
                                myside, oppside);
                      break;
                  case 'q':
                      piece = new Queen (color, rank,
                                 file, myside,
                                 oppside);
                      break;
                  case 'b':
                      piece = new Bishop (color, rank,
                                  file, myside,
                                  oppside);
                      break;
                  case 's':
                      piece = new Knight (color, rank,
                                  file, myside,
                                  oppside);
                      break;
                  case 'n':
                      piece = new Knight (color, rank,
                                  file, myside,
                                  oppside);
                      break;
                  case 'r':
                      piece = new Rook (color, rank, file,
                                myside, oppside);
                      break;
                  default:
                      throw new
                          ChessException
                          (Catalog.GetString("Invalid piece type ") +
                           ch);
                  }
            }
Example #17
0
 public void BeforeEachTest()
 {
     this.Target =new Rook();
     this.MovesFrom11 = Target.GetMovesFrom(new BoardCoordinate(1, 1));
 }
Example #18
0
 private bool Equals(Rook other)
 {
     return !ReferenceEquals(null, other)
         && Color == other.Color;
 }
Example #19
0
        public void Whether_Board_UpdatesIsWhiteMove_On_MoveTo()
        {
            var board = new Board();
            var newPos = new Square(1, 6);
            var oldPos = new Square(1, 1);
            var rook = new Rook(oldPos, board, PieceColor.White);

            var hasMoved = rook.MoveTo(newPos);

            Assert.That(rook.Position.Equals(newPos));
            Assert.IsFalse(board.IsWhiteMove);
            Assert.That(hasMoved);
        }
Example #20
0
        public void Whether_Piece_CantMoveWhenIsWhiteMove_On_MoveTo()
        {
            var board = new Board();
            var newPos = new Square(1, 6);
            var oldPos = new Square(1, 1);
            var rook = new Rook(oldPos, board, PieceColor.Black);

            var hasMoved = rook.MoveTo(newPos);

            Assert.That(board.IsWhiteMove);
            Assert.That(rook.Position.Equals(oldPos));
            Assert.That(!hasMoved);
        }
Example #21
0
 public void BeforeEachTest()
 {
     Target = new Rook();
 }
Example #22
0
        public static void initGame()
        {
            Console.Clear();
            //initalize game

            Random rnd = new Random ();
            if (rnd.Next(2)==1)
            {
                State.playerOneColor = "White";
            }
            else
            {
                State.playerOneColor = "Black";
            }

            State.moveNumber = 0;
            State.turn = "White";
            State.strength = 18;

            //Initiate White pieces
            Piece k2 = new King (4, 0, "White");
            Piece q1 = new Queen(3, 0, "White");
            Piece r1 = new Rook(0, 0, "White");
            Piece r2 = new Rook(7, 0, "White");
            Piece b1 = new Bishop (5, 0, "White");
            Piece b2 = new Bishop (2, 0, "White");
            Piece n1 = new Knight (1, 0, "White");
            Piece n2 = new Knight (6, 0, "White");
            //pawns
            Piece p0 = new Pawn (0, 1, "White");
            Piece p1 = new Pawn (1, 1, "White");
            Piece p2 = new Pawn (2, 1, "White");
            Piece p3 = new Pawn (3, 1, "White");
            Piece p4 = new Pawn (4, 1, "White");
            Piece p5 = new Pawn (5, 1, "White");
            Piece p6 = new Pawn (6, 1, "White");
            Piece p7 = new Pawn (7, 1, "White");

            //Initiate Black pieces
            Piece k1 = new King (4, 7, "Black");
            Piece q2 = new Queen (3, 7, "Black");
            Piece r3 = new Rook (0, 7, "Black");
            Piece r4 = new Rook (7, 7, "Black");
            Piece b3 = new Bishop(5, 7, "Black");
            Piece b4 = new Bishop(2, 7, "Black");
            Piece n3 = new Knight (1,7, "Black");
            Piece n4 = new Knight (6,7, "Black");
            //pawns
            Piece p8 = new Pawn (0, 6, "Black");
            Piece p9 = new Pawn (1, 6, "Black");
            Piece p10 = new Pawn (2, 6, "Black");
            Piece p11 = new Pawn (3, 6, "Black");
            Piece p12 = new Pawn (4, 6, "Black");
            Piece p13 = new Pawn (5, 6, "Black");
            Piece p14 = new Pawn (6, 6, "Black");
            Piece p15 = new Pawn (7, 6, "Black");

            //Add pieces to piece array
            Piece[] pieces = new Piece[] {k1, k2, q1, q2,
                r1, r2, r3, r4, n1, n2, n3, n4, b1, b2, b3, b4,
                p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};

            //Send setup back to the state
            State.addPiece(pieces);
        }
Example #23
0
        public void Whether_Piece_PositionUpdatedRaised_On_PieceMoved()
        {
            var board = new Board();
            var newPos = new Square(1, 6);
            var moved = false;
            var rook = new Rook(new Square(1, 1), board, PieceColor.White);
            rook.PositionUpdated += (s, p) => moved = true;

            rook.MoveTo(newPos);

            Assert.That(moved, Is.True);
        }
Example #24
0
        public void Whether_Piece_UpdatesPosition_On_MoveTo()
        {
            var board = new Board();
            var newPos = new Square(1, 6);
            var rook = new Rook(new Square(1, 1), board, PieceColor.White);

            rook.MoveTo(newPos);

            Assert.That(rook.Position.Equals(newPos));
        }
            // images[blksq/whtsq][side][idx] - idx = {KING, QUEEN, ROOK, KNIGHT, BISHOP}
            public ChessPiece createPiece(PieceType type,
						       ColorType color,
						       int rank, int file)
            {
                ChessPiece piece = null;
                ChessSide myside, oppside;
                if (color == ColorType.WHITE)
                  {
                      myside = whites;
                      oppside = blacks;
                  }
                else
                  {
                      myside = blacks;
                      oppside = blacks;
                  }

                switch (type)
                  {
                  case PieceType.KING:
                      piece = new King (color, rank, file,
                                myside, oppside);
                      break;
                  case PieceType.QUEEN:
                      piece = new Queen (color, rank,
                                 file, myside,
                                 oppside);
                      break;
                  case PieceType.ROOK:
                      piece = new Rook (color, rank, file,
                                myside, oppside);
                      break;
                  case PieceType.KNIGHT:
                      piece = new Knight (color, rank,
                                  file, myside,
                                  oppside);
                      break;
                  case PieceType.BISHOP:
                      piece = new Bishop (color, rank,
                                  file, myside,
                                  oppside);
                      break;
                  default:
                      return null;
                  }

                return piece;
            }
Example #26
0
        private static IPiece InitialPieceAtLocation(Rank rank, File file)
        {
            var emptyRanks = new[] { Rank.Three, Rank.Four, Rank.Five, Rank.Six};
            if (emptyRanks.Contains(rank))
            {
                return null;
            }

            var colour = PlayerColour.Black;
            if (rank == Rank.One || rank == Rank.Two)
            {
                colour = PlayerColour.White;
            }

            IPiece piece = new Rook(colour);
            if (rank == Rank.Seven || rank == Rank.Two)
            {
                piece = new Pawn(colour);
            }
            else if (file == File.B || file == File.G)
            {
                piece = new Knight(colour);
            }
            else if (file == File.C || file == File.F)
            {
                piece = new Bishop(colour);
            }
            else if (file == File.D)
            {
                piece = new Queen(colour);
            }
            else if (file == File.E)
            {
                piece = new King(colour);
            }

            return piece;
        }
Example #27
0
 public void Setup()
 {
     Target = new Rook();
 }