예제 #1
0
        private bool DecodeGrid_Regular(string gridContent)
        {
            gridContent = gridContent.Replace("\r", "")
                          .Replace(" ", "");
            // Find if the grid is valid
            GridDimensions dimensions = FindGridDimensions_Regular(gridContent);

            if (dimensions == null)
            {
                return(false);
            }

            if (!asCSP && dimensions.GridSizeY != 9)
            {
                MessageBox.Show("La taille de sudoku supportée est de 9*9 uniquement avec une structure de tableau.");
                return(false);
            }

            if (asCSP)
            {
                csp.ClearLists();
                csp.Dimensions = dimensions;
            }
            actualDimensions = dimensions;
            recreateCells();

            string cleanContent = gridContent.Replace("!", "")
                                  .Replace(" ", "")
                                  .Replace("-", "");

            string[] columns     = cleanContent.Split('\n');
            int      actualIndex = 0;

            foreach (string column in columns)
            {
                if (column.Length == 0)
                {
                    continue;
                }
                for (int j = 0; j < column.Length; ++j)
                {
                    if (!asCSP)
                    {
                        grid.SudokuGrid[actualIndex, j].Value = Convert.ToChar(column[j]);
                    }
                    else
                    {
                        Cell cell = new Cell(actualIndex, j, dimensions.GridSizeX);
                        cell.Value      = Convert.ToChar(column[j]);
                        cell.ZoneNumber = dimensions.NumberOfSquaresOnLine() * (actualIndex / dimensions.SquareSizeX) + j / dimensions.SquareSizeY;
                        GraphNode node = new GraphNode(cell);
                        csp.Nodes.Add(node);
                    }
                }
                actualIndex++;
            }
            csp.GenerateArcs();
            return(true);
        }
예제 #2
0
        private void createCells(string[] zones = null)
        {
            int size = actualDimensions.GridSizeX;

            cells = new Label[size, size];
            // design inspired by code found at https://playwithcsharpdotnet.blogspot.com/
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    cells[i, j]             = new Label();
                    cells[i, j].Font        = new Font(SystemFonts.DefaultFont.FontFamily, 180 / size);
                    cells[i, j].Size        = new Size(360 / size, 360 / size);
                    cells[i, j].BorderStyle = BorderStyle.Fixed3D;
                    cells[i, j].TextAlign   = ContentAlignment.MiddleCenter;
                    cells[i, j].ForeColor   = SystemColors.ControlDarkDark;
                    cells[i, j].Location    = new Point(i * 360 / size, j * 360 / size);
                    if (zones == null)
                    {
                        cells[i, j].BackColor = ((i / actualDimensions.NumberOfSquaresOnLine()) + (j / actualDimensions.NumberOfSquaresOnColumn())) % 2 == 0 ? SystemColors.Control : Color.LightGray;
                    }
                    else
                    {
                        int  value;
                        char zoneChar = zones[i][j];
                        if (zoneChar >= 'A')
                        {
                            value = 9 + zoneChar - 'A';
                        }
                        else
                        {
                            value = zoneChar - '1';
                        }
                        cells[i, j].BackColor = colors[value];
                    }

                    Sudoku.Controls.Add(cells[i, j]);
                }
            }
        }