如何为战舰游戏编号网格阵列?

时间:2019-07-16 12:03:29

标签: 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 |
 +---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+

2 个答案:

答案 0 :(得分:2)

尝试这个。但是您应该自定义它。

remote stacktrace: 
Stacktrace:
    at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0 (NativeConstructorAccessorImpl.java:-2)
    at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstanceWithCaller (Constructor.java:500)
    at java.lang.reflect.Constructor.newInstance (Constructor.java:481)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$errorHandler$0 (W3CHandshakeResponse.java:62)
    at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0 (HandshakeResponse.java:30)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0 (ProtocolHandshake.java:126)
    at java.util.stream.ReferencePipeline$3$1.accept (ReferencePipeline.java:195)
at java.util.Spliterators$ArraySpliterator.tryAdvance (Spliterators.java:958)
    at java.util.stream.ReferencePipeline.forEachWithCancel (ReferencePipeline.java:127)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel (AbstractPipeline.java:502)
    at java.util.stream.AbstractPipeline.copyInto (AbstractPipeline.java:488)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto (AbstractPipeline.java:474)
    at java.util.stream.FindOps$FindOp.evaluateSequential (FindOps.java:150)
    at java.util.stream.AbstractPipeline.evaluate (AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.findFirst (ReferencePipeline.java:543)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession (ProtocolHandshake.java:128)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession (ProtocolHandshake.java:74)
    at org.openqa.selenium.grid.session.remote.RemoteSession$Factory.performHandshake (RemoteSession.java:147)
    at org.openqa.selenium.grid.session.remote.ServicedSession$Factory.apply (ServicedSession.java:161)
    at org.openqa.selenium.remote.server.ActiveSessionFactory.lambda$apply$12 (ActiveSessionFactory.java:180)
    at java.util.stream.ReferencePipeline$3$1.accept (ReferencePipeline.java:195)
    at java.util.stream.ReferencePipeline$11$1.accept (ReferencePipeline.java:442)
    at java.util.stream.ReferencePipeline$2$1.accept (ReferencePipeline.java:177)
    at java.util.Spliterators$ArraySpliterator.tryAdvance (Spliterators.java:958)

它给了我

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| | | | | | | | | | +--+--+--+--+--+--+--+--+-- 方法来填充船只。

答案 1 :(得分:-4)

尝试使用Int Array,以便每艘战舰都有自己的唯一ID(整数)

因此,如果您的船从位置1,1到位置1,4的长度为4,而id是2,那么

   0   1   2    3    4    5
------------------------------
0 | 0   0   0    0    0    0
1 | 0   2   2    2    2    0

这样,您可以存储哪个战舰正在被攻击或未被攻击。