java - 战舰游戏中的网格阵列如何编号?

标签 java

您好,我正在尝试沿着我的战舰海洋的顶部和左侧实现 0-9 的数字。我希望它看起来像这样

  0  1  2  3  4  5  6  7  8  9 
 +--+--+--+--+--+--+--+--+--+--+
0|  |  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--+--+  
1|  |  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--+--+
2|
 (you get the idea!)
3
4
5
6
7
8
9

我尝试了许多 if 循环,但没有得到真正的结果。我可以显示数字,但不是我想要的方式。

public void printOcean(boolean showShips) {
    for (int row = 0; row < OCEAN_SIZE; row++) 
    {
        if(row == 0)
        {
            System.out.print(" ");
            for (int i = 0; i < OCEAN_SIZE; i++) 
            {
                System.out.print(String.format("%4s", i));
                System.out.println();
                {
                    System.out.print(" ");
                    System.out.print("+---");        
                }
                System.out.print("+\n");
                System.out.print(row +" ");


                for (int col = 0; col < OCEAN_SIZE; col++) 
                {
                    System.out.print("| " + ocean[row][col].printCoordinate(showShips) + " ");
                }
                System.out.print("|");
                System.out.println(); 
            }
            System.out.print(" ");
        }
        for (int i = 0; i < OCEAN_SIZE; i++) 
        {
            System.out.print("+---");           
        }
        System.out.print("+\n");
    }
}

我希望看到类似下面的内容

  0  1  2  3  4  5  6  7  8  9 
 +--+--+--+--+--+--+--+--+--+--+
0|  |  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--+--+  
1|  |  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--+--+
2| etc.

但是我得到的结果是

    0
 +---+
0 |
   1
 +---+
0 |
   2
 +---+
0 |
   3
 +---+
0 |
   4
 +---+
0 |
   5
 +---+
0 |
   6
 +---+
0 |
   7
 +---+
0 |
   8
 +---+
0 |
 +---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+

最佳答案

试试这个。但你应该在某个地方自定义它。

public class Main {
    static int OCEAN_SIZE = 10;

    public static void main(String[] args) {
        printOcean(false);
    }

    private static void printColumnIndexes() {
        System.out.print("  ");
        for (int i = 0; i < OCEAN_SIZE; i++) {
            System.out.print(" " + i + " ");
        }
        System.out.println(" ");
    }

    private static void printBorders() {
        System.out.print(" ");
        for (int j = 0; j < OCEAN_SIZE; j++) {
            System.out.print("+--");
        }
        System.out.println();
    }

    private static void printRows(boolean showShips) {
        for (int i = 0; i < OCEAN_SIZE; i++) {
            // printing row indexes
            System.out.print(i + "|");

            // printing main body
            for (int j = 0; j < OCEAN_SIZE; j++) {
                // Here you can put your ships
                if (showShips) {
                    // print your ship
                    // from your ship map[i][j].
                    // mention that cell size from your 
                    // sample is 2. So it should be something
                    // like this: "* " or " *" etc.
                    // if (map[i][j]) {
                    //     System.out.print("* |");
                    // } else {
                    //     System.out.print("  |");
                    // }
                } else {
                    System.out.print("  |");
                }

            }
            System.out.println();

            // printing extra borders after row filled
            printBorders();
        }
    }

    private static void printOcean(boolean showShips) {
        printColumnIndexes();
        printBorders();
        printRows(showShips);
    }
}

它给了我:

   0  1  2  3  4  5  6  7  8  
 +--+--+--+--+--+--+--+--+--
0|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
1|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
2|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
3|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
4|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
5|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
6|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
7|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
8|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--

因此,您可以自定义 printRows() 方法来填充您的船只。

关于java - 战舰游戏中的网格阵列如何编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57056839/

相关文章:

javascript - 有没有一种方法可以在Javascript中编写像toString这样的Java方法,如果在方法上调用该方法会打印该方法的源代码?

java - Hibernate Entity 到 Json 对象,使用 jackson 到 http post

java - 如何避免 FOR JAVA 的一轮

java - 使用Java快速计算字符串中单词出现次数的方法

java - 在 Java 中确定 'type'

java - 语法错误插入 while 表达式来完成 JAVA block 语句

java - 两种不同的布局(垂直和水平)?

java - 当一侧没有文本时,字符串分割不起作用

java - Kafka 消费者协调器连接问题,Kafka 0.11.0.3

java - 数据库-JTable交互