Answer the question
In order to leave comments, you need to log in
How to pass multiple arrays from method to method?
Hello. I have a choiceLevel method that returns a table of the requested difficulty level, and a sortBoardsByLevels method that generates tables and sorts them into different difficulty levels until tables of all three difficulty levels are generated. How can I return the table from the second method (and this is already 3 two-dimensional arrays)? Of course, you can combine the methods into one, but it turns out cumbersome and ugly
public int[][] choiceLevel(int size, int level) {
sortBoardsByLevels(size);
int[][] gameReadyBoard = new int[size][size];
switch (level) {
case (1):
gameReadyBoard = board1; // вот здесь и ниже хочу получить нужные таблицы
break;
case (2):
gameReadyBoard = board2;
break;
case (3):
gameReadyBoard = board3;
break;
}
printTheBoard(gameReadyBoard);
return gameReadyBoard;
}
private void sortBoardsByLevels(int size) {
int[][] board1 = new int[size][size];
int[][] board2 = new int[size][size];
int[][] board3 = new int[size][size];
BoardFactory boardFactory = new BoardFactory();
int[][] board = boardFactory.generateBoard(size); // генерируется предварительная таблица, не готовая для игры
boolean a = false;
while (!a) {
int[][] unsorted = selectCell(board); // получаем таблицу с рандомно скрытыми клетками, таблица готова к игре, но не отсортирована по уровням
int count = countAmountOfHiddenCells(unsorted); // узнаем количество скрытых клеток
if (count >= 28 && count < 35) board1 = unsorted;
else if (count >= 35 && count < 42) board2 = unsorted;
else if (count >= 42) board3 = unsorted;
board = boardFactory.generateBoard(size);
if (cheсkTheBoardIsFull(board1) && cheсkTheBoardIsFull(board2) && cheсkTheBoardIsFull(board3)) a = true;
} // удостоверяемся, что получили таблицы всех уровней
}
Answer the question
In order to leave comments, you need to log in
Alas, it is already cumbersome and not beautiful.
Outputs:
1) Make a class with the necessary information and return one object, in which there will be all the necessary arrays and any additional. infa.
About Classes and Objects
2) Leave it as it is (extremely ugly) - and make for example an ArrayList of the type of two-dimensional arrays, where it will contain board1 at index0, board2 at index 1 and board3 at index2
Example
3) Make it even worse: - 3 arrays go to the static (global state), which can be accessed from any corner of the instance.
You just need to stop writing Java like C and start using OOP.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question