/// <summary> <!-- {{{1 --> Get cells from house index /// </summary> /// <param name="idx"></param> /// <returns></returns> public IEnumerable <Cell> CellsFromHouse(SudokuHouseIndex idx) { idx.AssertWhenInvalid(); return(CellsFromRow(idx) .Concat(CellsFromCol(idx)) .Concat(CellsFromBox(idx))); }
/// <summary> <!-- {{{1 --> Throw exception when specified index is invalid. /// </summary> /// <param name="self"></param> public static void AssertWhenInvalid(this SudokuHouseIndex self) { if (self.IsInvalid()) { var msg = string.Format("Invalid house index: {0}", self.ToInt()); throw new ArgumentOutOfRangeException(msg); } }
/// <summary> <!-- {{{1 --> Convert house index to cell indexes in box. /// </summary> /// <param name="self"></param> /// <returns></returns> public static IEnumerable <SudokuCellIndex> ToCellsIndexInBox(this SudokuHouseIndex self) { // calculate Top Left index in target Box var idx = self.ToInt(); int topleft = (27 * (idx / 3)) + (3 * (idx % 3)); return(Enumerable.Range(0, 3) .SelectMany(y => Enumerable.Range(0, 3) .Select(x => (SudokuCellIndex)(topleft + x + 9 * y)))); }
/// <summary> <!-- {{{1 --> Convert house index to cell indexes in column. /// </summary> /// <param name="self"></param> /// <returns></returns> public static IEnumerable <SudokuCellIndex> ToCellsIndexInCol(this SudokuHouseIndex self) { return(Enumerable.Range(0, 9) .Select(x => (SudokuCellIndex)(9 * x + self.ToInt()))); }
/// <summary> <!-- {{{1 --> Convert house index to cell indexes in row. /// </summary> /// <param name="self"></param> /// <returns></returns> public static IEnumerable <SudokuCellIndex> ToCellsIndexInRow(this SudokuHouseIndex self) { return(SudokuCellIndexExtension.IndexList() .Skip(self.ToInt() * 9).Take(9)); }
/// <summary> <!-- {{{1 --> Convert the house index to String /// </summary> /// <param name="self"></param> /// <returns></returns> public static string ToStr(this SudokuHouseIndex self) { return(self.ToInt().ToString()); }
/// <summary> <!-- {{{1 --> Convert the house index to Integer /// </summary> /// <param name="self"></param> /// <returns></returns> public static int ToInt(this SudokuHouseIndex self) { return((int)self); }
/// <summary> <!-- {{{1 --> Return true if the index is invalid. /// </summary> /// <param name="self"></param> /// <returns></returns> public static bool IsInvalid(this SudokuHouseIndex self) { return((SudokuHouseIndex._1 > self) || (SudokuHouseIndex._9 < self)); }
/// <summary> <!-- {{{1 --> Get cells from box index /// </summary> /// <param name="idx"></param> /// <returns></returns> public IEnumerable <Cell> CellsFromBox(SudokuHouseIndex idx) { idx.AssertWhenInvalid(); return(idx.ToCellsIndexInBox().Select(x => this.board.ElementAt(x.ToInt()))); }