public ReadOnlyCollection<Move> GenerateMoves(Color onMove, Square[,] board) { List<Move> moves = new List<Move>(); if (HasPiece && onMove == Piece.Color) moves.AddRange(Piece.GenerateMoves(m_coordinate.Rank.ToRow(), m_coordinate.File.ToColumn(), board)); return moves.AsReadOnly(); }
public override ReadOnlyCollection<Move> GenerateMoves(int row, int column, Square[,] board) { List<Move> moves = new List<Move>(); moves.AddRange(Scan(0, 1, row, column, board)); moves.AddRange(Scan(1, 0, row, column, board)); moves.AddRange(Scan(0, -1, row, column, board)); moves.AddRange(Scan(-1, 0, row, column, board)); return moves.AsReadOnly(); }
public static Square[,] EmptyBoard() { Square[,] board = new Square[8, 8]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) board[i, j] = new Square(new Coordinate(j, i), (i + j) % 2 == 0 ? Color.Black : Color.White); } return board; }
public static Square[,] StartingBoard() { Square[,] board = new Square[8, 8]; // rank 1 int row = 0; board[row, 0] = new Square(new Coordinate(0, row), Color.Black, new Rook(Color.White)); board[row, 1] = new Square(new Coordinate(1, row), Color.White, new Knight(Color.White)); board[row, 2] = new Square(new Coordinate(2, row), Color.Black, new Bishop(Color.White)); board[row, 3] = new Square(new Coordinate(3, row), Color.White, new Queen(Color.White)); board[row, 4] = new Square(new Coordinate(4, row), Color.Black, new King(Color.White)); board[row, 5] = new Square(new Coordinate(5, row), Color.White, new Bishop(Color.White)); board[row, 6] = new Square(new Coordinate(6, row), Color.Black, new Knight(Color.White)); board[row, 7] = new Square(new Coordinate(7, row), Color.White, new Rook(Color.White)); // rank 2 row = 1; for (int i = 0; i < 8; i++) board[row, i] = new Square(new Coordinate(i, row), i % 2 == 0 ? Color.White : Color.Black, new Pawn(Color.White)); // ranks 3 - 6 for (row = 2; row < 6; row++) { for (int j = 0; j < 8; j++) board[row, j] = new Square(new Coordinate(j, row), (row + j) % 2 == 0 ? Color.Black : Color.White); } // rank 7 row = 6; for (int i = 0; i < 8; i++) board[row, i] = new Square(new Coordinate(i, row), i % 2 == 0 ? Color.Black : Color.White, new Pawn(Color.Black)); // rank 8 row = 7; board[row, 0] = new Square(new Coordinate(0, row), Color.White, new Rook(Color.Black)); board[row, 1] = new Square(new Coordinate(1, row), Color.Black, new Knight(Color.Black)); board[row, 2] = new Square(new Coordinate(2, row), Color.White, new Bishop(Color.Black)); board[row, 3] = new Square(new Coordinate(3, row), Color.Black, new Queen(Color.Black)); board[row, 4] = new Square(new Coordinate(4, row), Color.White, new King(Color.Black)); board[row, 5] = new Square(new Coordinate(5, row), Color.Black, new Bishop(Color.Black)); board[row, 6] = new Square(new Coordinate(6, row), Color.White, new Knight(Color.Black)); board[row, 7] = new Square(new Coordinate(7, row), Color.Black, new Rook(Color.Black)); return board; }
public override ReadOnlyCollection<Move> GenerateMoves(int row, int column, Square[,] board) { List<Move> moves = new List<Move>(); // horizontal/vertical moves.AddRange(Scan(0, 1, row, column, board, onlyOnce: true)); moves.AddRange(Scan(1, 0, row, column, board, onlyOnce: true)); moves.AddRange(Scan(0, -1, row, column, board, onlyOnce: true)); moves.AddRange(Scan(-1, 0, row, column, board, onlyOnce: true)); // diagonal moves.AddRange(Scan(-1, 1, row, column, board, onlyOnce: true)); moves.AddRange(Scan(1, 1, row, column, board, onlyOnce: true)); moves.AddRange(Scan(1, -1, row, column, board, onlyOnce: true)); moves.AddRange(Scan(-1, -1, row, column, board, onlyOnce: true)); return moves.AsReadOnly(); }
private static ObservableCollection<Square> GetDisplayBoard(Square[,] logicalBoard) { List<Square> squares = new List<Square>(); for (int i = 7; i >= 0; i--) { for (int j = 0; j < 8; j++) squares.Add(logicalBoard[i, j]); } return new ObservableCollection<Square>(squares); }
public override ReadOnlyCollection<Move> GenerateMoves(int row, int column, Square[,] board) { List<Move> moves = new List<Move>(); Coordinate from = new Coordinate(column, row); if (Color == Color.White) { // moves forward if (row != 7 && !board[row + 1, column].HasPiece) { if (row == 1 && !board[row + 2, column].HasPiece) { // starting move of two squares Coordinate to = new Coordinate(column, row + 2); moves.Add(new Move(MoveType.PawnStart, from, to)); } if (row == 6) { // promotions Coordinate to = new Coordinate(column, row + 1); moves.AddRange(GetPromotions(from, to)); } else { // single square forward Coordinate to = new Coordinate(column, row + 1); moves.Add(new Move(MoveType.Standard, from, to)); } } // capture left if (column != 0 && row != 7 && board[row + 1, column - 1].HasPiece && board[row + 1, column - 1].Piece.Color == Color.Black) { Coordinate to = new Coordinate(column - 1, row + 1); if (row == 6) { // promotion captures moves.AddRange(GetPromotions(from, to)); } else { // regular captures moves.Add(new Move(MoveType.Standard, from, to)); } } // capture right if (column != 7 && row != 7 && board[row + 1, column + 1].HasPiece && board[row + 1, column + 1].Piece.Color == Color.Black) { Coordinate to = new Coordinate(column + 1, row + 1); if (row == 6) { // promotion captures moves.AddRange(GetPromotions(from, to)); } else { // regular captures moves.Add(new Move(MoveType.Standard, from, to)); } } } else if (Color == Color.Black) { // moves forward if (row != 0 && !board[row - 1, column].HasPiece) { if (row == 6 && !board[row - 2, column].HasPiece) { // starting move of two squares Coordinate to = new Coordinate(column, row - 2); moves.Add(new Move(MoveType.PawnStart, from, to)); } if (row == 1) { // promotions Coordinate to = new Coordinate(column, row - 1); moves.AddRange(GetPromotions(from, to)); } else { // single square forward Coordinate to = new Coordinate(column, row - 1); moves.Add(new Move(MoveType.Standard, from, to)); } } // capture left if (column != 0 && row != 0 && board[row - 1, column - 1].HasPiece && board[row - 1, column - 1].Piece.Color == Color.White) { Coordinate to = new Coordinate(column - 1, row - 1); if (row == 1) { // promotion captures moves.AddRange(GetPromotions(from, to)); } else { // regular captures moves.Add(new Move(MoveType.Standard, from, to)); } } // capture right if (column != 7 && row != 0 && board[row - 1, column + 1].HasPiece && board[row - 1, column + 1].Piece.Color == Color.White) { Coordinate to = new Coordinate(column + 1, row - 1); if (row == 6) { // promotion captures moves.AddRange(GetPromotions(from, to)); } else { // regular captures moves.Add(new Move(MoveType.Standard, from, to)); } } } return moves.AsReadOnly(); }
public string ToString(Square[,] board) { if (m_moveType == MoveType.CastleKingside) return "0-0"; if (m_moveType == MoveType.CastleQueenside) return "0-0-0"; Piece piece = board[m_from.BoardRow(), m_from.BoardColumn()].Piece; bool isCapture = board[m_to.BoardRow(), m_to.BoardColumn()].HasPiece; string promotionType = m_moveType == MoveType.Promotion ? m_promoteTo.ToString() : ""; string pieceContext = ""; if (piece.Type == PieceType.Knight || piece.Type == PieceType.Rook) { // may need to append file or rank info int count = 0; for (int i = 0; i < 8; i++) { if (board[m_from.BoardRow(), i].HasPiece && board[m_from.BoardRow(), i].Piece.Type == piece.Type && board[m_from.BoardRow(), i].Piece.Color == piece.Color) count++; } pieceContext = count <= 2 ? m_from.File.ToString() : m_from.Rank.ToString(); } return "{0}{1}{2}{3}{4}{5}".FormatInvariant(piece, pieceContext, isCapture ? "x" : "", m_to.File.ToString().ToLowerInvariant(), m_to.Rank, promotionType); }