Example #1
0
    void InitArray()
    {
        for (int i = 0; i < ArrayCout; i++)
        {
            for (int j = 0; j < ArrayCout; j++)
            {
                if (arrayInfor[i, j].tempNum.Count <= 0)
                {
                    continue;
                }

                int index   = Random.Range(0, arrayInfor[i, j].tempNum.Count);
                int tempNum = arrayInfor[i, j].tempNum[index];
                arrayInfor[i, j].Num = tempNum;
                for (int m = 0; m < 9; m++)
                {
                    Vector2     tempVec2  = arrayInfor[i, j].array01[m];
                    SudokuInfor tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];

                    for (int n = 0; n < tempInfor.tempNum.Count; n++)
                    {
                        if (tempInfor.tempNum[n] == tempNum)
                        {
                            tempInfor.tempNum.RemoveAt(n);
                        }
                    }

                    tempVec2  = arrayInfor[i, j].array02[m];
                    tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];

                    for (int n = 0; n < tempInfor.tempNum.Count; n++)
                    {
                        if (tempInfor.tempNum[n] == tempNum)
                        {
                            tempInfor.tempNum.RemoveAt(n);
                        }
                    }

                    tempVec2  = arrayInfor[i, j].array03[m];
                    tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];

                    for (int n = 0; n < tempInfor.tempNum.Count; n++)
                    {
                        if (tempInfor.tempNum[n] == tempNum)
                        {
                            tempInfor.tempNum.RemoveAt(n);
                        }
                    }
                }
            }
        }
    }
Example #2
0
    bool CheckNumber(int X, int Y, int iNumber)
    {
        if (_SudokuInfor == null)
        {
            return(false);
        }

        SudokuInfor tempSudoku = _SudokuInfor[X, Y];

        for (int i = 0; i < 9; i++)
        {
            Vector2     tempVec2        = tempSudoku.array01[i];
            SudokuInfor tempAboutSudoku = _SudokuInfor[(int)tempVec2.x, (int)tempVec2.y];

            if (tempAboutSudoku != tempSudoku &&
                tempAboutSudoku.Num == iNumber)
            {
                return(false);
            }

            tempVec2        = tempSudoku.array02[i];
            tempAboutSudoku = _SudokuInfor[(int)tempVec2.x, (int)tempVec2.y];

            if (tempAboutSudoku != tempSudoku &&
                tempAboutSudoku.Num == iNumber)
            {
                return(false);
            }

            tempVec2        = tempSudoku.array03[i];
            tempAboutSudoku = _SudokuInfor[(int)tempVec2.x, (int)tempVec2.y];

            if (tempAboutSudoku != tempSudoku &&
                tempAboutSudoku.Num == iNumber)
            {
                return(false);
            }
        }

        tempSudoku.Num = iNumber;

        return(true);
    }
Example #3
0
    void CreateArray()
    {
        arrayInfor = new SudokuInfor[ArrayCout, ArrayCout];
        for (int i = 0; i < ArrayCout; i++)
        {
            for (int j = 0; j < ArrayCout; j++)
            {
                arrayInfor[i, j] = new SudokuInfor();

                //创建二维数组
                arrayInfor[i, j].ID = new Vector2(i, j);

                arrayInfor[i, j].array01 = new Vector2[9];
                arrayInfor[i, j].array02 = new Vector2[9];
                arrayInfor[i, j].array03 = new Vector2[9];

                //添加每行每列的数组
                for (int m = 0; m < 9; m++)
                {
                    arrayInfor[i, j].array01[m] = new Vector2(i, m);
                    arrayInfor[i, j].array02[m] = new Vector2(m, j);
                }

                //添加每个宫格的数组
                int p = i - (i % 3);
                int q = j - (j % 3);
                int n = 0;
                for (int k = 0; k < 3; k++)
                {
                    for (int l = 0; l < 3; l++)
                    {
                        arrayInfor[i, j].array03[n] = new Vector2(p + k, q + l);
                        n++;
                    }
                }
            }
        }
    }
