Exemple #1
0
        /* Method for the movement of pawn */
        public override bool move(Board board, coord start, coord end)
        {
            int direction;        /* Int value for direction of movement */

              /* If the color is white we have an increment of y(positive direction) else a decrement */
              if (this.color == "white") {
            direction = 1;
              } else {
            direction = -1;
              }
              if (!this.hasMoved &&
              start.x == end.x &&
              start.y + (2 * direction) == end.y &&
              board.getFieldFigureName (start.x, start.y + direction) == "Empty" &&
              board.getFieldFigureName (start.x, start.y + (2 * direction)) == "Empty")
            return true;
              if (start.x == end.x &&
              start.y + direction == end.y &&
              board.getFieldFigureName (start.x, start.y + direction) == "Empty")
            return true;
              if ((start.x + 1 == end.x || start.x - 1 == end.x) &&
              start.y + direction == end.y &&
              board.getFieldFigureName (end) != "Empty") {
            return true;
              }

              return false;                   /* If the value of x and y are not corresponding to the rule the movement isn't allowed  */
        }
        public void BeforeEachTest()
        {
            Piece = new Pawn();
            Target = new Board();

            Target.AddPiece(Piece, new BoardCoordinate(1, 1));
        }
Exemple #3
0
 /* Method for the movement of the bishop*/
 public override bool move(Board board, coord start, coord end)
 {
     if (Math.Abs (start.x - end.x) == Math.Abs (start.y - end.y)) {
       coord tmp = start;
       while (!tmp.Equals (end)) {                                           // Until tmp isn't equal to end coordinates:
       if (board.getFieldFigureName (tmp) != "Empty" && !tmp.Equals(start))  // If there is an object along trajectory
       return false;                                                     // movement isn't permitted
     if (tmp.x > end.x && tmp.y > end.y) {                               // If temp.x and temp.y are major than their end
       tmp.x--;                                                          //Decrement both
       tmp.y--;
     }
     if (tmp.x > end.x && tmp.y < end.y) {                               // or temp.x is major and temp.x is minor
       tmp.x--;                                                          // decrement x
       tmp.y++;                                                          // increment y
     }
     if (tmp.x < end.x && tmp.y > end.y) {                               // or temp.x is minor and temp.y is major
       tmp.x++;                                                          // increment x
       tmp.y--;                                                          // decrement y
     }
     if (tmp.x < end.x && tmp.y < end.y) {                               // or temp.x is minor and temp.y is minor
       tmp.x++;                                                          // Increment both
       tmp.y++;
     }
       }
       return true;                                                          // movement is allowed if there isn't an object along trajectory
       }
       return false;                                                             // If the movement isn't diagonal it isn't permitted
 }
        public CastlingStatusChecker(Board board)
        {
            if (board == null)
                throw new ArgumentNullException(nameof(board));

            _board = board;
        }
Exemple #5
0
        /* Method for the movement of knight */
        public override bool move(Board board, coord start, coord end)
        {
            coord rule = new coord (1, 2);
              int x = rule.x;                     // x is setted to 1
              int y = rule.y;                     // y is setted to 2

              for (int i = 0; i < 2; i++) {
            if (i == 1) {                     // When i is equal to 1
              x = rule.y;                     // x is setted to 2
              y = rule.x;                     // and y is setted to 1
            }
            if (start.x + x == end.x) {       // If the movement on x is equal to starting position plus x
              if (start.y + y == end.y) {     // and the movement on y is equal to starting position plus y
            return true;                  // movement is permitted
              }
            }
            if (start.x - x == end.x) {       // If the movement on x is equal to starting position minus x
              if (start.y - y == end.y) {     // and the movement on y is equal to starting position minus y
            return true;                  // movement is permitted
              }
            }
            if (start.x - x == end.x) {       // If the movement on x is equal to starting position minus x
              if (start.y + y == end.y) {     // and the movement on y is equal to starting position plus y
            return true;                  // movement is permitted
              }
            }
            if (start.x + x == end.x) {       // If the movement on x is equal to starting position plus x
              if (start.y - y == end.y) {     // and the movement on y is equal to starting position minus y
            return true;                  // movement is permitted
              }
            }
              }
              return false;                       // Other movement aren't permitted
        }
Exemple #6
0
        /*
         * This method returns the only instance of this class
         */
        public static Board getInstance()
        {
            if (board == null)
            {
                board = new Board();
            }

            return board;
        }
