【问题标题】:Something like hanoi tower河内塔之类的
【发布时间】: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|

规则是:

  1. 任何符号都可以移动到相邻的空白处。
  2. 如果 O 旁边的位置为空白,则 X 可以跳过 O,反之亦然。例如:

    |X|X|X| |X|O|O|O|O| -> |X|X|X|O|X| |O|O|O|
    
  3. 没有其他方法可以移动它们。

我被第二条规则卡住了 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ü?

标签: java arrays methods


【解决方案1】:

类似的东西:

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
}

【讨论】:

  • 这并没有提供问题的答案。一旦你有足够的reputation,你就可以comment on any post;相反,provide answers that don't require clarification from the asker。 - From Review
  • @MartinBarker 为什么不呢?很简洁,是的,但与此同时,也没什么好说的。这怎么不是答案?
  • 因为您说的是“类似的东西”,所以答案应该针对问题量身定制并准确回答。除非有特定的理由不这样做,如果是这种情况,您应该解释为什么不这样做的原因。
猜你喜欢
  • 1970-01-01
  • 2014-02-18
  • 2012-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
相关资源
最近更新 更多