0

Есть переменная Difficult с начальным значением "Easy". (Она обозначает уровень сложности моей игры). Дальше я хочу сделать проверку условия. Если уровень такой-то такой-то, то переменная Diff принимает определённое значение. Но вот в чём проблема. При проверке условия на переменную Difficult появляется ошибка: Unknown class: "Difficult"введите сюда описание изображения

Не могли бы вы помочь мне. Думаю что решение проблемы не такое сложное как мне кажется, но я пока не понимаю, т.к не спец. в Android Studio.

package com.example.sudoku408;

import android.content.Context;
import android.os.Build;
import android.widget.ArrayAdapter;

import androidx.annotation.RequiresApi;

import java.util.Random;
import java.util.stream.IntStream;

public class Game extends ArrayAdapter<String>  {

    private static final String[] mContacts = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    private final int SUBSECTION__SIZE = 3;
    private final int NO__VALUE = 0;
    private final int MIN__VALUE = 1;
    private final int MAX__VALUE = 9;
    private final int BOARD__SIZE = 9;
    private final int BOARD__START__INDEX = 0;
    private int N = 9;


    Context mContext;
    Random random = new Random();

    private int[][] Sudoku = {{1, 2, 3, 4, 5, 6, 7, 8, 9}, {4, 5, 6, 7, 8, 9, 1, 2, 3}, {7, 8, 9, 1, 2, 3, 4, 5, 6}, {2, 3, 4, 5, 6, 7, 8, 9, 1}, {5, 6, 7, 8, 9, 1, 2, 3, 4}, {8, 9, 1, 2, 3, 4, 5, 6, 7}, {3, 4, 5, 6, 7, 8, 9, 1, 2}, {6, 7, 8, 9, 1, 2, 3, 4, 5}, {9, 1, 2, 3, 4, 5, 6, 7, 8}};
    private int[][] Sudoku_temp;


    int Diff = 0; String Difficult = "Easy";
    if (Difficult == "Easy") {
        Diff = (random.nextInt(35 - 30) + 1) + 30;
    }
    else if (Difficult == "Normal") {
        Diff = (random.nextInt(30 - 25) + 1) + 25;
    }
    else if (Difficult == "Hard") {
        Diff = (random.nextInt(25 - 20) + 1) + 20;
    }

    Transparent(Sudoku, N);
        for (int i = 0; i < 15;i++)
    SwapStr(Sudoku, N);
        for (int i = 0; i < 15; i++)
    SwapCol(Sudoku, N);
    Sudoku_temp = Sudoku;
    Remove_cells(Sudoku, Diff);

    //Конструктор
    public Game(Context context, int textViewResourceId) {
        super(context, textViewResourceId, mContacts);
        // TODO Auto-generated constructor stub
        this.mContext = context;

    }

    public String getItem(int position) {
        return mContacts[position];
    }

