public void UpdateFen() { string newFen = ""; int cptVoid = 0; for (int i = 0; i < GetBoardEdgeLen(); i++) { for (int j = 0; j < GetBoardEdgeLen(); j++) { if (boardTab[i, j] != ' ') { if (cptVoid > 0) { newFen += cptVoid.ToString(); } cptVoid = 0; newFen += boardTab[i, j]; } else { cptVoid++; } } if (cptVoid > 0) { newFen += cptVoid.ToString(); } cptVoid = 0; newFen += '/'; } newFen = newFen.Remove(newFen.Length - 1); if (colorTurn) { newFen += " w"; } else { newFen += " b"; } string canCastling = ""; King whiteKing = null; King blackKing = null; List <Rook> whiteRooks = new List <Rook>(); List <Rook> blackRooks = new List <Rook>(); foreach (Piece piece in piecesList) { if (piece.GetType() == typeof(King)) { if (piece.GetColor() == true) { whiteKing = (King)piece; } else { blackKing = (King)piece; } } else if (piece.GetType() == typeof(Rook)) { if (piece.GetColor() == true) { whiteRooks.Add((Rook)piece); } else { blackRooks.Add((Rook)piece); } } } canCastling += CastlingFen(whiteKing, whiteRooks); canCastling += CastlingFen(blackKing, blackRooks); if (canCastling == "") { canCastling = "-"; } newFen += " " + canCastling; fen = newFen; }
public List <string> ChecksCastling(bool color) { List <string> castlingMoves = new List <string>(); King kingPiece = null; List <Rook> rookPieces = new List <Rook>(); foreach (Piece piece in piecesList) { if (piece.GetColor() == color) { if (piece.GetType() == typeof(King)) { kingPiece = (King)piece; } else if (piece.GetType() == typeof(Rook)) { rookPieces.Add((Rook)piece); } } } if (!IsChecked(color) && !kingPiece.HasBeenMoved()) { List <int> kingPos = CoordToIj(kingPiece.GetPos()); Dictionary <string, List <string> > ennemyMoves = GetAllMove(!color); bool queenSide = false; foreach (Rook rook in rookPieces) { bool canCastling = true; if (!(rook.HasBeenMoved())) { int i = 0; if (color) { i = GetBoardEdgeLen() - 1; } List <int> rookPos = CoordToIj(rook.GetPos()); int minj, maxj; if (rookPos[1] < kingPos[1]) { minj = rookPos[1] + 1; maxj = kingPos[1] - 1; queenSide = true; } else { minj = kingPos[1] + 1; maxj = rookPos[1] - 1; queenSide = false; } for (int j = minj; j <= maxj; j++) { if (boardTab[i, j] == ' ') { if ((queenSide && j >= kingPos[1] - 2) || (!queenSide)) { foreach (KeyValuePair <string, List <string> > pieceMoves in ennemyMoves) { foreach (string move in pieceMoves.Value) { if (move == IjToCoord(i, j)) { canCastling = false; break; } } if (!canCastling) { break; } } } } else { canCastling = false; break; } if (!canCastling) { break; } } } else { canCastling = false; } if (canCastling) { char[] kingPosStr = kingPiece.GetPos().ToCharArray(); if (queenSide) { kingPosStr[0] -= (char)2; } else { kingPosStr[0] += (char)2; } castlingMoves.Add(new string(kingPosStr)); } } } return(castlingMoves); }
private Chessman movePieceOnBoard(BoardSpace fromSpace, BoardSpace toSpace, String extraArgs = "") { // This is kust updating the data model. Validation Should occur prior to this // This function Assumes that the moves are valid. Chessman fromPiece = fromSpace.piece; Chessman killedPiece = null; if (toSpace.piece != null) { // We killed piece remove it. killedPiece = toSpace.piece; } fromPiece.position = toSpace; toSpace.piece = fromPiece; fromSpace.piece = null; if (toSpace.piece.GetType() == typeof(King)) { King kp = (King)toSpace.piece; if (kp.canCastle) { kp.canCastle = false; } if (kp.color == ChessmanColor.white) { whiteKingPos = toSpace; } else { blackKingPos = toSpace; } } else if (toSpace.piece.GetType() == typeof(Rook)) { Rook rp = (Rook)toSpace.piece; if (rp.canCastle) { rp.canCastle = false; } } else if (toSpace.piece.GetType() == typeof(Pawn)) { ((Pawn)toSpace.piece).canMoveTwice = false; // Pawns Can only Move Twice on the first move if ((toSpace.piece.color == ChessmanColor.black && toSpace.position.Item2 == 1) || (toSpace.piece.color == ChessmanColor.white && toSpace.position.Item2 == 8)) { Char promotionType = 'q'; if (extraArgs.Length > 0) { promotionType = extraArgs.ToLower()[0]; } switch (promotionType) { case 'q': // Promote to Queen Queen q = new Queen(toSpace, toSpace.piece.color); toSpace.piece = q; break; case 'b': // Promote to Queen Bishop b = new Bishop(toSpace, toSpace.piece.color); toSpace.piece = b; break; case 'n': // Promote to Queen Knight kn = new Knight(toSpace, toSpace.piece.color); toSpace.piece = kn; break; case 'r': // Promote to Queen Rook r = new Rook(toSpace, toSpace.piece.color); toSpace.piece = r; break; } } } updateAttacksOnKing(toSpace.piece.color); return(killedPiece); }
public void SetupBoard() { piece[0] = new Rook(false, "rbk"); piece[1] = new Knight(false, "nbk"); piece[2] = new Bishop(false, "bbk"); piece[3] = new Queen(false, "qb"); piece[4] = new King(false, "kb"); piece[5] = new Bishop(false, "bbq"); piece[6] = new Knight(false, "nbq"); piece[7] = new Rook(false, "rbq"); piece[8] = new Pawn(false, "pba"); piece[9] = new Pawn(false, "pbb"); piece[10] = new Pawn(false, "pbc"); piece[11] = new Pawn(false, "pbd"); piece[12] = new Pawn(false, "pbe"); piece[13] = new Pawn(false, "pbf"); piece[14] = new Pawn(false, "pbg"); piece[15] = new Pawn(false, "pbh"); piece[16] = new Rook(true, "rwk"); piece[17] = new Knight(true, "nwk"); piece[18] = new Bishop(true, "bwk"); piece[19] = new Queen(true, "qw"); piece[20] = new King(true, "kw"); piece[21] = new Bishop(true, "bbw"); piece[22] = new Knight(true, "nbw"); piece[23] = new Rook(true, "rbw"); piece[24] = new Pawn(true, "pba"); piece[25] = new Pawn(true, "pbb"); piece[26] = new Pawn(true, "pbc"); piece[27] = new Pawn(true, "pbd"); piece[28] = new Pawn(true, "pbe"); piece[29] = new Pawn(true, "pbf"); piece[30] = new Pawn(true, "pbg"); piece[31] = new Pawn(true, "pbh"); // set the first turn to white turn = true; // Adds the black home row to the board for (int i = 0; i < 8; i++) { piece[i].Location = new Point(i, 0); } // Adds the black pawns to the board for (int i = 8; i < 16; i++) { piece[i].Location = new Point(i - 8, 1); } // Adds the white home row to the board for (int i = 16; i < 24; i++) { piece[i].Location = new Point(i - 16, 7); } // Adds the White pawns to the board for (int i = 24; i < 32; i++) { piece[i].Location = new Point(i - 24, 6); } }
public bool updateBoardForMove(String move) { bool moveValid = false; String fromPos = move.Substring(0, 2).ToUpper(); String toPos = move.Substring(2, 2).ToUpper(); String extraArgs = ""; if (move.Length > 4) { extraArgs = move.Substring(4, move.Length - 4); } try { BoardSpace fromSpace = getSpace(fromPos[0], int.Parse(new String(new char[] { fromPos[1] }))); BoardSpace toSpace = getSpace(toPos[0], int.Parse(new String(new char[] { toPos[1] }))); if (fromSpace.piece != null) { Chessman fromPiece = fromSpace.piece; if (true) // Do check if move is valid for piece type { if (fromPiece.GetType() == typeof(King) && ((King)fromPiece).canCastle && ((fromPiece.color == ChessmanColor.white && fromSpace.position.Item1 == 'E' && fromSpace.position.Item2 == 1 && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 1) || (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 1)) || (fromPiece.color == ChessmanColor.black && fromSpace.position.Item1 == 'E' && fromSpace.position.Item2 == 8 && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 8) || (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 8)))) { King kingPiece = ((King)fromPiece); //Attempting a castle VAlidate it BoardSpace rookSpace = null; if (kingPiece.color == ChessmanColor.white && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 1)) { // White King Side Castle rookSpace = getSpace('H', 1); if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle) // Was the King Side Rook moved before now { if (getSpace('F', 1).piece == null && getSpace('G', 1).piece == null) // Are all the spaces between the Rook and Knight Clear { if (true) // Are any Spaces the King Must Move Under Attack. (E1(Check),F1, G1) (Skipping for now until we can check this) { movePieceOnBoard(fromSpace, toSpace, extraArgs); // Move King movePieceOnBoard(rookSpace, getSpace('F', 1), extraArgs); // Move Rook moveValid = true; } } } } else if (kingPiece.color == ChessmanColor.white && (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 1)) { // White Queen Side Castle rookSpace = getSpace('A', 1); if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle) // Was the King Side Rook moved before now { if (getSpace('B', 1).piece == null && getSpace('C', 1).piece == null && getSpace('D', 1).piece == null) // Are all the spaces between the Rook and Knight Clear { if (true) // Are any Spaces the King Must Move Under Attack. (E1(Check), D1, C1) (Skipping for now until we can check this) { movePieceOnBoard(fromSpace, toSpace, extraArgs); // Move King movePieceOnBoard(rookSpace, getSpace('D', 1), extraArgs); // Move Rook moveValid = true; } } } } else if (kingPiece.color == ChessmanColor.black && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 8)) { // White King Side Castle rookSpace = getSpace('H', 8); if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle) // Was the King Side Rook moved before now { if (getSpace('F', 8).piece == null && getSpace('G', 8).piece == null) // Are all the spaces between the Rook and Knight Clear { if (true) // Are any Spaces the King Must Move Under Attack. (E8(Check),F8, G8) (Skipping for now until we can check this) { movePieceOnBoard(fromSpace, toSpace, extraArgs); // Move King movePieceOnBoard(rookSpace, getSpace('F', 8), extraArgs); // Move Rook moveValid = true; } } } } else if (kingPiece.color == ChessmanColor.black && (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 8)) { // White Queen Side Castle rookSpace = getSpace('A', 8); if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle) // Was the King Side Rook moved before now { if (getSpace('B', 8).piece == null && getSpace('C', 8).piece == null && getSpace('D', 8).piece == null) // Are all the spaces between the Rook and Knight Clear { if (true) // Are any Spaces the King Must Move Under Attack. (E1(Check), D1, C1) (Skipping for now until we can check this) { movePieceOnBoard(fromSpace, toSpace, extraArgs); // Move King movePieceOnBoard(rookSpace, getSpace('D', 8), extraArgs); // Move Rook moveValid = true; } } } } if (moveValid) { // Grab All King and Rooks for the color and disable castling foreach (BoardSpace s in boardState) { if (s.piece != null && s.piece.color == fromPiece.color) { if (s.piece.GetType() == typeof(Rook)) { ((Rook)s.piece).canCastle = false; } else if (s.piece.GetType() == typeof(King)) { ((King)s.piece).canCastle = false; } } } } } else { movePieceOnBoard(fromSpace, toSpace, extraArgs); moveValid = true; } } } else { // There should always be a piece in the from Space } } catch { } return(moveValid); }