示例#1
0
    //Called by gridSquare whenever a number is changed
    public static void NumberChanged(GridSquare gs)
    {
        int index = all_squares_.IndexOf(gs);

        switch (PlayerPrefs.GetInt(pref_keys.error_highlighting_int.ToString()))
        {
        default:
        case 0:
            //No error highlighting
            break;

        case 1:
            if (gs.Number_ == 0)
            {
                gs.SetErrorColor(false);
            }
            //Highlight obvious logical errors
            //Data on the current GridSquare, to find the current row, column and square
            int row         = index / length;
            int col         = index % length;
            int squarestack = (index / root) % root;
            int squareband  = index / (length * root);

            //Values needed in the for loop

            List <GridSquare> same_row_      = new List <GridSquare>();
            List <GridSquare> same_col_      = new List <GridSquare>();
            List <GridSquare> same_square_   = new List <GridSquare>();
            List <GridSquare> error_squares_ = new List <GridSquare>();

            //HashSets to check for duplicates in the loop
            var row_keys    = new HashSet <int>();
            var col_keys    = new HashSet <int>();
            var square_keys = new HashSet <int>();

            for (int i = 0; i < length; i++)
            {
                ////Current squares
                GridSquare row_square    = all_squares_[i + row * length];
                GridSquare col_square    = all_squares_[i * length + col];
                GridSquare square_square = all_squares_[(squarestack * root + squareband * root * length) + (i % root) + (length * (i / root))];

                same_row_.Add(row_square);
                same_col_.Add(col_square);
                same_square_.Add(square_square);
                //Checking for duplicates in row, column and square
                //Row check
                if (!row_keys.Add(row_square.Number_))
                {
                    if (row_square.Number_ > 0)
                    {
                        foreach (GridSquare gridSquare in same_row_.Where(x => x.Number_ == row_square.Number_))
                        {
                            if (!error_squares_.Contains(gridSquare))
                            {
                                error_squares_.Add(gridSquare);
                            }
                        }
                    }
                }
                //Column check
                if (!col_keys.Add(col_square.Number_))
                {
                    if (col_square.Number_ > 0)
                    {
                        foreach (GridSquare gridSquare in same_col_.Where(x => x.Number_ == col_square.Number_))
                        {
                            if (!error_squares_.Contains(gridSquare))
                            {
                                error_squares_.Add(gridSquare);
                            }
                        }
                    }
                }
                //Square Check
                if (!square_keys.Add(square_square.Number_))
                {
                    if (square_square.Number_ > 0)
                    {
                        foreach (GridSquare gridSquare in same_square_.Where(x => x.Number_ == square_square.Number_))
                        {
                            if (!error_squares_.Contains(gridSquare))
                            {
                                error_squares_.Add(gridSquare);
                            }
                        }
                    }
                }
            }
            //Error highlighting
            if (error_squares_.Count > 0)
            {
                foreach (GridSquare gridSquare in error_squares_)
                {
                    if (!all_errors_.Contains(gridSquare))
                    {
                        all_errors_.Add(gridSquare);
                    }
                    gridSquare.SetErrorColor(true);
                }
            }
            else
            {
                foreach (GridSquare gridSquare in all_errors_)
                {
                    gridSquare.SetErrorColor(false);
                }
                all_errors_.Clear();
            }
            break;

        case 2:
            //Highlight ALL error (will need solved sudoku for this)
            break;
        }
    }