我必须制作一个单人游戏。我们有4-4个玩家X和O,还有一个空格(从0到8)。看起来像这样:
|X|X|X|X| |O|O|O|O|
我必须交换X和O:
|O|O|O|O| |X|X|X|X|
规则是:
X旁边的空白为空白,则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;
}
答案 0 :(得分:0)
类似的东西:
for (int i=Math.min(steps[0],steps[1]); i<Math.max(steps[0],steps[1])-1; i++){
// if table[i] is opposite of table[step[0]]
// move not valid
}