Example #4
0
    //void OnGUI()
    //{
    //    if (arrayInfor == null)
    //        return;
    //    if (listSudoku.Count <= 0)
    //        return;

    //    for (int i = 0; i < ArrayCout; i++)
    //    {
    //        for (int j = 0; j < ArrayCout; j++)
    //        {
    //            // GUI.Label(new Rect(260+i * 35, 100+j * 35, 30, 30), arrayInfor[i, j].Num.ToString());
    //            GUI.Label(new Rect(260 + i * 35, 100 + j * 35, 30, 30), listSudoku[0][i,j].Num.ToString());
    //        }
    //    }
    //}
    #endregion

    #region 最终检查数独是否正确
    bool CheckSudoku()
    {
        for (int i = 0; i < ArrayCout; i++)
        {
            for (int j = 0; j < ArrayCout; j++)
            {
                for (int m = 0; m < 9; m++)
                {
                    Vector2     tempVec2  = arrayInfor[i, j].array01[m];
                    SudokuInfor tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];
                    if (arrayInfor[i, j].Num == tempInfor.Num &&
                        arrayInfor[i, j] != tempInfor)
                    {
                        return(false);
                    }

                    tempVec2  = arrayInfor[i, j].array02[m];
                    tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];
                    if (arrayInfor[i, j].Num == tempInfor.Num &&
                        arrayInfor[i, j] != tempInfor)
                    {
                        return(false);
                    }

                    tempVec2  = arrayInfor[i, j].array03[m];
                    tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];
                    if (arrayInfor[i, j].Num == tempInfor.Num &&
                        arrayInfor[i, j] != tempInfor)
                    {
                        return(false);
                    }
                }
            }
        }

        return(true);
    }
Example #5
0
    void CalcArray()
    {
        CalcCout++;
        //限制回溯次数  解决堆栈溢出
        if (CalcCout >= 100)
        {
            Debug.Log("计算失败,回溯次数超过100次");
            return;
        }

        bool reStart = false;


        List <Vector2> ZeroIndex = new List <Vector2>();

        for (int i = 0; i < ArrayCout; i++)
        {
            for (int j = 0; j < ArrayCout; j++)
            {
                if (arrayInfor[i, j].Num == 0)
                {
                    ZeroIndex.Add(new Vector2(i, j));
                }
            }
        }


        for (int i = 0; i < ZeroIndex.Count; i++)
        {
            for (int m = 0; m < 9; m++)
            {
                Vector2     tempVec2  = arrayInfor[(int)ZeroIndex[i].x, (int)ZeroIndex[i].y].array01[m];
                SudokuInfor tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];

                tempInfor.Num     = 0;
                tempInfor.tempNum = new List <int>()
                {
                    1, 2, 3, 4, 5, 6, 7, 8, 9
                };

                tempVec2  = arrayInfor[(int)ZeroIndex[i].x, (int)ZeroIndex[i].y].array02[m];
                tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];

                tempInfor.Num     = 0;
                tempInfor.tempNum = new List <int>()
                {
                    1, 2, 3, 4, 5, 6, 7, 8, 9
                };

                tempVec2  = arrayInfor[(int)ZeroIndex[i].x, (int)ZeroIndex[i].y].array03[m];
                tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];

                tempInfor.Num     = 0;
                tempInfor.tempNum = new List <int>()
                {
                    1, 2, 3, 4, 5, 6, 7, 8, 9
                };
            }
        }

        for (int i = 0; i < ArrayCout; i++)
        {
            for (int j = 0; j < ArrayCout; j++)
            {
                int tempNum = arrayInfor[i, j].Num;
                if (arrayInfor[i, j].Num == 0)
                {
                    if (arrayInfor[i, j].tempNum.Count <= 0 || arrayInfor[i, j].tempNum == null)
                    {
                        reStart = true;
                        continue;
                    }
                    int index = Random.Range(0, arrayInfor[i, j].tempNum.Count);
                    tempNum = arrayInfor[i, j].tempNum[index];
                    arrayInfor[i, j].Num = tempNum;
                }

                for (int m = 0; m < 9; m++)
                {
                    Vector2     tempVec2  = arrayInfor[i, j].array01[m];
                    SudokuInfor tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];

                    for (int n = 0; n < tempInfor.tempNum.Count; n++)
                    {
                        if (tempInfor.tempNum[n] == tempNum)
                        {
                            tempInfor.tempNum.RemoveAt(n);
                        }
                    }

                    tempVec2  = arrayInfor[i, j].array02[m];
                    tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];

                    for (int n = 0; n < tempInfor.tempNum.Count; n++)
                    {
                        if (tempInfor.tempNum[n] == tempNum)
                        {
                            tempInfor.tempNum.RemoveAt(n);
                        }
                    }

                    tempVec2  = arrayInfor[i, j].array03[m];
                    tempInfor = arrayInfor[(int)tempVec2.x, (int)tempVec2.y];

                    for (int n = 0; n < tempInfor.tempNum.Count; n++)
                    {
                        if (tempInfor.tempNum[n] == tempNum)
                        {
                            tempInfor.tempNum.RemoveAt(n);
                        }
                    }
                }
            }
        }

        if (reStart)
        {
            CalcArray();
        }
        else
        {
            //添加数独到列表中
            if (CheckSudoku())
            {
                listSudoku.Add(arrayInfor);
                Debug.Log("计算成功,回溯次数:" + (CalcCout + 1));
            }
        }
    }