public Move CopyMove() { PiecePossibleMove piecePossibleMove = new PiecePossibleMove(new Point(_move.MoveToPosition.X, _move.MoveToPosition.Y), new Point(_move.FromPosition.X, _move.FromPosition.Y)) { IsCastlingMove = _move.IsCastlingMove, CastlingRook = _move.CastlingRook, RookPosition = new Point(_move.RookPosition.X, _move.RookPosition.Y) }; return(new Move(piecePossibleMove, _actualPiece, _takenPiece, _promotionPiece)); }
public Move(PiecePossibleMove move, BasePiece piece, BasePiece takenPiece = null, BasePiece promotionPiece = null) { _move = move; _fromPosition = (int)piece.Position.X + (int)piece.Position.Y * 8; _toPosition = (int)move.MoveToPosition.X + (int)move.MoveToPosition.Y * 8; _actualPiece = piece.CopyPiece(); _isPieceTaken = takenPiece == null ? false : true; _takenPiece = takenPiece == null ? null : takenPiece.CopyPiece(); _takenPiecePosition = takenPiece == null ? 0 : (int)takenPiece.Position.X + (int)takenPiece.Position.Y * 8; _isCastling = move.IsCastlingMove; _castlingRook = move.IsCastlingMove ? move.CastlingRook.CopyPiece() : null; _castlingRookFromPosition = move.IsCastlingMove ? (int)move.CastlingRook.Position.X + (int)move.CastlingRook.Position.Y * 8 : 0; _castlingRookToPosition = move.IsCastlingMove ? (int)move.RookPosition.X + (int)move.RookPosition.Y * 8 : 0; _isPromotionMove = promotionPiece == null ? false : true; _promotionPiece = promotionPiece == null ? null : promotionPiece.CopyPiece(); _hasMadeMove = true; }
public bool IsValidMove(PiecePossibleMove move, bool isWhite) { Point?location = FindKingLocation(isWhite); if (location.HasValue) { Point kingLocation = location.Value; if (move.IsCastlingMove) { if (move.RookPosition.X == 0) { for (int x = 4; x > 1; x--) { if (IsAttacked(new Point(x, kingLocation.Y), isWhite)) { return(false); } } return(true); } else { for (int x = 4; x < 7; x++) { if (IsAttacked(new Point(x, kingLocation.Y), isWhite)) { return(false); } } return(true); } } if (IsAttacked(kingLocation, isWhite)) { return(false); } else { return(true); } } else { return(true); } }
/// <summary> /// Makes a move on chess board /// </summary> /// <returns></returns> public void MakeMove(int index, bool useLog) { if (CurrentPiece != null) { AssignCellBlackBorder(); PiecePossibleMove move = CurrentPiecePossibleMoves.Where(m => (int)(m.MoveToPosition.Y * 8 + m.MoveToPosition.X) == index).Single(); BasePiece promotionPiece = null; if (CurrentPiece is Pawn && (Board[index].Position.Y == 0 || Board[index].Position.Y == 7)) { PromotionWindow dialog = new PromotionWindow(CurrentPiece.IsWhite); if (dialog.ShowDialog() == true) { promotionPiece = dialog.SelectedPiece; } } Move madeMove = new Move(move, CurrentPiece, Board[index].Piece, promotionPiece); HistoryOfMoves.Add(madeMove); madeMove.MakeMove(ref Board); LastMadeMove = madeMove.CopyMove(); if (madeMove.IsCastlingMove) { hasCastled[madeMove.IsWhiteMove] = true; } bool isWhite = CurrentPiece.IsWhite; CurrentPiece = null; bool isGameOver = IsGameOver(!isWhite); bool isChecked = IsAttacked((Point)FindKingLocation(!isWhite), !isWhite); if (isGameOver) { MessageBox.Show("Game over! " + (isWhite ? "White" : "Black") + " wins!"); GameFinished = true; } if (useLog) { WriteLogMove(isGameOver, isChecked); } } }
public Move MakeFakeMove(PiecePossibleMove move, BasePiece actualPiece) { int index = (int)(move.MoveToPosition.Y * 8 + move.MoveToPosition.X); Move madeMove; //TODO if piece is a pawn then promote to queen if it's at row 0 or row 7 if (actualPiece is Pawn && (move.MoveToPosition.Y == 0 || move.MoveToPosition.Y == 7)) { madeMove = new Move(move, actualPiece, Board[index].Piece, new Queen((int)move.MoveToPosition.X, (int)move.MoveToPosition.Y, actualPiece.IsWhite)); } else { madeMove = new Move(move, actualPiece, Board[index].Piece); } HistoryOfMoves.Add(madeMove); madeMove.MakeMove(ref Board); if (madeMove.IsCastlingMove) { hasCastled[madeMove.IsWhiteMove] = true; } LastMadeMove = madeMove.CopyMove(); return(madeMove); }