예제 #1
0
        //enum GridSolutionType
        //{
        //    None = 0,
        //    SingleRow = 1,
        //    SingleColumn = 2,
        //    SingleSquare = 3,
        //    CompleteRow = 4,
        //    CompleteColumn = 5,
        //    CompleteSquare = 6,
        //    OnlyAvailable = 7,
        //    NakedSingle = 8,
        //    SeveralMissingRow = 9,
        //    SeveralMissingColumn = 10,
        //    DoubleDeduction = 11
        //}

        #region - Public API
        public static GridSolutionType SolveGridAtIndex(GridSquareScript[] gridSquares, GridSquareScript selection)
        {
            if (SingleRuleCheck(selection, gridSquares))
            {
                return(GridSolutionType.None);
            }
            GridSolutionType check = SimpleCompletionCheck(selection.Index, gridSquares);

            if (check != GridSolutionType.None)
            {
                return(check);
            }
            check = OnlyAvailableNumberCheck(selection, gridSquares);
            if (check != GridSolutionType.None)
            {
                return(check);
            }
            check = OnlyAvailableSquareHashCheck(selection, gridSquares);
            if (check != GridSolutionType.None)
            {
                return(check);
            }
            check = CheckForNakedSingle(selection.Index, gridSquares);
            if (check != GridSolutionType.None)
            {
                return(check);
            }
            check = CheckSeveralMissing(selection, gridSquares);
            if (check != GridSolutionType.None)
            {
                return(check);
            }
            check = CheckDoubleDeduction(gridSquares, selection);
            if (check != GridSolutionType.None)
            {
                return(check);
            }
            return(GridSolutionType.None);
        }
예제 #2
0
        public bool IsRuleValid()
        {
            int errorCount = 0;

            int[] levelDataArray = levelData.Select(x => Int32.Parse(x.ToString())).ToArray();
            if (levelDataArray.Length != 81)
            {
                //Debug.LogError($"Rule id: {Id} length is invalid ({levelDataArray.Length})");
                Debug.LogError($"Rule id: {Id} length is invalid ({levelDataArray.Length})");
                errorCount += 1;
            }

            if (targetIndexes.Length != 3)
            {
                Debug.LogError($"Rule id: {Id} target length is invalid");
                errorCount += 1;
            }
            GameObject gameObject = new GameObject();

            for (int i = 0; i < levelDataArray.Length; i++)
            {
                gameObject.AddComponent <GridSquareScript>();
            }
            GridSquareScript[] gridSquares = gameObject.GetComponentsInChildren <GridSquareScript>();

            for (int i = 0; i < gridSquares.Length; i++)
            {
                bool             isTarget   = targetIndexes.Contains(i);
                GridSquareScript gridSquare = gridSquares[i];
                gridSquare.SetupTestGridSquare(i, levelDataArray[i], isTarget);
            }
            Debug.LogWarning($"************* RULE {Id} *************");
            targetSolutions  = new GridSolutionType[targetIndexes.Length];
            difficultyRating = 0;
            for (int i = 0; i < targetIndexes.Length; i++)
            {
                int targetCount         = 0;
                GridSquareScript target = gridSquares[targetIndexes[i]];
                Debug.LogWarning($"---------- TARGET {target.Index} ---------");
                for (int j = 1; j <= 9; j++)
                {
                    target.UpdateTestGridNumber(j);
                    Debug.LogWarning($"---------- Number {target.Number} ---------");
                    GridSolutionType solution = GridSolver.SolveGridAtIndex(gridSquares, target);
                    if (solution != GridSolutionType.None)
                    {
                        targetCount += 1;
                        Debug.LogWarning($"Rule id: {Id} target {targetIndexes[i]} has solution with {j} ({solution})");
                        targetSolutions[i] = solution;
                    }
                }

                //RESET TARGET AFTER TEST SO DOESN"T EFFECT NEXT TARGET!!!
                target.UpdateTestGridNumber(0);

                if (targetCount != 1)
                {
                    Debug.LogError($"Rule id: {Id} target {targetIndexes[i]} has {targetCount} solutions");
                    errorCount += 1;
                }
                else
                {
                    difficultyRating += (int)targetSolutions[i];
                }
            }
            DestroyImmediate(gameObject);
#if UNITY_EDITOR
            EditorUtility.SetDirty(this);
#endif
            return(errorCount == 0);
        }