private static IEnumerable <Location> MovesInOneDirection(Location from, Board board, Location dir, bool infinit) { Cell fromCell = board.Get(from); for (int i = 1; i < (infinit ? 8 : 2); i++) { var to = new Location(from.X + dir.X * i, from.Y + dir.Y * i); if (!to.InBoard) { break; } Cell toCell = board.Get(to); if (toCell.Piece == null) { yield return(to); } else { if (toCell.Color != fromCell.Color) { yield return(to); } yield break; } } }
public string GetWhiteStatus() { var underCheck = CheckForWhite(); var canMove = false; foreach (Loc figureLocation in board.Figures(Cell.White)) { foreach (Loc figureMoves in board.Get(figureLocation).Figure.Moves(figureLocation, board)) { Cell oldDest = board.PerformMove(figureLocation, figureMoves); if (!CheckForWhite( )) { canMove = true; } board.PerformUndoMove(figureLocation, figureMoves, oldDest); } } if (underCheck) { if (canMove) { return("check"); } else { return("mate"); } } if (canMove) { return("ok"); } return("stalemate"); }
public string GetWhiteStatus() { bool check = checkToWhiteKing(); bool checkNextMove = true; foreach (Loc oldFigureLoc in board.Figures(Cell.White)) { foreach (Loc newFigureLoc in board.Get(oldFigureLoc).Figure.Moves(oldFigureLoc, board)) { Cell destCell = board.PerformMove(oldFigureLoc, newFigureLoc); if (!checkToWhiteKing()) { checkNextMove = false; break; } board.PerformUndoMove(oldFigureLoc, newFigureLoc, destCell); } } if (check) { return(checkNextMove ? "mate" : "check"); } else if (checkNextMove) { return("stalemate"); } else { return("ok"); } }
private bool WhiteKingHasCheck(Board board) { foreach (var location in board.GetPieces(PieceColor.Black)) { var cell = board.Get(location); var moves = cell.Piece.GetMoves(location, board); if (moves.Any(destination => board.Get(destination).Is(PieceColor.White, Piece.King))) { return true; } } return false; }
private static IEnumerable<Location> MovesInOneDirection(Location from, Board board, Location dir, bool infinit) { CellContent fromCellContent = board.Get(from); for (int i = 1; i < (infinit ? 8 : 2); i++) { var to = new Location(from.X + dir.X*i, from.Y + dir.Y*i); if (!to.InBoard) break; CellContent toCellContent = board.Get(to); if (toCellContent.Piece == null) yield return to; else { if (toCellContent.Color != fromCellContent.Color) yield return to; yield break; } } }
private bool IsSafeMove(Board board, Location pieceLocation, Location moveTo) { var oldLocation = board.Get(moveTo); try { board.Set(moveTo, board.Get(pieceLocation)); board.Set(pieceLocation, CellContent.Empty); return !WhiteKingHasCheck(board); } finally { board.Set(pieceLocation, board.Get(moveTo)); board.Set(moveTo, oldLocation); } }
private bool WhiteHasSafeMoves(Board board) { foreach (var pieceLocation in board.GetPieces(PieceColor.White)) { if (board.Get(pieceLocation).Piece.GetMoves(pieceLocation, board).Any(moveTo => IsSafeMove(board, pieceLocation, moveTo))) { return true; } } return false; }
// Определяет мат, шах или пат белым. public static void SolveTask() { var isCheck = IsBad(); var hasMoves = false; foreach (Location locFrom in board.GetPieces(PieceColor.White)) { foreach (Location locTo in board.Get(locFrom).Piece.GetMoves(locFrom, board)) { var old = board.Get(locTo); board.Set(locTo, board.Get(locFrom)); board.Set(locFrom, CellContent.Empty); if (!IsBad()) { hasMoves = true; } board.Set(locFrom, board.Get(locTo)); board.Set(locTo, old); } } if (isCheck) { if (hasMoves) { Result = "check"; } else { Result = "mate"; } } else if (hasMoves) { Result = "ok"; } else { Result = "stalemate"; } }
public void Undo() { board.Set(from, board.Get(to)); board.Set(to, oldDestinationCell); }