【发布时间】:2019-03-02 16:05:24
【问题描述】:
我必须制作一个单人游戏。我们有 4-4 个玩家 X 和 O 以及一个空格(从 0 到 8)。它看起来像这样:
|X|X|X|X| |O|O|O|O|
我必须交换 X 和 O:
|O|O|O|O| |X|X|X|X|
规则是:
- 任何符号都可以移动到相邻的空白处。
-
如果 O 旁边的位置为空白,则 X 可以跳过 O,反之亦然。例如:
|X|X|X| |X|O|O|O|O| -> |X|X|X|O|X| |O|O|O| 没有其他方法可以移动它们。
我被第二条规则卡住了 2 天。
public static final int charX = 88;
public static final int charO = 79;
public static final int charSpace = 32;
//steps[0] = from;
//steps[1] = to
// X = 88 ; O = 79 ; Space = 32;
//char[] table = {'X', 'X', 'X', 'X', ' ', 'O', 'O', 'O', 'O'};
//int[] table = {88, 88, 88, 88, 32, 79, 79, 79, 79};
public static int[] ask() {
Scanner sc = new Scanner(System.in);
int from;
int to;
System.out.println("From where? (0-8)");
from = sc.nextInt();
System.out.println("To where? (0-8)");
to = sc.nextInt();
int[] step = {from, to};
return step;
}
public static boolean isValidMove(int[] steps, int[] table) {
int from = steps[0];
int to = steps[1];
boolean checker = (Math.abs(from - to) != 1 && table[to] != charSpace);
if (checker == true) {
return true;
} else
return false;
}
public static int[] validMove(Scanner sc, int to, int from, int[] table) {
int[] steps = ask();
from = steps[0];
to = steps[1];
while (isValidMove(steps, table)) {
System.out.println("Invalid move!");
steps = ask();
from = steps[0];
to = steps[1];
}
return steps;
}
【问题讨论】:
-
嘿,如果我不打扰你,你能帮我解决这个问题吗?或者你能告诉我怎么做吗?
-
嗨@JohnKugelman,我是Java 房间的房主之一。不幸的是,我们通常不提供辅导服务。也许你的学校有人可以帮助你,@Düsüngülü?