0

我想用Java创建游戏战舰,但我不知道如何制作这样的数组:

10 - - - - - - - - - - 
9 - - - - - - - - - - 
8 - - - - - - - - - - 
7 - - - - - - - - - - 
6 - - - - - - - - - - 
5 - - - - - - - - - - 
4 - - - - - - - - - - 
3 - - - - - - - - - - 
2 - - - - - - - - - - 
1 - - - - - - - - - - 
  A B C D E F G H I J 

我希望轻松设置字段的元素。所以我可以通过用户输入轻松操作该字段。

例如:

我希望 D6 成为 *

10 - - - - - - - - - - 
9 - - - - - - - - - - 
8 - - - - - - - - - - 
7 - - - - - - - - - - 
6 - - - * - - - - - - 
5 - - - - - - - - - - 
4 - - - - - - - - - - 
3 - - - - - - - - - - 
2 - - - - - - - - - - 
1 - - - - - - - - - - 
  A B C D E F G H I J 
4

2 回答 2

1

我向你提出一个实施方案。它是一个 Grid 类,允许您定义一个 10x10 的字符串网格。如您所见,您可以使用 rows between1-10和 columns来读取和写入元素A-J。该main方法仅显示使用示例:

public class Grid {

    public Grid() {
        for (int i = 0; i < elements.length; i++) {
            elements[i] = "-";
        }
    }

    private String elements[] = new String[100];

    public void print() {

        for (int i = 0; i < elements.length; i++) {
            if (i % 10 == 0) {
                System.out.println();
                System.out.print(String.format("%2d",10 - i / 10));
            }
            System.out.print(" " + elements[i]);
        }

        System.out.print("\n  ");

        for (int i = 0; i < 10; i++) {
            System.out.print(" " + (char)(i + 'A'));
        }
    }

    public String get(int row, char col) {
        return elements[(10-row)*10-('A'-col)];
    }

    public void set(int row, char col, String value) {
        if (row>=1 && row<=10 && col>='A' && col<='J') {
            elements[(10-row)*10-('A'-col)]=value;
        }
    }


    public static void main(String[] args) {
        Grid grid = new Grid();

        grid.set(1, 'A', "x");
        grid.set(1, 'B', "t");
        grid.set(10, 'A', "y");
        grid.set(10, 'J', "r");
        grid.set(5, 'J', "r");
        grid.set(1, 'J', "x");
        grid.set(6, 'D', "*");

        grid.print();
    }
}

的执行结果Main是:

10 y - - - - - - - - r
 9 - - - - - - - - - -
 8 - - - - - - - - - -
 7 - - - - - - - - - -
 6 - - - * - - - - - -
 5 - - - - - - - - - r
 4 - - - - - - - - - -
 3 - - - - - - - - - -
 2 - - - - - - - - - -
 1 x t - - - - - - - x
   A B C D E F G H I J
于 2018-07-10T20:34:56.840 回答
0

您是否正在寻找类似的东西?

接口:

课程:

  • 战场
  • 位置
  • 平铺(枚举)

互动:

  • 新板:new Battleground() b
  • 印制板:System.out.print(b)
  • 设置瓷砖:b.set(Position.get(10, 'J'), Tile.IntactShip)

代码:

public class Game
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Game game = new Game();
        game.demo();
    }

    public void demo()
    {
        Battleground b = new Battleground();
        b.set(Position.get(7, 'C'), Tile.IntactShip);
        b.set(Position.get(7, 'D'), Tile.IntactShip);
        b.set(Position.get(7, 'E'), Tile.IntactShip);
        b.set(Position.get(7, 'F'), Tile.IntactShip);
        b.set(Position.get(5, 'E'), Tile.IntactShip);
        b.set(Position.get(4, 'E'), Tile.IntactShip);
        System.out.println(b);
    }
}

class Battleground
{
    private Tile[][] tiles = new Tile[10][10];

    public Battleground()
    {
        fill(Tile.Unknown);
    }

    public Tile get(Position pos)
    {
        return tiles[pos.row-1][pos.column-'A'];
    }

    public void set(Position pos, Tile tile)
    {
        tiles[pos.row-1][pos.column-'A'] = tile;
    }

    public void fill(Tile tile)
    {
        for (Position p = Position.FIRST; p != null; p = p.next())
            set(p, tile);
    }

    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        for (int row = 10; row >= 1; row--)
        {
            sb.append((row > 9 ? "" : " ") + row);
            for (char col = 'A'; col <= 'J'; col++)
                sb.append(" " + tiles[row-1][col-'A']);
            sb.append("\n");
        }
        sb.append("  ");
        for (char col = 'A'; col <= 'J'; col++)
            sb.append(" " + col);
        sb.append("\n");
        return sb.toString();
    }
}

class Position
{
    public static final Position FIRST = get(10, 'A');

    public final int row;
    public final char column;

    private Position(int row, char col)
    {
        this.row = row;
        this.column = col;
    }

    public static Position get(int row, char col) throws java.lang.IllegalArgumentException
    {
        if (row < 1 || row > 10 || col < 'A' || col > 'J')
            throw new IllegalArgumentException();
        return new Position(row, col);
    }

    public Position next()
    {
        if (row == 1 && column == 'J')
            return null;
        if (column == 'J')
            return new Position(row-1, 'A');
        return new Position(row, (char)(column+1));
    }
}

enum Tile
{
    Unknown('.'),
    Empty('*'),
    IntactShip('O'),
    DestroyedShip('X');

    private char symbol;

    Tile(char symbol) {
        this.symbol = symbol;
    }

    public String toString() {
        return "" + symbol;
    }
}
于 2018-07-10T21:24:15.410 回答