Niek Beijloos
Niek Beijloos

Reputation: 83

Multi-dimensional Array easy way to set elements

I want to create the game Battleship in Java, but I don't know how to make an array like this:

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

I want it to be easy to set elements of the field. So I can manipulate the field easy by user input.

For example:

I want D6 to be a *

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

Upvotes: 0

Views: 174

Answers (2)

tomosius
tomosius

Reputation: 1429

Are you looking for something along these lines?

API:

Classes:

  • Battleground
  • Position
  • Tile (enum)

Interactions:

  • new board: new Battleground() b
  • print board: System.out.print(b)
  • set tile: b.set(Position.get(10, 'J'), Tile.IntactShip)

Code:

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;
    }
}

Upvotes: 0

xcesco
xcesco

Reputation: 4838

I propose to you an implementation. It's a Grid class, that allows to you to define a grid 10x10 of String. As you can see, you can read and write elements using rows between 1-10 and columns A-J. The main method, show just an example of usage:

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();
    }
}

The result of Main's execution is:

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

Upvotes: 1

Related Questions