Exemple #7
0
 /* Method for the movement of king */
 public override bool move(Board board, coord start, coord end)
 {
     for (int x = (start.x == 0) ? 0 : start.x - 1; x <= start.x + 1; x++) {   /* If the movement on x is between -1 and 1(included) */
     for (int y = (start.y == 0) ? 0 : start.y - 1; y <= start.y + 1; y++) { /* if the movement on y is between -1 and 1(included) */
       if (end.Equals (new coord (x, y)))                                    /* and the end coordinates are updated */
     return true;                                                        /* this method return a true value */
     }
       }
       return false;                                                             /* If the value of x and y are not corresponding to the rule the movement isn't allowed  */
 }
Exemple #8
0
        private static void StartNewGameCore()
        {
            var board = new Board(new ComputerPlayer(AILevel.Easy), new ComputerPlayer(AILevel.Easy));
            //board.Check += p => Console.WriteLine("Check");
            board.Checkmate += p => { Console.WriteLine("Checkmate: {0} wins", p == PlayerColor.Black ? PlayerColor.White : p); StartNewGame(); };
            board.Stalemate += (reason) => { Console.WriteLine("Stalemate: " + reason); StartNewGame(); };
            board.Start();

            while (board.IsActive)
                System.Threading.Thread.Sleep(1);
        }
Exemple #9
0
        internal static string FromBoard(Board board)
        {
            StringBuilder sb = new StringBuilder();

            int emptyCount = 0;
            for (int rank = 8; rank >= 1; rank--)
            {
                for (int column = 1; column <= 8; column++)
                {
                    var piece = board[rank, column];
                    if (piece == null)
                    {
                        emptyCount++;
                        continue;
                    }

                    if (emptyCount != 0)
                        sb.Append(emptyCount);
                    sb.Append(GetNotation(piece));
                    emptyCount = 0;
                }

                if (emptyCount != 0) sb.Append(emptyCount);
                emptyCount = 0;

                if (rank > 1)
                    sb.Append('/');
            }

            sb.Append(' ').Append(char.ToLower(board.CurrentPlayer.PlayerColor.ToString()[0]));

            sb.Append(' ');
            string castle = GetNotation(board.GetCastleAvailabity(PlayerColor.White), PlayerColor.White) + GetNotation(board.GetCastleAvailabity(PlayerColor.Black), PlayerColor.Black);
            if (castle.Length == 0)
                sb.Append('-');
            else
                sb.Append(castle);

            sb.Append(' ');
            if (board.LastMove != null && board.LastMove.Piece is Pawn && Math.Abs(board.LastMove.Source.GetRank() - board.LastMove.Target.GetRank()) == 2)
                sb.Append(board.LastMove.Target.ToString().ToLower());
            else
                sb.Append('-');

            sb.Append(' ')
              .Append(board.History.Reverse().TakeWhile(i => !(i.Piece is Pawn) && i.CapturedPiece == null).Count())
              .Append(' ')
              .Append((int)(board.History.Count / 2));

            return sb.ToString();
        }
Exemple #10
0
        // Method for the movement of rock
        public override bool move(Board board, coord start, coord end)
        {
            //  int x = this.rule.x;
              // int y = this.rule.y;

              if (start.x == end.x) {                                          // Check for the vertical movement
            if (start.y < end.y) {                                         // If the end position is major than the starting position
              for (int i = start.y + 1; i < end.y; i++) {                  // Check for an object in this trajectory
            if (board.getFieldFigureName (start.x, i) != "Empty") {
              return false;                                            // If there is one (not empty) movement isn't allowed
            }
              }
            } else {                                                      // If the end position is minor than the starting position
              for (int i = start.y - 1; i > end.y; i--) {                 // Check for an object in this trajectory
            if (board.getFieldFigureName (start.x, i) != "Empty") {
              return false;                                           // If there is one (not empty) movement isn't allowed
            }
              }
            }
            return true;                                                  // If there aren't object movement is permitted

              } else if (start.y == end.y) {                                  // Check for the horizontal movement
            if (start.x < end.x) {                                        // If the end position is major than the starting position
              for (int i = start.x + 1; i < end.x; i++) {                 // Check for an object in this trajectory
            if (board.getFieldFigureName (i, start.y) != "Empty") {
              return false;                                            // If there is one (not empty) movement isn't allowed
            }
              }
            } else {                                                      // If the end position is minor than the starting position
              for (int i = start.x - 1; i > end.x; i--) {                 // Check for an object in this trajectory
            if (board.getFieldFigureName (i, start.y) != "Empty") {
              return false;                                           // If there is one (not empty) movement isn't allowed
            }
              }
            }
            return true;                                                  // If there aren't object along trajectory movement is permitted
              }
              return false;                                                   // If the movement isn't vertical or horizontal it isn't permitted
        }
