Как искать и менять номера в массиве?
Я сейчас создаю 24 головоломки. Если я введу "move", а затем "12", то индекс массива, соответствующий номеру, будет искать, если ноль соседствует с самим собой. Если найден 0, он поменяется местами, в противном случае будет сказано "неправильное число"
Это то, что я имею до сих пор, но цифры не сдвинутся с места.
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.util.Arrays;
public class Board
{
File file = new File("input.txt");
File out = new File("output.txt");
int[][] the_frame = new int[5][5];
Scanner command = new Scanner(System.in);
public void readInitialBoard() throws Exception
{
PrintWriter output = new PrintWriter(out);
Scanner input = new Scanner(file); //Reads the input from file "input.txt"
for ( int row = 0; row < the_frame.length; row++) //5 rows in total
{
for (int column = 0; column < the_frame[row].length; column++) //5 columns read from each row
{
the_frame[row][column] = input.nextInt(); //each values are put into array
}
}
}
public int makeMove(String move, int num) throws ArrayIndexOutOfBoundsException
{
Scanner command = new Scanner(System.in);
move = command.nextLine();
if (move.equals("move")) //"move" is inputted follow by number. If number not next to 0, then it will be considered an incorrect move.
{
num = command.nextInt(); //input number
for ( int row = 0; row < the_frame.length; row++ )
{
for (int column = 0; column < the_frame[row].length; column++)
{
if (num == the_frame[row][column]) //"searches" the number within the array. If found it goes to next step
{
if (num >= 100 * (the_frame[row+1][column]))
{
the_frame[row+1][column] = num;
the_frame[row][column] = 0;
System.out.println("Success");
}
else if (num >= 100 * (the_frame[row][column+1]))
{
the_frame[row][column+1] = num;
the_frame[row][column] = 0;
System.out.println("Success");
}
else if (num >= 100 * (the_frame[row-1][column]))
{
the_frame[row-1][column] = num;
the_frame[row][column] = 0;
System.out.println("Success");
}
else if (num >= 100 * (the_frame[row][column-1]))
{
the_frame[row][column-1] = num;
the_frame[row][column] = 0;
System.out.println("Success");
}
else
{
System.out.println("Wrong move");
}
System.out.println("please continue");
}
}
}
}
else if (move.equals("help"))
{
System.out.println("help");
}
else
{
System.out.println("Invalid number found");
}
return num;
}
public void showBoard()
{
String leftAlignFormat = "| %-2s |";
System.out.println("Current board");
for (int row = 0; row < the_frame.length; row++)
{
System.out.println("------------------------------");
for (int column = 0; column < the_frame[row].length; column++)
{
System.out.format(leftAlignFormat, the_frame[row][column]);
}
System.out.println();
}
}
public boolean isCorrect()
{
return false;
}
}
Это результат попытки "переместить" число 12
----jGRASP exec: java Driver
Current board
------------------------------
| 1 || 2 || 3 || 4 || 5 |
------------------------------
| 6 || 7 || 8 || 9 || 10 |
------------------------------
| 11 || 12 || 0 || 13 || 14 |
------------------------------
| 15 || 16 || 17 || 18 || 19 |
------------------------------
| 20 || 21 || 22 || 23 || 24 |
move
12
Success
please continue
Wrong move
please continue
Current board
------------------------------
| 1 || 2 || 3 || 4 || 5 |
------------------------------
| 6 || 7 || 8 || 9 || 10 |
------------------------------
| 11 || 0 || 12 || 13 || 14 |
------------------------------
| 15 || 16 || 17 || 18 || 19 |
------------------------------
| 20 || 21 || 22 || 23 || 24 |
----jGRASP exec: java Driver
Current board
------------------------------
| 1 || 2 || 3 || 4 || 5 |
------------------------------
| 6 || 7 || 8 || 9 || 10 |
------------------------------
| 11 || 12 || 0 || 13 || 14 |
------------------------------
| 15 || 16 || 17 || 18 || 19 |
------------------------------
| 20 || 21 || 22 || 23 || 24 |
move
17
Success
please continue
Current board
------------------------------
| 1 || 2 || 3 || 4 || 5 |
------------------------------
| 6 || 7 || 8 || 9 || 10 |
------------------------------
| 11 || 12 || 17 || 13 || 14 |
------------------------------
| 15 || 16 || 0 || 18 || 19 |
------------------------------
| 20 || 21 || 22 || 23 || 24 |
move
18
Success
please continue
Current board
------------------------------
| 1 || 2 || 3 || 4 || 5 |
------------------------------
| 6 || 7 || 8 || 9 || 10 |
------------------------------
| 11 || 12 || 17 || 13 || 14 |
------------------------------
| 15 || 16 || 18 || 0 || 19 |
------------------------------
| 20 || 21 || 22 || 23 || 24 |
move
13
Success
please continue
Success
please continue
Current board
------------------------------
| 1 || 2 || 3 || 4 || 5 |
------------------------------
| 6 || 7 || 8 || 9 || 10 |
------------------------------
| 11 || 12 || 17 || 13 || 14 |
------------------------------
| 15 || 16 || 18 || 0 || 19 |
------------------------------
| 20 || 21 || 22 || 23 || 24 |
move
23
Current board
------------------------------
| 1 || 2 || 3 || 4 || 5 |
------------------------------
| 6 || 7 || 8 || 9 || 10 |
------------------------------
| 11 || 12 || 17 || 13 || 14 |
------------------------------
| 15 || 16 || 18 || 0 || 19 |
------------------------------
| 20 || 21 || 22 || 23 || 24 |
move
19
Current board
------------------------------
| 1 || 2 || 3 || 4 || 5 |
------------------------------
| 6 || 7 || 8 || 9 || 10 |
------------------------------
| 11 || 12 || 17 || 13 || 14 |
------------------------------
| 15 || 16 || 18 || 0 || 19 |
------------------------------
| 20 || 21 || 22 || 23 || 24 |
move
18
Success
please continue
Success
please continue
Current board
------------------------------
| 1 || 2 || 3 || 4 || 5 |
------------------------------
| 6 || 7 || 8 || 9 || 10 |
------------------------------
| 11 || 12 || 17 || 13 || 14 |
------------------------------
| 15 || 16 || 18 || 0 || 19 |
------------------------------
| 20 || 21 || 22 || 23 || 24 |
1 ответ
Я думаю, что проблема в том, что ваш makeMove
Метод продолжает сканирование даже после того, как находит номер. Таким образом, он находит 12, перемещает его вправо, а затем немедленно находит его снова и пытается переместить его снова.
добавлять return num;
сразу после System.out.println("please continue");
и посмотрим, решит ли это вашу проблему.
Редактировать:
У вас также есть проблема с не проверкой границ. Попробуй это:
if (row < 4 && num >= 100 * (the_frame[row + 1][column])) {
...
} else if (column < 4 && num >= 100 * (the_frame[row][column + 1])) {
...
} else if (row > 0 && num >= 100 * (the_frame[row - 1][column])) {
...
} else if (column > 0 && num >= 100 * (the_frame[row][column - 1])) {
...