public Move(Board board, Location from, Location to, CellContent oldDestinationCell) { this.board = board; this.from = from; this.to = to; this.oldDestinationCell = oldDestinationCell; }
public Move PerformMove(Location from, Location to) { Cell old = Get(to); Set(to, Get(from)); Set(from, Cell.Empty); return new Move(this, from, to, old); }
private bool HasMoves(Location to, Location @from, Cell old) { var hasMoves = false; board.Set(to, board.Get(@from)); board.Set(@from, Cell.Empty); if (!IsCheck()) hasMoves = true; board.Set(@from, board.Get(to)); board.Set(to, old); return hasMoves; }
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 void Set(Location location, Cell cell) { cells[location.X, location.Y] = cell; }
public Cell Get(Location location) { return !location.InBoard ? Cell.Empty : cells[location.X, location.Y]; }
public IEnumerable<Location> GetMoves(Location location, Board board) { return ds.SelectMany(d => MovesInOneDirection(location, board, d, infinit)); }