public bool PieceMovingLikeABlackPawn(WPFChess.MainWindow wmw, bool pieceAtDst, int xsrc, int ysrc, int xdst, int ydst) { //on any move can move forward 1 square if destination is free if ((ysrc - ydst == -1) && (!pieceAtDst) && (xsrc - xdst == 0)) { return(true); } //on first move can move forward 2 squares if destination is free and square is empty in between if ((ysrc == 1) && (ysrc - ydst == -2) && (xsrc - xdst == 0)) { var pieceHere = Utility.WhatPieceIsHere(wmw, xsrc, ysrc + 1); if (!pieceAtDst && pieceHere == null) { return(true); } else { return(false); } } //can move forward 1 square diagonally if taking a piece if ((((ysrc - ydst) == -1)) && (pieceAtDst) && (Math.Abs(xsrc - xdst) == 1)) { return(true); } //TODO //can move forward 1 square diagonally into empty square if executing en passant //if destintion square is the last rank (1 or 8) then change to another piece return(false); }
public static void MovePiece(WPFChess.MainWindow wmw, int xsrc, int ysrc, int xdst, int ydst) { //get the piece to move var pieceToMove = Utility.WhatPieceIsHere(wmw, xsrc, ysrc); //take the piece if any in destination and add it to collection of pieces taken var pieceToTake = Utility.WhatPieceIsHere(wmw, xdst, ydst); //get the piece to take if any if (pieceToTake != null) //if there is a piece here take it { wmw.Pieces.Remove(pieceToTake); //remove the piece from board wmw.PiecesTaken.Add(pieceToTake); //add the piece to the list of taken pieces } //move the piece - add and remove from the observable location ChessPiece moveTo = new ChessPiece { Player = pieceToMove.Player, Type = pieceToMove.Type, Pos = new Point(xdst, ydst) //put the piece here }; wmw.Pieces.Add(moveTo); wmw.Pieces.Remove(pieceToMove); //remove the piece }
public static bool IsPieceAtDestinationLocation(WPFChess.MainWindow wmw, int[] array) { if (IsPieceAtThisLocation(wmw, array[2], array[3])) { return(true); } return(false); }
//test method public void MakeSomeMoves(WPFChess.MainWindow wmw) { _ = wmw.Pieces.ElementAt(6); //test MovePiece(wmw, 1, 7, 2, 5); //white knight MovePiece(wmw, 1, 0, 2, 2); //black knight MovePiece(wmw, 4, 4, 2, 2); //nothing _ = Utility.WhatPieceIsHere(wmw, 1, 1); }
public static bool IsPieceAtThisLocation(WPFChess.MainWindow wmw, int x, int y) { var whatPiece = Utility.WhatPieceIsHere(wmw, x, y); if (whatPiece != null) { return(true); } return(false); }
public static void Setup1(WPFChess.MainWindow wmw) { //pawns MovePiece(wmw, 4, 6, 4, 4); MovePiece(wmw, 4, 1, 4, 3); //knights MovePiece(wmw, 6, 7, 5, 5); MovePiece(wmw, 6, 0, 5, 2); //bishops MovePiece(wmw, 5, 7, 4, 6); MovePiece(wmw, 5, 0, 4, 1); }
public static bool IsItYourMove(WPFChess.MainWindow wmw, GameState gs) { var sourcePiece = Utility.WhatPieceIsHere(wmw, gs.array[0], gs.array[1]); if ((sourcePiece.Player == Player.White && gs.WM == "WHITE") || (sourcePiece.Player == Player.Black && gs.WM == "BLACK")) { return(true); } else { return(false); } }
public static void Setup2(WPFChess.MainWindow wmw) { //pawns MovePiece(wmw, 3, 6, 3, 4); MovePiece(wmw, 3, 1, 3, 3); MovePiece(wmw, 2, 6, 2, 4); MovePiece(wmw, 2, 1, 2, 3); //knights MovePiece(wmw, 1, 0, 2, 2); MovePiece(wmw, 1, 7, 2, 5); //queens MovePiece(wmw, 3, 7, 3, 5); MovePiece(wmw, 3, 0, 3, 2); //bishops MovePiece(wmw, 2, 7, 3, 6); MovePiece(wmw, 2, 0, 3, 1); }
public static bool IsClearPath(WPFChess.MainWindow wmw, GameState gs) { bool result = false; var whatPiece = Utility.WhatPieceIsHere(wmw, gs.array[0], gs.array[1]); //any piece moving 1 square doesn't need this check if ((Math.Abs(gs.array[0] - gs.array[2]) <= 1) && (Math.Abs(gs.array[1] - gs.array[3]) <= 1)) { result = true; } //knights dont need this check as they jump over if (whatPiece.Type == PieceType.Knight) { result = true; } return(result); }
public static bool ValidateMove(WPFChess.MainWindow wmw, GameState gs) { //get the pieces at src and dst var pieceAtSrc = Utility.WhatPieceIsHere(wmw, gs.array[0], gs.array[1]); var pieceAtDst = Utility.WhatPieceIsHere(wmw, gs.array[2], gs.array[3]); //check there is a piece to move from here if (pieceAtSrc == null) { return(false); } //if there is a destination piece it cannot be of the same colour as source piece colour, ie. you can't take your own piece! if (pieceAtDst != null && (pieceAtSrc.Player == pieceAtDst.Player)) { return(false); } //check that it is the correct turn of move if ((pieceAtSrc.Player == Player.White && gs.WM == "BLACK") || (pieceAtSrc.Player == Player.Black && gs.WM == "WHITE")) { return(false); } // it is invalid if the destination of the move has a piece that is not of the opposite colour //if ((IsPieceAtDestinationLocation(gs.array)) && (!IsPieceAtSourceDestinationLocationOppositeColour(gs))) // return false; //if (!IsClearPath(gs)) // return false; if (!IsPieceMovingCorrectly(wmw, gs)) { return(false); } //all validated ok return(true); }
public static void SetCastlingFlags(WPFChess.MainWindow wmw, int xFile, int yRank, GameState gs) { var piece = Utility.WhatPieceIsHere(wmw, xFile, yRank); if (piece != null) { //white king has moved if (piece.Player == Player.White && piece.Type == PieceType.King) { gs.WKM = true; } //black king has moved if (piece.Player == Player.Black && piece.Type == PieceType.King) { gs.BKM = true; } //white rook king side moved if (xFile == 7 && yRank == 7) { gs.WRK = true; } //white rook queen side moved if (xFile == 0 && yRank == 7) { gs.WRQ = true; } //black rook king side moved if (xFile == 7 && yRank == 0) { gs.BRK = true; } //black rook queen side moved if (xFile == 0 && yRank == 0) { gs.BRQ = true; } } }
public static void MoveCastleWhenCastling(WPFChess.MainWindow wmw, GameState GS) { if (GS.WKCRS) { Utility.MovePiece(wmw, 7, 7, 5, 7); //move castle over to complete the castle move GS.WKCRS = false; //and reset the flag } if (GS.WKCQS) { Utility.MovePiece(wmw, 0, 7, 3, 7); //move castle over to complete the castle move GS.WKCQS = false; //and reset the flag } if (GS.BKCRS) { Utility.MovePiece(wmw, 7, 0, 5, 0); //move castle over to complete the castle move GS.BKCRS = false; //and reset the flag } if (GS.BKCQS) { Utility.MovePiece(wmw, 0, 0, 3, 0); //move castle over to complete the castle move GS.BKCQS = false; //and reset the flag } }
public static bool IsPieceMovingCorrectly(WPFChess.MainWindow wmw, GameState gs) { bool result = false; var whatPiece = Utility.WhatPieceIsHere(wmw, gs.array[0], gs.array[1]); bool pieceAtDst = IsPieceAtDestinationLocation(wmw, gs.array); PieceMoveValidations pmv = new PieceMoveValidations(); switch (whatPiece.Type) { case PieceType.Pawn: if (whatPiece.Player == Player.White) { if (pmv.PieceMovingLikeAWhitePawn(wmw, pieceAtDst, gs.array[0], gs.array[1], gs.array[2], gs.array[3])) { result = true; } } else { if (pmv.PieceMovingLikeABlackPawn(wmw, pieceAtDst, gs.array[0], gs.array[1], gs.array[2], gs.array[3])) { result = true; } } break; case PieceType.Rook: if (pmv.PieceMovingLikeARook(gs.array[0], gs.array[1], gs.array[2], gs.array[3])) { result = true; } break; case PieceType.Knight: if (pmv.PieceMovingLikeAKnight(gs.array[0], gs.array[1], gs.array[2], gs.array[3])) { result = true; } break; case PieceType.Bishop: if (pmv.PieceMovingLikeABishop(gs.array[0], gs.array[1], gs.array[2], gs.array[3])) { result = true; } break; case PieceType.Queen: if (pmv.PieceMovingLikeAQueen(gs.array[0], gs.array[1], gs.array[2], gs.array[3])) { result = true; } break; case PieceType.King: if (pmv.PieceMovingLikeAKing(gs, gs.array[0], gs.array[1], gs.array[2], gs.array[3])) { result = true; } break; default: break; } return(result); }
public static ChessPiece WhatPieceIsHere(WPFChess.MainWindow wmw, int xRank, int yFile) { var pieceToMove = wmw.Pieces.Where(x => x.Pos.X == xRank && x.Pos.Y == yFile).ToList(); //return the piece return(pieceToMove.FirstOrDefault()); }