Exemplo n.º 1
0
        public SudokuStruct setSudokuLevel(SudokuStruct oldMatrix, string level)
        {
            SudokuStruct NewMatrix = new SudokuStruct();

            //Array.Copy(oldMatrix,NewMatrix,9);
            int[] FixedIndex = new int[9] {
                0, 1, 2, 3, 4, 5, 6, 7, 8
            };

            Random RandomGen = new Random();
            int    indx;
            int    numofiterations = 0;

            for (int Rows = 0; Rows < 9; Rows++)
            {
                Array.Copy(oldMatrix.SudokuCells, (Rows * 9), NewMatrix.SudokuCells, (9 * Rows), 9);
            }
            switch (level)
            {
            case "Easy":
                numofiterations = 2;
                break;

            case "Medium":
                numofiterations = 3;
                break;

            case "Hard":
                numofiterations = 4;
                break;

            case "Insane":
                numofiterations = 7;
                break;
            }

            for (int row = 0; row < 9; row++)
            {
                int[] Indexes = new int[9];

                Array.Copy(FixedIndex, Indexes, 9);


                for (int hide = 0; hide <= numofiterations; hide++)
                {
                    indx = RandomGen.Next(Indexes.Length);
                    int NumIndx = Indexes[indx];
                    NewMatrix.SetVisibility(false, row, NumIndx);
                    //NewMatrix.SudokuCells[row, NumIndx].IsVisible = false;
                    Indexes = UpdateIndxArray(Indexes, indx);
                }
            }


            for (int col = 0; col < 9; col++)
            {
                int   numhidden = 0;
                int[] Indexes   = FixedIndex;
                for (int row = 0; row < 9; row++)
                {
                    if (!NewMatrix.SudokuCells[row, col].IsVisible)
                    {
                        Indexes[row] = -1;
                        numhidden++;
                    }
                }
                if (numhidden < numofiterations)
                {
                    for (int hide = numhidden; hide <= numofiterations; hide++)
                    {
                        indx = RandomGen.Next(Indexes.Length);
                        int NumIndx = Indexes[indx];
                        if (NumIndx > -1)
                        {
                            NewMatrix.SetVisibility(false, NumIndx, col);
                            //NewMatrix.SudokuCells[NumIndx, col].IsVisible = false;
                            Indexes = UpdateIndxArray(Indexes, indx);
                        }
                        else
                        {
                            Indexes = UpdateIndxArray(Indexes, indx);
                        }
                    }
                }
            }



            return(NewMatrix);
        }