    public void GetSudoku(int[][] Sudoku, int N) {
        for (int str = 0; str < N; str++) {
            for (int col = 0; col < N; col++) {
                System.out.print(Sudoku[str][col] + " ");
            }
            System.out.print("\n");
        }
    }
    public void SwapStr(int[][] Sudoku, int N) {
        Random random = new Random();
        int Z = random.nextInt(N+1);
        for (int str = 0; str < N; str++) {
            if (str == Z && str >= 0 && str <= 2) {
                int V = (random.nextInt(2 - 0) + 1) + 0;
                while (V == Z) {
                    V = (random.nextInt(2 - 0) + 1) + 0;
                }
                for (int j = 0; j < N; j++) {
                    int X;
                    X = Sudoku[str][j];
                    Sudoku[str][j] = Sudoku[V][j];
                    Sudoku[V][j] = X;
                }
                break;
            }
            else if (str == Z && str >= 3 && str <= 5) {
                int V = (random.nextInt(5 - 3) + 1) + 3;
                while (V == Z) {
                    V = (random.nextInt(5 - 3) + 1) + 3;
                }
                for (int j = 0; j < N; j++) {
                    int X;
                    X = Sudoku[str][j];
                    Sudoku[str][j] = Sudoku[V][j];
                    Sudoku[V][j] = X;
                }
                break;
            }
            else if (str == Z && str >= 6 && str <= 8) {
                int V = (random.nextInt(8 - 6) + 1) + 6;
                while (V == Z) {
                    V = (random.nextInt(8 - 6) + 1) + 6;
                }
                for (int j = 0; j < N; j++) {
                    int X;
                    X = Sudoku[str][j];
                    Sudoku[str][j] = Sudoku[V][j];
                    Sudoku[V][j] = X;
                }
                break;
            }
        }
    }
    public void SwapCol (int[][] Sudoku, int N) {
        Random random = new Random();
        int Z = random.nextInt(N+1);
        for (int col = 0; col < N; col++) {
            if (col == Z && col >= 0 && col <= 2) {
                int V = (random.nextInt(2 - 0) + 1) + 0;
                while (V == Z) {
                    V = (random.nextInt(2 - 0) + 1) + 0;
                }
                for (int j = 0; j < N; j++) {
                    int X;
                    X = Sudoku[j][col];
                    Sudoku[j][col] = Sudoku[j][V];
                    Sudoku[j][V] = X;
                }
                break;
            }
            else if (col == Z && col >= 3 && col <= 5) {
                int V = (random.nextInt(5 - 3) + 1) + 3;
                while (V == Z) {
                    V = (random.nextInt(5 - 3) + 1) + 3;
                }
                for (int j = 0; j < N; j++) {
                    int X;
                    X = Sudoku[j][col];
                    Sudoku[j][col] = Sudoku[j][V];
                    Sudoku[j][V] = X;
                }
                break;
            }
            else if (col == Z && col >= 6 && col <= 8) {
                int V = (random.nextInt(8 - 6) + 1) + 6;
                while (V == Z) {
                    V = (random.nextInt(8 - 6) + 1) + 6;
                }
                for (int j = 0; j < N; j++) {
                    int X;
                    X = Sudoku[j][col];
                    Sudoku[j][col] = Sudoku[j][V];
                    Sudoku[j][V] = X;
                }
                break;
            }
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    private boolean solve(int[][]board) {
        for (int row = BOARD__START__INDEX; row < BOARD__SIZE; row++) {
            for (int column = BOARD__START__INDEX; column < BOARD__SIZE; column++) {
                if (board[row][column]== NO__VALUE) {
                    for (int k = MIN__VALUE; k <= MAX__VALUE; k++) {
                        board[row][column]= k;
                        if (isValid(board, row, column) && solve(board)) {
                            return true;
                        }
                        board[row][column]= NO__VALUE;
                    }
                    return false;
                }
            }
        }
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private boolean isValid(int[][]board, int row, int column) {
        return (rowConstraint(board, row)
                && columnConstraint(board, column)
                && subsectionConstraint(board, row, column));
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private boolean rowConstraint(int[][]board, int row) {
        boolean[]constraint = new boolean[BOARD__SIZE];
        return IntStream.range(BOARD__START__INDEX, BOARD__SIZE)
                .allMatch(column -> checkConstraint(board, row, constraint, column));
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private boolean columnConstraint(int[][]board, int column) {
        boolean[]constraint = new boolean[BOARD__SIZE];
        return IntStream.range(BOARD__START__INDEX, BOARD__SIZE)
                .allMatch(row -> checkConstraint(board, row, constraint, column));
    }

    private boolean subsectionConstraint(int[][]board, int row, int column) {
        boolean[]constraint = new boolean[BOARD__SIZE];
        int subsectionRowStart = (row/SUBSECTION__SIZE) * SUBSECTION__SIZE;
        int subsectionRowEnd = subsectionRowStart + SUBSECTION__SIZE;

        int subsectionColumnStart = (column/SUBSECTION__SIZE) * SUBSECTION__SIZE;
        int subsectionColumnEnd = subsectionColumnStart + SUBSECTION__SIZE;

        for (int r = subsectionRowStart; r < subsectionRowEnd; r++) {
            for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) {
                if (!checkConstraint(board, r, constraint, c)) return false;
            }
        }
        return true;
    }

    boolean checkConstraint(
            int[][]board,
            int row,
            boolean[]constraint,
            int column) {
        if (board[row][column]!= NO__VALUE) {
            if (!constraint[board[row][column]- 1]) {
                constraint[board[row][column]- 1]= true;
            } else {
                return false;
            }
        }
        return true;
    }
    public int[][] Remove_cells(int[][] Sudoku, int Diff) {
        int a = 0;
        int b = 9;
        int random_number_1 = 0;
        int random_number_2 = 0;
        int Z = 0;

        for(int i = Diff; i < 81; i++){
            random_number_1 = a + (int) (Math.random() * b);
            random_number_2 = a + (int) (Math.random() * b);

            if (Sudoku[random_number_1][random_number_2] != 0) {
                Sudoku[random_number_1][random_number_2] = 0;
                Z++;
            }
            else
                i--;
        }
        return Sudoku;
    }
}
2
  • попробуйте перезапустить андроид студио)
    – denisnumb
    30 мая 2021 в 8:43
  • 1. Переменные называются с маленькой буквы. Если что-то с большой - IDE будет думать, что вы имя класса пишете, а не к переменной обращаетесь. Оно, конечно, должно уметь такое обрабатывать, но лучше не искушать судьбу и следовать правилам именования в Яве. 2. Сравнивать строки надо через equals(). В вашем случае оно может сработать, но потом может не сработать и вы будете долго искать причину проблемы. Возможно, эти 2 исправления всё исправят.
    – ЮрийСПб
    30 мая 2021 в 13:44

1 ответ 1

1

1) Переменная пишется с маленькой буквой

    public int[][] remove_cells(int[][] sudoku, int diff) {
    int a = 0;
    int b = 9;
    int random_number_1 = 0;
    int random_number_2 = 0;
    int z = 0;

    for(int i = diff; i < 81; i++){
        random_number_1 = a + (int) (Math.random() * b);
        random_number_2 = a + (int) (Math.random() * b);

        if (sudoku[random_number_1][random_number_2] != 0) {
            sudoku[random_number_1][random_number_2] = 0;
            Z++;
        }
        else
            i--;
    }
    return sudoku;
}

int diff = 0; String difficult = "Easy";
if (difficult.equals("Easy")) {
    diff = (random.nextInt(35 - 30) + 1) + 30;
}
else if (difficult == "Normal") {
    diff = (random.nextInt(30 - 25) + 1) + 25;
}
else if (difficult == "Hard") {
    diff = (random.nextInt(25 - 20) + 1) + 20;
}

2) Сравнивать String надо через equals() а не ==

не правильно

 if (difficult == "Easy") {
        ....
    }

правилно

if (difficult.equals("Easy")) {
    ....
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.