public void ExecuteCommand(ChessCommand command) { int posX = command.PositionXY[0]; int posY = command.PositionXY[1]; ChessSquare[,] chessSquares = this.GetSquares(); AbstractChessPiece chessPiece = chessSquares[posX, posY].ChessPiece; if (chessPiece != null) { if (this.CurrentPiece != null && this.CurrentPiece.IsWhite != chessPiece.IsWhite) { this.PlaceCurrentPiece(posX, posY); } else { this.TakePiece(chessPiece); } //TestIfCheck(); } else { this.PlaceCurrentPiece(posX, posY); //TestIfCheck(); } }
/// <summary> /// Take a ChessPiece. /// </summary> /// <param name="pPiece">The ChessPiece to take.</param> public void TakePiece(AbstractChessPiece pPiece) { if (pPiece.IsWhite != this.IsWhite) { this.CurrentPiece = pPiece; } }
/// <summary> /// Try if a future position for a ChessPiece is allowed (if the move will not make the player check). /// </summary> /// <param name="pPosXY">The position X and Y of a ChessPiece.</param> /// <param name="pOffsetXY">The offset X and Y where the piece will move.</param> /// <returns></returns> public bool TryMoveIsAllowed(int[] pPosXY, int[] pOffsetXY) { this.UpdateTempBoard(); AbstractChessPiece piece = this.TempBoard.ChessSquares[pPosXY[0], pPosXY[1]].ChessPiece; this.TempBoard.MoveChessPiece(piece, pPosXY[0] + pOffsetXY[0], pPosXY[1] + pOffsetXY[1]); this.TempBoard.RemoveChessPiece(pPosXY[0], pPosXY[1]); return(!this.TempBoard.IsCheck(piece.IsWhite)); }
/// <summary> /// Get the new position of a ChessPiece using a ChessMove. /// </summary> /// <param name="pChessPiece">The ChessPiece.</param> /// <param name="pMove">The ChessMove.</param> /// <returns>The new position of the ChessPiece.</returns> public int[] GetNewPositionXY(AbstractChessPiece pChessPiece, ChessMove pMove) { int[] oldPositionXY = GetChessPiecePosition(pChessPiece); int[] newPositionXY = new int[2]; newPositionXY[0] = oldPositionXY[0] + pMove.OffsetXY[0]; newPositionXY[1] = oldPositionXY[1] + pMove.OffsetXY[1]; return(newPositionXY); }
/// <summary> /// Place a ChessPiece on the ChessSquare. /// </summary> /// <param name="pChessPiece">The ChessPiece.</param> /// <param name="pPosX">The X position.</param> /// <param name="pPosY">The Y position.</param> /// <returns>True if the ChessPiece has been removed, false if it has not.</returns> public bool PlaceChessPiece(AbstractChessPiece pChessPiece, int pPosX, int pPosY) { bool hasBennPlaced = false; if (IsOnBoard(pPosX, pPosY)) { this.ChessSquares[pPosX, pPosY].ChessPiece = pChessPiece; hasBennPlaced = true; } return(hasBennPlaced); }
/// <summary> /// Move a ChessPiece. /// </summary> /// <param name="pChessPiece">The ChessPiece to move.</param> /// <param name="pPosX">The future X position of the ChessPiece.</param> /// <param name="pPosY">The future Y position of the ChessPiece.</param> /// <returns>True if the ChessPiece has been moved, false if it's not (for example, if it want to move on a friendly ChessPiece).</returns> public bool MoveChessPiece(AbstractChessPiece pChessPiece, int pPosX, int pPosY) { bool hasMoved = false; int[] oldPosXY = GetChessPiecePosition(pChessPiece); if (this.ChessSquares[pPosX, pPosY].ChessPiece != null) { if (this.ChessSquares[pPosX, pPosY].ChessPiece.IsWhite != pChessPiece.IsWhite) { this.PiecesOut.Add(this.ChessSquares[pPosX, pPosY].ChessPiece); } } PlaceChessPiece(pChessPiece, pPosX, pPosY); RemoveChessPiece(oldPosXY[0], oldPosXY[1]); pChessPiece.HasMoved = true; this.UpdateTempBoard(); return(hasMoved); }
/// <summary> /// Get a ChessPiece position. /// </summary> /// <param name="pChessPiece">The ChessPiece.</param> /// <returns>The ChessPiece position (int[]) if it has been found, int[-1, -1] if not found.</returns> public int[] GetChessPiecePosition(AbstractChessPiece pChessPiece) { int[] positionXY = new int[2]; // If the piece is not found, returns -1, -1 positionXY[0] = -1; positionXY[1] = -1; // Go through all squares to find the piece's position for (int i = 0; i < this.ChessSquares.GetLength(0); i++) { for (int j = 0; j < this.ChessSquares.GetLength(1); j++) { if (this.ChessSquares[i, j].ChessPiece == pChessPiece) { positionXY[0] = i; positionXY[1] = j; } } } return(positionXY); }
/// <summary> /// Get all future possible positions for a ChessPiece, using his ChessMoves. /// </summary> /// <param name="pChessPiece">The ChessPiece.</param> /// <returns>A List of int[] containing all possible positions.</returns> public List <int[]> GetPossiblePositions(AbstractChessPiece pChessPiece) { List <int[]> lstPossiblePositions = new List <int[]>(); int[] posXY = GetChessPiecePosition(pChessPiece); int offsetX, offsetY; foreach (ChessMove move in pChessPiece.Moves) { offsetX = move.OffsetXY[0]; offsetY = move.OffsetXY[1]; if (move.CanGoOver) { // Can go over & is repeatable (never used in chess game) if (move.Repeatable) { while (IsOnBoard(posXY[0] + offsetX, posXY[1] + offsetY)) { // MOVE if (this.ChessSquares[posXY[0] + offsetX, posXY[1] + offsetY].ChessPiece == null) { lstPossiblePositions.Add(new int[] { posXY[0] + offsetX, posXY[1] + offsetY }); } else // ATTACK { // TODO } offsetX += move.OffsetXY[0]; offsetY += move.OffsetXY[1]; } } else // Can go over & isn't repeatable (Knight) { if (IsOnBoard(posXY[0] + offsetX, posXY[1] + offsetY)) { // MOVE if (this.ChessSquares[posXY[0] + offsetX, posXY[1] + offsetY].ChessPiece == null) { // Test if the move make the player check (if yes, don't allow the move) if (TryMoveIsAllowed(posXY, new int[2] { offsetX, offsetY })) { lstPossiblePositions.Add(new int[] { posXY[0] + offsetX, posXY[1] + offsetY }); // The move is allowed and added to the list } } else // ATTACK { if (this.ChessSquares[posXY[0] + offsetX, posXY[1] + offsetY].ChessPiece.IsWhite != pChessPiece.IsWhite) { // Test if the move make the player check (if yes, don't allow the move) if (TryMoveIsAllowed(posXY, new int[2] { offsetX, offsetY })) { lstPossiblePositions.Add(new int[] { posXY[0] + offsetX, posXY[1] + offsetY }); } } } } } } else // Can't go over & is repeatable (Bishop, Rook, Queen) { if (move.Repeatable) { bool over = false; while (IsOnBoard(posXY[0] + offsetX, posXY[1] + offsetY) && !over) { // MOVE if (this.ChessSquares[posXY[0] + offsetX, posXY[1] + offsetY].ChessPiece == null) { // Test if the move make the player check (if yes, don't allow the move) if (TryMoveIsAllowed(posXY, new int[2] { offsetX, offsetY })) { lstPossiblePositions.Add(new int[] { posXY[0] + offsetX, posXY[1] + offsetY }); } } else // ATTACK { if (this.ChessSquares[posXY[0] + offsetX, posXY[1] + offsetY].ChessPiece.IsWhite != pChessPiece.IsWhite) { // Test if the move make the player check (if yes, don't allow the move) if (TryMoveIsAllowed(posXY, new int[2] { offsetX, offsetY })) { lstPossiblePositions.Add(new int[] { posXY[0] + offsetX, posXY[1] + offsetY }); } } over = true; } offsetX += move.OffsetXY[0]; offsetY += move.OffsetXY[1]; } } else // Can't go over & isn't repeatable (Pawn, King) { if (IsOnBoard(posXY[0] + offsetX, posXY[1] + offsetY)) { // MOVE if (this.ChessSquares[posXY[0] + offsetX, posXY[1] + offsetY].ChessPiece == null) { if (!move.IsAttackMoveOnly) { // Test if the move make the player check (if yes, don't allow the move) if (TryMoveIsAllowed(posXY, new int[2] { offsetX, offsetY })) { lstPossiblePositions.Add(new int[] { posXY[0] + offsetX, posXY[1] + offsetY }); } } } else // ATTACK { if (this.ChessSquares[posXY[0] + offsetX, posXY[1] + offsetY].ChessPiece.IsWhite != pChessPiece.IsWhite) { if (!move.IsNotAttackMove) { // Test if the move make the player check (if yes, don't allow the move) if (TryMoveIsAllowed(posXY, new int[2] { offsetX, offsetY })) { lstPossiblePositions.Add(new int[] { posXY[0] + offsetX, posXY[1] + offsetY }); } } } } } } } } // Special moves, hard-coded (sorry) if (!pChessPiece.HasMoved) { // White Pawn if (pChessPiece.Type == ChessPieceType.Pawn && pChessPiece.IsWhite) { if (this.ChessSquares[posXY[0], posXY[1] + 1].ChessPiece == null && this.ChessSquares[posXY[0], posXY[1] + 2].ChessPiece == null) { // Test if the move make the player check (if yes, don't allow the move) offsetX = 0; offsetY = 2; if (TryMoveIsAllowed(posXY, new int[2] { offsetX, offsetY })) { lstPossiblePositions.Add(new int[] { posXY[0] + offsetX, posXY[1] + offsetY }); } } } // Black Pawn if (pChessPiece.Type == ChessPieceType.Pawn && !pChessPiece.IsWhite) { if (this.ChessSquares[posXY[0], posXY[1] - 1].ChessPiece == null && this.ChessSquares[posXY[0], posXY[1] - 2].ChessPiece == null) { // Test if the move make the player check (if yes, don't allow the move) offsetX = 0; offsetY = -2; if (TryMoveIsAllowed(posXY, new int[2] { offsetX, offsetY })) { lstPossiblePositions.Add(new int[] { posXY[0] + offsetX, posXY[1] + offsetY }); } } } } return(lstPossiblePositions); }
/// <summary> /// Try a ChessMove on a ChessPiece. /// </summary> /// <param name="pChessPiece">The ChessPiece.</param> /// <param name="pMove">The ChessMove.</param> /// <returns>True if the ChessPiece is on board after the move.</returns> public bool TryMoveIsOnBoard(AbstractChessPiece pChessPiece, ChessMove pMove) { int[] newPositionXY = GetNewPositionXY(pChessPiece, pMove); return(IsOnBoard(newPositionXY[0], newPositionXY[1])); }
/// <summary> /// Remove a ChessPiece from ChessSquares. /// </summary> /// <param name="pChessPiece">The ChessPiece.</param> /// <returns>True if the ChessPiece has been removed, false if it has not.</returns> public bool RemoveChessPiece(AbstractChessPiece pChessPiece) { int[] posXY = GetChessPiecePosition(pChessPiece); return(RemoveChessPiece(posXY[0], posXY[1])); }
/// <summary> /// Place a ChessPiece on the ChessBoard. /// </summary> /// <param name="pPiece">The ChessPiece to place.</param> /// <param name="pPosX">The future X position of the ChessPiece.</param> /// <param name="pPosY">The future Y position of the ChessPiece.</param> public void PlacePiece(AbstractChessPiece pPiece, int pPosX, int pPosY) { this.Board.MoveChessPiece(pPiece, pPosX, pPosY); this.PlayerTurn = !this.PlayerTurn; }
/// <summary> /// Take a piece with the current ChessPlayer. /// </summary> /// <param name="pPiece">The ChessPiece to take.</param> public void TakePiece(AbstractChessPiece pPiece) { this.CurrentPlayer.TakePiece(pPiece); }
/// <summary> /// Get all future possible positions for a ChessPiece. /// </summary> /// <param name="pPiece">The ChessPiece.</param> /// <returns>A list of int[] containing all future possible positions.</returns> public List <int[]> GetPossiblePositions(AbstractChessPiece pPiece) { return(this.Board.GetPossiblePositions(pPiece)); }
/// <summary> /// Get the position of a ChessPiece. /// </summary> /// <param name="pPiece">The ChessPiece we want to know the position on the ChessBoard.</param> /// <returns>A int[] with the ChessPiece's coordonates.</returns> public int[] GetChessPiecePosition(AbstractChessPiece pPiece) { return(this.Board.GetChessPiecePosition(pPiece)); }
/// <summary> /// Constructor of ChessSquare class. /// </summary> /// <param name="pIsWhite">If the ChessSquare is white.</param> public ChessSquare(bool pIsWhite) { this.IsWhite = pIsWhite; this.ChessPiece = null; }