Exemple #11
0
 static void Main(string[] args)
 {
     Board ChessBoard = new Board();
     String test = ChessBoard.GetBoardString();
 }
Exemple #12
0
 public CellFactory(Board board)
 {
     _board = board;
 }
Exemple #13
0
 public Game()
 {
     chessBoard = new Board();
 }
Exemple #14
0
 /* Method for the movement of the figure on the board */
 public virtual bool move(Board board, coord start, coord end)
 {
     return true;
 }
Exemple #15
0
 public Piece CreatePiece(Board board)
 {
     var piece = (Piece)Activator.CreateInstance(this.PieceType);
     piece.SetPlacement(board, this);
     return piece;
 }
Exemple #16
0
 public Rook(Board board, Color color) : base(board, color)
 {
 }
Exemple #17
0
        private bool HasEnemy(Position pos)
        {
            Piece p = Board.Piece(pos);

            return(p != null && p.Color != Color);
        }
Exemple #18
0
        //Methods

        /* Checks if it is posible to move to a position */
        private bool PossibleMove(Position pos)
        {
            Piece p = Board.GetPiece(pos);

            return(p == null || p.Color != this.Color);
        }
Exemple #19
0
        public override bool[,] PossibleMoves()
        {
            bool[,] mat = new bool[Board.Lines, Board.Columns];

            Position pos = new Position(0, 0);

            if (Color == Color.White)
            {
                pos.DefineValues(Position.Line - 1, Position.Column);
                if (Board.IsValidPosition(pos) && IsFree(pos))
                {
                    mat[pos.Line, pos.Column] = true;
                }

                pos.DefineValues(Position.Line - 2, Position.Column);
                Position pos2 = new Position(Position.Line - 1, Position.Column);
                if (Board.IsValidPosition(pos2) && IsFree(pos2) && Board.IsValidPosition(pos) && IsFree(pos) && MoveCount == 0)
                {
                    mat[pos.Line, pos.Column] = true;
                }

                pos.DefineValues(Position.Line - 1, Position.Column - 1);
                if (Board.IsValidPosition(pos) && HasEnemy(pos))
                {
                    mat[pos.Line, pos.Column] = true;
                }

                pos.DefineValues(Position.Line - 1, Position.Column + 1);
                if (Board.IsValidPosition(pos) && HasEnemy(pos))
                {
                    mat[pos.Line, pos.Column] = true;
                }

                //#SpecialMove En Passant
                if (Position.Line == 3)
                {
                    Position left = new Position(Position.Line, Position.Column - 1);
                    if (Board.IsValidPosition(left) && HasEnemy(left) && Board.Piece(left) == Match.VulnerableEnPassant)
                    {
                        mat[left.Line - 1, left.Column] = true;
                    }

                    Position right = new Position(Position.Line, Position.Column + 1);
                    if (Board.IsValidPosition(right) && HasEnemy(right) && Board.Piece(right) == Match.VulnerableEnPassant)
                    {
                        mat[right.Line - 1, right.Column] = true;
                    }
                }
            }
            else
            {
                pos.DefineValues(Position.Line + 1, Position.Column);
                if (Board.IsValidPosition(pos) && IsFree(pos))
                {
                    mat[pos.Line, pos.Column] = true;
                }

                pos.DefineValues(Position.Line + 2, Position.Column);
                Position pos2 = new Position(Position.Line + 1, Position.Column);

                if (Board.IsValidPosition(pos2) && IsFree(pos2) && Board.IsValidPosition(pos) && IsFree(pos) && MoveCount == 0)

                {
                    mat[pos.Line, pos.Column] = true;
                }

                pos.DefineValues(Position.Line + 1, Position.Column - 1);
                if (Board.IsValidPosition(pos) && HasEnemy(pos))
                {
                    mat[pos.Line, pos.Column] = true;
                }

                pos.DefineValues(Position.Line + 1, Position.Column + 1);
                if (Board.IsValidPosition(pos) && HasEnemy(pos))
                {
                    mat[pos.Line, pos.Column] = true;
                }

                //#SpecialMove En Passant
                if (Position.Line == 4)
                {
                    Position left = new Position(Position.Line, Position.Column - 1);
                    if (Board.IsValidPosition(left) && HasEnemy(left) && Board.Piece(left) == Match.VulnerableEnPassant)
                    {
                        mat[left.Line + 1, left.Column] = true;
                    }

                    Position right = new Position(Position.Line, Position.Column + 1);
                    if (Board.IsValidPosition(right) && HasEnemy(right) && Board.Piece(right) == Match.VulnerableEnPassant)
                    {
                        mat[right.Line + 1, right.Column] = true;
                    }
                }
            }

            return(mat);
        }
