我正在创造一个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 |
答案 0 :(得分:0)
我认为问题是您的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])) {
...