Java中的Ludo游戏板

时间:2020-05-09 22:34:28

标签: java

enter image description here

如何在不使用数组的情况下向该板编写代码? 我尝试过,但是没有得到正确的结果。它应该与函数,循环和条件结构一起使用。行= 11,列= 11。

public static void board(int size) {
    for (int i=1; i<=size;i++){
        for (int j=1; j<=size; j++){
            if (i==6 && j==6){
                System.out.print(" ");
            }
            else if ( i==5 || i==7 || j==5 || j==7 || i==6 && j==1 || i==6 && j==11 ||
                    i==1 && j==6 || i==11 && j==6){
                if (i==6 && j==7 || i==5 && j==6 || i==6 && j==5 || i==7 && j==6 ){
                    break;
                }
                System.out.print("o");
            }
            else if (i==6 || j==6 ) {
                System.out.print(".");
            }
            else{
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    board(11);
}

2 个答案:

答案 0 :(得分:2)

您可以像这样打印它 1

static void printLudoBoard(int size) {
    if (size < 5 || size % 2 != 1)
        throw new IllegalArgumentException("Size must be odd and at least 5 (got " + size + ")");
    final String path = "o", goalPath = ".", goalCenter = " ";
    int armLength = (size - 3) / 2;
    for (int i = 0; i < armLength; i++)
        System.out.println(" ".repeat(armLength) + path + (i == 0 ? path : goalPath) + path);
    System.out.println(path.repeat(armLength + 1) + goalPath + path.repeat(armLength + 1));
    System.out.println(path + goalPath.repeat(armLength) + goalCenter + goalPath.repeat(armLength) + path);
    System.out.println(path.repeat(armLength + 1) + goalPath + path.repeat(armLength + 1));
    for (int i = armLength - 1; i >= 0; i--)
        System.out.println(" ".repeat(armLength) + path + (i == 0 ? path : goalPath) + path);
}

1)repeat()是在Java 11中添加的。如果他们在此之前教给您Java版本,请退还您的学费,并去一所不教书的学校古老的过时编码。或实现您自己的repeat()辅助方法。

printLudoBoard(11)

    ooo
    o.o
    o.o
    o.o
ooooo.ooooo
o.... ....o
ooooo.ooooo
    o.o
    o.o
    o.o
    ooo

printLudoBoard(15)

      ooo
      o.o
      o.o
      o.o
      o.o
      o.o
ooooooo.ooooooo
o...... ......o
ooooooo.ooooooo
      o.o
      o.o
      o.o
      o.o
      o.o
      ooo

答案 1 :(得分:0)

您正在使用两个单独的循环来完成一个循环的操作(并且还需要打印空格)。问题是您正在弄清楚字符应该去哪里,但没有在字符之间填充空格。

例如,假设您要在第1、3和8列上打印,因此您写道:

for (j = 1; j <= 11; j++) {
    if (j == 1 || j == 3 || j == 8) {
        System.out.println("o");
    }
}

因为之间没有空格,所以只打印ooo

所以,您可能想要类似的东西:

public static void main(String[] args ){
    for (int i=1; i<=11;i++) {
        for (int j = 1; j <= 11; j++) {
            if (i == 5 || i == 7 || j == 5 || j == 7 || i == 6 && j == 1 || i == 6 && j == 11 ||
                    i == 1 && j == 6 || i == 11 && j == 6) {
                System.out.print("o");
            } else if (i==6 || j==6) {
                System.out.print(".");
            } else {
                // Nothing should be here: print a space
                System.out.print(" ");
            }
        }
        System.out.println();
    }
    System.out.println();
}

我认为您需要对逻辑中的数字进行一些固定,因为这样会打印:

    ooo
    o.o
    o.o
    o.o
ooooooooooo
o...o.o...o
ooooooooooo
    o.o
    o.o
    o.o
    ooo

这不是您想要的输出,但这应该可以将您设置在正确的方向。

相关问题