public void Remove(Piece piece) { _pieces.Remove(piece); WhitePieces.Remove(piece); BlackPieces.Remove(piece); OnPropertyChanged("Pieces"); OnPropertyChanged("WhitePieces"); OnPropertyChanged("BlackPieces"); }
public void AddPieceToBoard(Piece piece) { if (piece.Color == ChessColor.White) { WhitePieces.Add(piece); } else { BlackPieces.Add(piece); } }
/// <summary> /// Finds all instances of implementations of <see cref="IChessPiece"/> of the specified type and colour"/> /// </summary> /// <typeparam name="T">The <see cref="Type"/> to look for</typeparam> /// <param name="color">The <see cref="PieceColor"/> of the pieces to look for</param> /// <returns>A list of the chess pieces of the specified type and colour</returns> // Consider overload with no parameter and filtering after retrieval public IEnumerable <T> GetPiecesOfType <T>(PieceColor color) where T : IChessPiece { if (color == PieceColor.Black) { return(BlackPieces.OfType <T>()); } else { return(WhitePieces.OfType <T>()); } }
/// <summary> /// Determines of the container has capacity for the passed in chess piece /// </summary> /// <param name="piece">The <see cref="IChessPiece"/> to be added</param> /// <returns>True if there is enough capacity</returns> public bool HasCapacityFor(IChessPiece piece) { Type pieceType = piece.GetType(); if (piece.PieceColor == PieceColor.Black) { return(BlackPieces.Where(p => p.GetType() == pieceType).Count() < piece.NumberPiecesAllowedPerColour); } else { return(WhitePieces.Where(p => p.GetType() == pieceType).Count() < piece.NumberPiecesAllowedPerColour); } }
public void RemovePieceAt(Position position) { foreach (Piece piece in WhitePieces) { if (piece.Position == position) { WhitePieces.Remove(piece); return; } } foreach (Piece piece in BlackPieces) { if (piece.Position == position) { BlackPieces.Remove(piece); return; } } }
// // Summary: // The basic constructor of a DraughtsMatch.s public DraughtsMatch() { // // Place the red pieces in their initial positions and place the piece in a collection. for (int arrRow = 0; arrRow <= 3; arrRow++) { // // Explanation about the value to be assigned to arrColumn variable: // // Arrray's odd lines will have their frist pieces in column 0, // while Even lines will have the frist pieces in the array column 1. for (int arrColumn = 1 - arrRow % 2; arrColumn < Board.Width; arrColumn += 2) { Board.PlacePiece( new Pawn(new TwoDimensionsArrayPosition(arrRow, arrColumn).ToBoardPosition(Board.Height), Board, Team.Red), new TwoDimensionsArrayPosition(arrRow, arrColumn).ToBoardPosition(Board.Height)); RedPieces.Add(Board.Pieces(arrRow, arrColumn)); } } // // Place the white pieces in their initial positions and place the piece in a collection. for (int arrRow = 6; arrRow < Board.Height; arrRow++) { // // Explanation about the value to be assigned to arrColumn variable: // // Arrray's odd lines will have their frist pieces in column 0, // while Even lines will have the frist pieces in the array column 1. for (int arrColumn = 1 - arrRow % 2; arrColumn < Board.Width; arrColumn += 2) { Board.PlacePiece( new Pawn(new TwoDimensionsArrayPosition(arrRow, arrColumn).ToBoardPosition(Board.Height), Board, Team.White), new TwoDimensionsArrayPosition(arrRow, arrColumn).ToBoardPosition(Board.Height)); WhitePieces.Add(Board.Pieces(arrRow, arrColumn)); } } Round = 1; TurnPlayer = Team.White; }
/// <summary> /// Add a piece to the board, if there already is a piece on the same square then remove the current piece /// </summary> /// <param name="piece"></param> public void AddPiece(Piece piece) { // see if there already is a piece at that position Piece overridenPiece = GetPiece(piece.Position); if (overridenPiece != null) { //this.Remove(overridenPiece); } // Add new piece _pieces.Add(piece); // Make sure that the black and white pices lists are keeps up to date if (piece.Side == Globals.Side.Black) { BlackPieces.Add(piece); } else { WhitePieces.Add(piece); } }
// // Summary: // Make the move, this also includes catching a possible piece in the process. // // Parameters: // origin: // The piece that will make its move. // // target: // The position of the final destination of the Piece origin. public void MakeMovement(Piece origin, BoardPosition target) { TwoDimensionsArrayPosition originArrPos = origin.BoardPosition.ToArrayPosition(Board.Height); TwoDimensionsArrayPosition targetArrPos = target.ToArrayPosition(Board.Height); // If the target results in a capture if (Math.Abs(originArrPos.Row - targetArrPos.Row) >= 2) { // The 'P' represents a piece, not a Pawn itself or a Dame, and the 'E' an enemy piece. // // 4---1 // -E-E- // --P-- // -E-E- // 3---2 TwoDimensionsArrayPosition piece2BCapPos; // Locate and set the position of the piece to be captured if ((originArrPos.Column - targetArrPos.Column) <= -2) // NE && SE { if ((originArrPos.Row - targetArrPos.Row) >= 2) // NE (1) { piece2BCapPos = new TwoDimensionsArrayPosition(targetArrPos.Row + 1, targetArrPos.Column - 1); } else // SE (2) { piece2BCapPos = new TwoDimensionsArrayPosition(targetArrPos.Row - 1, targetArrPos.Column - 1); } } else // SW && NW { if ((originArrPos.Row - targetArrPos.Row) <= -2) // SW (3) { piece2BCapPos = new TwoDimensionsArrayPosition(targetArrPos.Row - 1, targetArrPos.Column + 1); } else // NW (4) { piece2BCapPos = new TwoDimensionsArrayPosition(targetArrPos.Row + 1, targetArrPos.Column + 1); } } if (TurnPlayer == Team.Red) { WhitePieces.Remove(Board.TakePiece(piece2BCapPos)); } else { RedPieces.Remove(Board.TakePiece(piece2BCapPos)); } // Take the origin from the board and place it in the target Board.PlacePiece(Board.TakePiece(origin.BoardPosition), target); } else { // Take the origin from the board and place it in the target Board.PlacePiece(Board.TakePiece(origin.BoardPosition), target); } // If the Red pawn or a White pawn is in its promotion area it'll be promoted switch (Board.Pieces(target)) { case Pawn _ when Board.Pieces(target).Team == Team.Red && target.Row == 1: case Pawn _ when Board.Pieces(target).Team == Team.White && target.Row == 10: Promotion(Board.Pieces(target) as Pawn); break; } }
/// <summary>Constructor using fen string </summary> /// <see cref="Board"/> /// <param name="fen">FEN string that will be parsed to a corresponding board position</param> /// <exception cref="InvalidBoardException"> /// Invlalid strings /// The fen string is interpeted as an array of string, if the array isn't in the correct length (6) /// It can and will throw an exception if any of the arguments provided is not by the fen standart /// </exception> public Grid(string fen) { Board = new Tile[8, 8]; string[] arr = fen.Split(' '); if (arr.Length != 6) { throw new InvalidFENBoardException("FEN string must have six arguments"); } //Initalize pieces section string[] boardState = arr[0].Split('/'); for (int i = 0; i < boardState.Length; i++) { int xPos = 0; foreach (char ch in boardState[i]) { int number; bool success = int.TryParse(ch.ToString(), out number); if (success) { for (int j = 0; j < number; j++) { Board[7 - i, xPos] = new Tile(null, xPos, 7 - i); xPos++; } } else { Piece piece = Piece.PieceIdentifier(ch); if (piece == null) { throw new InvalidFENBoardException("Invalid board state"); } Board[7 - i, xPos] = new Tile(piece, xPos, 7 - i); xPos++; } } } InitPieces(); //initialize currentplayer section string teamFlag = arr[1]; if (teamFlag.Length != 1) { throw new InvalidFENBoardException("Invalid player turn argument"); } if (teamFlag[0] == 'w') { CurrentPlayer = new Player(true); } else if (teamFlag[0] == 'b') { CurrentPlayer = new Player(false); } else { throw new InvalidFENBoardException("Invalid player turn argument"); } //Initialize castling rights section string castlingRights = arr[2]; if (castlingRights.Length > 4 || castlingRights.Length < 0) { throw new InvalidFENBoardException("Invalid castling rights argument"); } string temp = castlingRights.ToUpper(); foreach (char c in temp) { if (c != 'K' && c != 'Q' && c != '-') { throw new InvalidFENBoardException("Invalid castling rights argument"); } } Piece wKing = WhitePieces.Find(piece => piece is King && piece.IsWhite); King whiteKing = wKing as King; if (whiteKing == null) { throw new InvalidFENBoardException("Board missing white king"); } Piece bKing = BlackPieces.Find(piece => piece is King && !piece.IsWhite); King blackKing = bKing as King; if (blackKing == null) { throw new InvalidFENBoardException("Board missing black king"); } if (castlingRights == "-") { whiteKing.HasMoved = true; blackKing.HasMoved = true; } if (!castlingRights.Contains('K')) { whiteKing.kingSideCatlingDone = true; } if (!castlingRights.Contains('Q')) { whiteKing.queenSideCasltingDone = true; } if (!castlingRights.Contains('k')) { blackKing.kingSideCatlingDone = true; } if (!castlingRights.Contains('q')) { blackKing.queenSideCasltingDone = true; } //Initialize enpassant section string enpassant = arr[3]; if (enpassant != "-") { if (enpassant.Length != 2) { throw new InvalidFENBoardException("En passant argument must be 2 characters long"); } int row = (int)enpassant[0] - (int)'a'; int col = int.Parse(enpassant[1].ToString()); Tile pawnTile; if (col == 6) { pawnTile = GetTile(row, col - 2); } else if (col == 3) { pawnTile = GetTile(row, col); } else { throw new InvalidFENBoardException("En passant argument is invalid"); } Pawn pawn = pawnTile.Piece as Pawn; if (pawn == null) { throw new InvalidFENBoardException("En passant is invalid"); } pawn.CanBeCapturedEnPassant = true; } //Initialize 50 move rule count string fiftyMoveRule = arr[4]; int fiftyMoveRuleCount; bool isInt = int.TryParse(fiftyMoveRule, out fiftyMoveRuleCount); if (isInt) { if (fiftyMoveRuleCount >= 0) { FiftyMoveRuleCount = fiftyMoveRuleCount; } else { throw new InvalidFENBoardException("Fifty move rule argument must be a positive integer"); } } else { throw new InvalidFENBoardException("Fifty move rule argument must be an integer"); } //Initialize move count string FENMoveCount = arr[5]; int moveCount; isInt = int.TryParse(FENMoveCount, out moveCount); if (isInt) { if (moveCount > 0) { MoveCount = moveCount; } else { throw new InvalidFENBoardException("Move count argument must be greater than 0"); } } else { throw new InvalidFENBoardException("Move count argument must be an integer"); } UpdateGameState(); UpdateKilledPieces(); AllBoards.Add(CreateCopyOfBoard()); }
/// <summary> /// Returns the fen representation of the current board position /// <see cref="Board"/> /// </summary> /// <returns>The corresponding FEN string of the current board position</returns> public string FEN() { //Init board state section string res = ""; int emptySpacesCount = 0; for (int i = 7; i >= 0; i--) { for (int j = 0; j < 8; j++) { Tile tile = GetTile(j, i); if (tile.Piece != null) { if (emptySpacesCount != 0) { res += emptySpacesCount; } res += tile.Piece.ToString(); emptySpacesCount = 0; } else { emptySpacesCount++; } } if (emptySpacesCount != 0) { res += emptySpacesCount; } emptySpacesCount = 0; res += "/"; } res = res.TrimEnd('/'); //Init current player section if (CurrentPlayer.IsWhite) { res += " w "; } else { res += " b "; } //Init caslting right position Piece wKing = WhitePieces.Find(piece => piece is King); King whiteKing = wKing as King; if (whiteKing == null) { throw new InvalidBoardException("White king is missing"); } Piece bKing = BlackPieces.Find(piece => piece is King); King blackKing = bKing as King; if (blackKing == null) { throw new InvalidBoardException("Black king is missing"); } string castlingRights = ""; if (!whiteKing.HasMoved) { if (!whiteKing.kingSideCatlingDone) { Rook rightRook = GetTile(7, 0).Piece as Rook; if (rightRook != null && !rightRook.HasMoved) { castlingRights += 'K'; } } if (!whiteKing.queenSideCasltingDone) { Rook leftRook = GetTile(0, 0).Piece as Rook; if (leftRook != null && !leftRook.HasMoved) { castlingRights += 'Q'; } } } if (!blackKing.HasMoved) { if (!blackKing.kingSideCatlingDone) { Rook rightRook = GetTile(7, 7).Piece as Rook; if (rightRook != null && !rightRook.HasMoved) { castlingRights += 'k'; } } if (!blackKing.queenSideCasltingDone) { Rook leftRook = GetTile(0, 7).Piece as Rook; if (leftRook != null && !leftRook.HasMoved) { castlingRights += 'q'; } } } if (whiteKing.HasMoved && blackKing.HasMoved) { castlingRights = "-"; } res += castlingRights + " ";