public Move(Board board, Location from, Location to, CellContent oldDestinationCell) { this.board = board; this.from = from; this.to = to; this.oldDestinationCell = oldDestinationCell; }
// Определяет мат, шах или пат белым. public string GetStringForWhiteKing(StreamReader reader) { _board = new Board(reader); var isWhiteKingUnderAttack = IsWhiteKingUnderAttack(); var whiteKingHasMoves = WhiteKingSafeMone(); return GetStrinForWhiteKingResult(isWhiteKingUnderAttack, whiteKingHasMoves); }
public string GetWhiteKingStatus(Board board) { var whiteKingHasCheck = WhiteKingHasCheck(board); var whiteHasSafeMoves = WhiteHasSafeMoves(board); if (whiteKingHasCheck) return whiteHasSafeMoves ? "check" : "mate"; return whiteHasSafeMoves ? "ok" : "stalemate"; }
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; }
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); } }
public static void LoadFrom(StreamReader reader) { board = new Board(reader); }
public void Load(StreamReader reader) { board = new Board(reader); }
public IEnumerable<Location> GetMoves(Location location, Board board) { return ds.SelectMany(d => MovesInOneDirection(location, board, d, infinit)); }
public Chess(StreamReader reader) { board = new Board(reader); }