1. Member
Join Date
Nov 2012
Posts
11
Rep Power
0

## Battleship field

Hello everyone

I have made a 2D array (board to place ships on) for my battleship game. As output i get this:

0 x x x x x x x x x x
1 x x x x x x x x x x
2 x x x x x x x x x x
3 x x x x x x x x x x
4 x x x x x x x x x x
5 x x x x x x x x x x
6 x x x x x x x x x x
7 x x x x x x x x x x
8 x x x x x x x x x x
9 x x x x x x x x x x

Ofcourse for the players i need the first row to be filled with A,B,C,... But I don't know exactly how to do it. This is my code

public class Field {
private int row = 10;
private int colomn = 10;

private String field [][] = new String[row][colomn];

public void makeField(){
for(int i =0; i<row;i++){
System.out.print(i);
for(int j=0; j<colomn;j++){
System.out.print(" x");
}
System.out.println("");
}
}
}

2. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
14

## Re: Battleship field

First of all, when posting a code please use the [code][/code] tag.

You question is how to print the letter A, B, C, etc? There are many ways to do it. You can define an array that contains these letters and print out using a for-loop by accessing the correct array index. Or you can convert the ASCII code of the letter and print it out as a character.

3. Member
Join Date
Nov 2012
Posts
11
Rep Power
0

## Re: Battleship field

I don't know ho I can do that, thats the problem

4. ## Re: Battleship field

Try creating code to do just that and nothing but that. Create a for loop that tries to print out the letters in a line, and let's work with that code attempt.

5. ## Re: Battleship field

Nah, if you know absolutely sure you have to print ten elements, why not just do this:

Java Code:
System.out.println("  A B C D E F G H I J");
It's shorter than a silly loop ...

kind regards,

Jos

6. Member
Join Date
Nov 2012
Posts
11
Rep Power
0

## Re: Battleship field

Because, i have to select for example 3A as a coordinate, and I don't know how to select a coordinate with a systm.out.println

7. ## Re: Battleship field

Originally Posted by Matthieu105
Because, i have to select for example 3A as a coordinate, and I don't know how to select a coordinate with a systm.out.println
You don't select a coordinate with an output statement; you have to read a String (typed by the user) and check a couple of things:

1) does it contain two characters?
2) is the first character a digit?
2) is the second character in the range A ... J?

If so, you have a valid coordinate in String form; you do have to transform it to its 'internal' form (x,y) though ...

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•