Beispiel #1
0
 private void BtnGenerate_Click(object sender, EventArgs e)
 {
     if (!asCSP)
     {
         MessageBox.Show("La génération de sudoku n'est disponible qu'avec une résolution par CSP.");
         return;
     }
     LabelWaitingGeneration.Visible = true;
     LabelWaitingGeneration.Update();
     actualDimensions = new GridDimensions(9, 9, 3, 3);
     csp.Dimensions   = actualDimensions;
     csp.ClearLists();
     recreateCells();
     csp.GenerateSudoku(this.SliderCellsGenerated.Value);
     LabelWaitingGeneration.Visible = false;
     if (irregularSudoku)
     {
         UpdateGridDisplay_Irregular();
     }
     else
     {
         UpdateGridDisplay_Regular();
     }
     state = State.SUDOKU_LOADED;
 }
Beispiel #2
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);
        }
Beispiel #3
0
        public Form1()
        {
            InitializeComponent();
            this.AutoSize = true;

            actualDimensions = new GridDimensions(9, 9, 3, 3);
            createCells();
            Sudoku.AutoSize = true;

            state           = State.INITIAL_STATE;
            asCSP           = true;
            irregularSudoku = false;
        }
Beispiel #4
0
        private GridDimensions FindGridDimensions_Regular(string gridContent)
        {
            string[] columns                  = gridContent.Split('\n');
            int      squareSizeX              = -1;
            int      numberOfSquaresX         = 1;
            int      squareSizeY              = -1;
            int      numberOfSquaresY         = 0;
            int      nonEmptyColumns          = 0;
            int      numberOfLinesBeforeLimit = 0;

            foreach (string column in columns)
            {
                if (column.Length == 0)
                {
                    continue;
                }
                nonEmptyColumns++;
                string[] columnSplitInSquares = column.Split('!');

                if (numberOfSquaresY == 0)
                {
                    numberOfSquaresY = columnSplitInSquares.Length;
                }
                else if (numberOfSquaresY != columnSplitInSquares.Length)
                {
                    MessageBox.Show("Invalid grid !");
                    return(null);
                }

                foreach (string splitColumn in columnSplitInSquares)
                {
                    if (squareSizeY == -1)
                    {
                        squareSizeY = splitColumn.Length;
                    }
                    else if (squareSizeY != splitColumn.Length)
                    {
                        MessageBox.Show("Invalid grid !");
                        return(null);
                    }
                }

                if (column[0] == '-')
                {
                    numberOfSquaresX++;
                    if (squareSizeX == -1)
                    {
                        squareSizeX = numberOfLinesBeforeLimit;
                    }
                    else if (squareSizeX != numberOfLinesBeforeLimit)
                    {
                        MessageBox.Show("Invalid grid !");
                        return(null);
                    }
                    numberOfLinesBeforeLimit = 0;
                }
                else
                {
                    numberOfLinesBeforeLimit++;
                }
            }
            if (squareSizeX != numberOfLinesBeforeLimit)
            {
                MessageBox.Show("Invalid grid !");
                return(null);
            }

            int sizeY = numberOfSquaresY * squareSizeY;
            int sizeX = numberOfSquaresX * squareSizeX;

            GridDimensions dimensions = new GridDimensions(sizeX, sizeY, squareSizeX, squareSizeY);

            if (dimensions.IsValid())
            {
                return(dimensions);
            }
            else
            {
                MessageBox.Show("Grille Invalide");
                return(null);
            }
        }
Beispiel #5
0
        private bool DecodeGrid_Irregular(string gridContent)
        {
            if (!asCSP)
            {
                MessageBox.Show("Les sudokus irréguliers ne sont solvables qu'avec un CSP.");
                return(false);
            }
            gridContent = gridContent.Replace("\r", "")
                          .Replace(" ", "");
            // Find size of grid
            GridDimensions dimensions = FindGridDimensions_Irregular(gridContent);

            if (dimensions == null)
            {
                return(false);
            }
            if (dimensions.GridSizeX > colors.Count)
            {
                MessageBox.Show("Les sudokus irréguliers sont pour le moment limités à une taille de "
                                + colors.Count.ToString()
                                + "x"
                                + colors.Count.ToString()
                                + ".");
                return(false);
            }

            // Recuperate zones
            string[] zones = GetZones(gridContent, dimensions.GridSizeX);
            if (zones == null)
            {
                return(false);
            }

            actualDimensions = dimensions;
            csp.ClearLists();
            recreateCells();

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

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

            for (int i = 0; i < dimensions.GridSizeX; ++i)
            {
                for (int j = 0; j < dimensions.GridSizeX; ++j)
                {
                    Cell cell = new Cell(i, j, dimensions.GridSizeX);
                    cell.Value = Convert.ToChar(columns[i][j]);
                    int  value;
                    char zoneChar = zones[i][j];
                    if (zoneChar >= 'A')
                    {
                        value = 9 + zoneChar - 'A';
                    }
                    else
                    {
                        value = zoneChar - '1';
                    }
                    cell.ZoneNumber = value;
                    GraphNode node = new GraphNode(cell);
                    csp.Nodes.Add(node);
                }
            }
            csp.GenerateArcs();
            return(true);
        }