Java中的Ludo游戏板 [英] Ludo game board in java

查看:51
本文介绍了Java中的Ludo游戏板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to I write code to this board without using an array? I tried, but I did not get the right result. It should be with functions, loops and conditional structures. Row=11, column=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);
}

推荐答案

您可以像这样打印它 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)Java 11中添加了 repeat().如果之前在教您Java版本,请退还学费,然后去上学不会讲古老的过时编码.或实现您自己的 repeat()辅助方法.

1) repeat() was added in java 11. If they are teaching you a Java version before that, go get your tuition money back, and go to a school that doesn't teach ancient out-dated coding. Or implement you own repeat() helper method.

printLudoBoard(11)

printLudoBoard(11)

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

printLudoBoard(15)

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

这篇关于Java中的Ludo游戏板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