Exemple #20
0
 private bool IsFree(Position pos)
 {
     return(Board.Piece(pos) == null);
 }
Exemple #21
0
        /* Method for the movement of the queen */
        public override bool move(Board board, coord start, coord end)
        {
            // Diagonal movement
              if (Math.Abs (start.x - end.x) == Math.Abs (start.y - end.y)) {
            coord tmp = start;
            while (!tmp.Equals (end)) {                                              // Until tmp isn't equal to end coordinates:
              if (board.getFieldFigureName (tmp) != "Empty" && !tmp.Equals(start))   // If there is an object along trajectory
            return false;                                                        // movement isn't permitted
              if (tmp.x > end.x && tmp.y > end.y) {                                  // If temp.x and temp.y are major than their end
            tmp.x--;                                                             //Decrement both
            tmp.y--;
              }
              if (tmp.x > end.x && tmp.y < end.y) {                                 // or temp.x is major and temp.x is minor
            tmp.x--;                                                            // decrement x
            tmp.y++;                                                            // increment y
              }
              if (tmp.x < end.x && tmp.y > end.y) {                                 // or temp.x is minor and temp.y is major
            tmp.x++;                                                             // increment x
            tmp.y--;                                                             // decrement y
              }
              if (tmp.x < end.x && tmp.y < end.y) {                                  // or temp.x is minor and temp.y is minor
            tmp.x++;                                                             // Increment both
            tmp.y++;
              }
            }
            return true;                                                             // movement is allowed if there isn't an object along trajectory
              }

              // Vertical and horizontal movement
              if (start.x == end.x) {                                                    // Check for the vertical movement
            if (start.y < end.y) {                                                   // If the end position is major than the starting position
              for (int i = start.y + 1; i < end.y; i++) {                           // Check for an object in this trajectory
            if (board.getFieldFigureName (start.x, i) != "Empty") {
              return false;                                                     // If there is one (not empty) movement isn't allowed
            }
              }
            } else {                                                               // If the end position is minor than the starting position
              for (int i = start.y - 1; i > end.y; i--) {                           // Check for an object in this trajectory
            if (board.getFieldFigureName (start.x, i) != "Empty") {
              return false;                                                    // If there is one (not empty) movement isn't allowed
            }
              }
            }
            return true;                                                            // If there aren't object movement is permitted

              } else if (start.y == end.y) {                                            // Check for the horizontal movement
            if (start.x < end.x) {                                                  // If the end position is major than the starting position
              for (int i = start.x + 1; i < end.x; i++) {                           // Check for an object in this trajectory
            if (board.getFieldFigureName (i, start.y) != "Empty") {
              return false;                                                     // If there is one (not empty) movement isn't allowed
            }
              }
            } else {                                                               // If the end position is minor than the starting position
              for (int i = start.x - 1; i > end.x; i--) {                          // Check for an object in this trajectory
            if (board.getFieldFigureName (i, start.y) != "Empty") {
              return false;                                                    // If there is one (not empty) movement isn't allowed
            }
              }
            }
            return true;                                                           // If there aren't object along trajectory movement is permitted
              }
              return false;                                                            // Any other case different from movements horizontal,diagonal or vertical aren't permitted
        }
Exemple #22
0
 public Pawn(Board board, Color color, ChessMatch match) : base(board, color)
 {
     Match = match;
 }
 public PiecePositioner(Board target)
 {
     _targetBoard = target;
 }
Exemple #24
0
 internal void SetPlacement(Board board, FEN.PiecePlacement piecePlacement)
 {
     this.Player = piecePlacement.Player;
     this.Square = piecePlacement.Square;
     this.Board = board;
 }
Exemple #25
0
		private async Task<IBoard> CloneBoard()
		{
			IBoard cloneBoard = new Board();

			var cellList = AllCells.Select(cell => cell.CloneCellViewModel()).ToList();

			cloneBoard.WhiteTurn = WhiteTurn;

			foreach (var cell in cellList)
			{
				cell.Board = cloneBoard;
			}

			await cloneBoard.CreateChessBoardWithTemplate(cellList);

			return cloneBoard;
		}
 public ThreatEvaluator(Board board)
 {
     _board = board;
 }
Exemple #27
0
        //Constructor

        // @param Color color, Board board
        public Bishop(Color color, Board board) : base(color, board)
        {
        }