Пример #1
0
        /// <param name="skipIndex">line index to skip</param>
        public static IEnumerable <Definition.Element> GetElementsInOtherGridLine(this Definition.Grid sourceGrid, int skipIndex, Definition.LineType lineType)
        {
            IEnumerable <Definition.GridLine> lines;

            switch (lineType)
            {
            case Definition.LineType.Row:
                lines = sourceGrid.Rows;
                break;

            case Definition.LineType.Column:
                lines = sourceGrid.Columns;
                break;

            default: throw new NotImplementedException();
            }

            foreach (var line in lines)
            {
                if (line.Index != skipIndex)
                {
                    foreach (var element in line.Elements)
                    {
                        yield return(element);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// get adjacent grid by direction
        /// </summary>
        /// <remarks>
        /// 0 1 2
        /// 3 4 5
        /// 6 7 8
        /// </remarks>
        private static bool TryGetAdjacentGrid(this Definition.Grid sourceGrid, GridAdjacentDirection direction, out Definition.Grid grid)
        {
            int index       = sourceGrid.Index;
            int targetIndex = -1;

            switch (direction)
            {
            case GridAdjacentDirection.Up:
            {
                targetIndex = index - 3;
            }
            break;

            case GridAdjacentDirection.Down:
            {
                targetIndex = index + 3;
            }
            break;

            case GridAdjacentDirection.Left:
            {
                if (index % 3 > 0)
                {
                    targetIndex = index - 1;
                }
            }
            break;

            case GridAdjacentDirection.Right:
            {
                if (index % 3 < 2)
                {
                    targetIndex = index + 1;
                }
            }
            break;

            default: throw new NotImplementedException();
            }

            if (targetIndex < 0)
            {
                grid = null;
                return(false);
            }
            else
            {
                grid = sourceGrid.Sudoku.Grids[targetIndex];
                return(true);
            }
        }
Пример #3
0
        public static IEnumerable <Definition.Element> GetElementsInCurrentGridLine(this Definition.Grid sourceGrid, int layerIndex, Definition.LineType lineType)
        {
            IEnumerable <Definition.GridLine> lines;

            switch (lineType)
            {
            case Definition.LineType.Row:
                lines = sourceGrid.Rows;
                break;

            case Definition.LineType.Column:
                lines = sourceGrid.Columns;
                break;

            default: throw new NotImplementedException();
            }

            return(lines.Skip(layerIndex).First().Elements);
        }
Пример #4
0
            private void printGrid(Definition.Grid grid, int gridIndex, int currentCursorTop)
            {
                if (grid == null)
                {
                    throw new ArgumentNullException("grid");
                }

                int cursorPosH = gridIndex % 3 * 10;
                int cursorPosV = gridIndex / 3 * 4 + currentCursorTop;

                bool firstColumn = gridIndex % 3 == 0;
                bool lastColumn  = gridIndex % 3 == 2;

                bool firstRow = gridIndex < 3;
                bool lastRow  = gridIndex > 5;

                if (lastColumn)
                {
                    cursorPosH--;
                }

                if (firstRow)
                {
                    Console.SetCursorPosition(lastColumn ? cursorPosH - 1 : cursorPosH, cursorPosV + 0);
                    if (firstColumn)
                    {
                        Console.Write(grid_left_top);
                    }
                    for (int i = 0; i < 3; i++)
                    {
                        Console.Write(grid_horizontal);
                    }
                    Console.Write(lastColumn ? grid_right_top : grid_top_cross);
                }

                Console.SetCursorPosition(cursorPosH, cursorPosV + 1);
                if (firstColumn)
                {
                    Console.Write(grid_vertical);
                }
                if (!lastColumn)
                {
                    Console.Write(whitespace);
                }
                Console.Write(grid.Row1);
                Console.Write(grid_vertical);

                Console.SetCursorPosition(cursorPosH, cursorPosV + 2);
                if (firstColumn)
                {
                    Console.Write(grid_vertical);
                }
                if (!lastColumn)
                {
                    Console.Write(whitespace);
                }
                Console.Write(grid.Row2);
                Console.Write(grid_vertical);

                Console.SetCursorPosition(cursorPosH, cursorPosV + 3);
                if (firstColumn)
                {
                    Console.Write(grid_vertical);
                }
                if (!lastColumn)
                {
                    Console.Write(whitespace);
                }
                Console.Write(grid.Row3);
                Console.Write(grid_vertical);

                Console.SetCursorPosition(lastColumn ? cursorPosH - 1 : cursorPosH, cursorPosV + 4);
                if (firstColumn)
                {
                    Console.Write(lastRow ? grid_left_bottom : grid_left_cross);
                }
                for (int i = 0; i < 3; i++)
                {
                    Console.Write(grid_horizontal);
                }
                Console.Write(lastColumn ?
                              (lastRow ? grid_right_bottom : grid_right_cross) :
                              (lastRow ? grid_bottom_cross : grid_cross));
            }
Пример #5
0
        public static void GetOtherGrids(this Definition.Grid sourceGrid, Definition.LineType gridLineType, out Definition.Grid otherGrid1, out Definition.Grid otherGrid2)
        {
            int currentLayer = GetLayerWithOppositeLineType(sourceGrid.Index, gridLineType);

            switch (gridLineType)
            {
            case Definition.LineType.Row:
            {
                switch (currentLayer)
                {
                case 0:
                {
                    sourceGrid.TryGetAdjacentGrid(GridAdjacentDirection.Right, out otherGrid1);
                    otherGrid1.TryGetAdjacentGrid(GridAdjacentDirection.Right, out otherGrid2);
                }
                break;

                case 1:
                {
                    sourceGrid.TryGetAdjacentGrid(GridAdjacentDirection.Left, out otherGrid1);
                    sourceGrid.TryGetAdjacentGrid(GridAdjacentDirection.Right, out otherGrid2);
                }
                break;

                case 2:
                {
                    sourceGrid.TryGetAdjacentGrid(GridAdjacentDirection.Left, out otherGrid2);
                    otherGrid2.TryGetAdjacentGrid(GridAdjacentDirection.Left, out otherGrid1);
                }
                break;

                default: throw new NotImplementedException();
                }
            }
            break;

            case Definition.LineType.Column:
            {
                switch (currentLayer)
                {
                case 0:
                {
                    sourceGrid.TryGetAdjacentGrid(GridAdjacentDirection.Down, out otherGrid1);
                    otherGrid1.TryGetAdjacentGrid(GridAdjacentDirection.Down, out otherGrid2);
                }
                break;

                case 1:
                {
                    sourceGrid.TryGetAdjacentGrid(GridAdjacentDirection.Up, out otherGrid1);
                    sourceGrid.TryGetAdjacentGrid(GridAdjacentDirection.Down, out otherGrid2);
                }
                break;

                case 2:
                {
                    sourceGrid.TryGetAdjacentGrid(GridAdjacentDirection.Up, out otherGrid2);
                    otherGrid2.TryGetAdjacentGrid(GridAdjacentDirection.Up, out otherGrid1);
                }
                break;

                default: throw new NotImplementedException();
                }
            }
            break;

            default: throw new NotImplementedException();
            }
        }