/ / Java - Wybierz liczbę z każdego kwadratu w siatce 9 x 9 - java, układ siatki, sudoku

Java - Wybierz liczbę z każdego kwadratu w siatce 9x9 - java, grid-layout, sudoku

Mam siatkę sudoku 9x9 i muszę uzyskać losową liczbę z każdego kwadratu 3x3 w siatce.

Najbardziej okropny kod wyglądałby tak:

    if(square == 0) {
row = random.nextInt(3);
col = random.nextInt(3);
}

if(square == 1) {
row = random.nextInt(3);
col = random.nextInt(3) + 3;
}
if(square == 2) {
row = random.nextInt(3);
col = random.nextInt(3) + 6;
}
if(square == 3) {
row = random.nextInt(3) + 3;
col = random.nextInt(3);
}
if(square == 4) {
row = random.nextInt(3) + 3;
col = random.nextInt(3) + 3;
}
if(square == 5) {
row = random.nextInt(3) + 3;
col = random.nextInt(3) + 6;
}
if(square == 6) {
row = random.nextInt(3) + 6;
col = random.nextInt(3);
}
if(square == 7) {
row = random.nextInt(3) + 6;
col = random.nextInt(3) + 3;
}
if(square == 8) {
row = random.nextInt(3) + 6;
col = random.nextInt(3) + 6;
}

gdzie square jest indeksem kwadratu w siatce (square = 0,1, ..., 8)

Nie mogę wymyślić, jak napisać to lepiej.

Jakieś pomysły? Dzięki

Odpowiedzi:

2 dla odpowiedzi № 1

Powinno to działać dla dowolnego rozmiaru kwadratu. W twoim przypadku jest to 3x3, więc rozmiar to 3.

int size = 3;
row = random.nextInt(size) + (square / size) * size;
col = random.nextInt(size) + (square % size) * size;

1 dla odpowiedzi nr 2

Coś takiego

int[] rowAdd = new int[] { 0, 0, 0, 3, 3, 3, 6, 6, 6 };
int[] colAdd = new int[] { 0, 3, 6, 0, 3, 6, 0, 3, 6 };
row = random.nextInt(3) + rowAdd[square];
col = random.nextInt(3) + colAdd[square];

Wstaw jedną wartość tablicy, którą należy dodać do zmiennej row, Nazwij to rowAdd. W drugiej tablicy colAdd wstaw zmienną, do której należy dodać col.
Wreszcie, użyj square jak indeks, aby pobrać poprawną wartość do dodania.

Oczywiście tablice rowAdd i colAdd powinien być częścią metody. (Jest mnóstwo czasu i pamięci, aby tworzyć nowe tablice za każdym razem, gdy wywołujesz metodę). Te tablice powinny być powiązane z klasami, więc powinny być static.


0 dla odpowiedzi № 3

To jest kod wszystkich rzeczy naraz

public class Test {
public static void main(String[] args) {
//initializing arrays
int[][] grid = new int[9][9];
int[] numbers = new int[9];

//populating grid
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
grid[i][j] = (int)(Math.random()*10);
}
}

//printing grid
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
System.out.print(grid[i][j]);
}
System.out.println();
}

System.out.println();

int counter = 0;
//first and second loops counts for 0,3,6
for(int i = 0; i < 9; i += 3) {
for(int j = 0; j < 9; j += 3) {
//after taking i or j values(which must be either 0,3 or 6) goes for 3x3 parts and saving those numbers in number array
for(int t = i; t < i+3; t++) {
for(int k = j; k < j+3; k++) {
numbers[counter] = grid[t][k];
counter++;
}
}

//you can pick randomly from numbers array here

//showing which numbers picked
for(int t = 0; t < 9; t++) {
System.out.print(numbers[t]);
}
System.out.println();
System.out.println();
counter = 0;
}
}
}
}