# Thread: Battleship game - arrays

1. Member
Join Date
Nov 2011
Posts
5
Rep Power
0

## Battleship game - arrays

Hi!

I've got some of my code, but no idea how to add two things.
1. modifying size of array, values ​​placed in cells, number of dimensions
2. I need to use:
Java Code:
```String data = javax.swing.JOptionPane.showInputDialog("Enter data");
int value = Integer.parseInt(data);```
Battleship
Java Code:
```class Battleship  {

public static void main(String[] args) {
int max = 30;
int w = 10;
int k = 10;
int[][] tab = new int[w][k];

int i = 0, j = 0;
while(i < w){
j = 0;
while(j < k){
tab[i][j] = 0;
j = j + 1;
}
i = i + 1;
}

int n = 0;
while(n < max){
int ws = (int)(w*Math.random());
int ks = (int)(k*Math.random());
System.out.println("" + ws + "x" + ks);
if (tab[ws][ks] == 0){
tab[ws][ks] = 1;
n++;
}
}

i = 0;
while(i < w){
j = 0;
System.out.print(i + "|");
while(j < k){
System.out.print(tab[i][j] == 1 ? "x" : " ");
j = j + 1;
}
System.out.println();
i = i + 1;
}
System.out.println(" -----------");
System.out.println("  0123456789");
}
}```

2. ## Re: Battleship game - arrays

The size of an array can not be modified. Its size is set when you create it.
You can create a new array with the new size and copy elements from the old array to the new array.

Why do you need to modify the size of an array?
Which array do you want to modify?

3. Member
Join Date
Nov 2011
Posts
5
Rep Power
0

## Re: Battleship game - arrays

Changing size of playing area like: 5x5 or 20x20. This is first point of my specification.

4. ## Re: Battleship game - arrays

If you are going to change the size of an array from one size to another, you need to create a new array with the new size, copy the contents of the old array to the new array and then set the old array's name to refer to the new array.
Are you sure that is what you need to do?

5. Member
Join Date
Nov 2011
Posts
5
Rep Power
0

## Re: Battleship game - arrays

I ve got some problem with display array in the window like:
Java Code:
`javax.swing.JOptionPane.showMessageDialog`
What should I add or change ? I don t know how to show array which is made in 'while'. Every single try of doing that was fail (only single symbols).
Last edited by makig; 11-17-2011 at 06:22 PM.

6. ## Re: Battleship game - arrays

I ve got some problem
how to show array
How do you want to show the array?
As a row of Strings, in a list?

For debugging, the java.util.Arrays toString() method is easy.

7. Member
Join Date
Nov 2011
Posts
5
Rep Power
0

## Re: Battleship game - arrays

I want to show last section or everything in one box.

8. ## Re: Battleship game - arrays

Are you asking how to change what is shown in the righthand view to be shown in the lefthand view?
Can you explain what each of those views are?
Is the lefthand view a JOPtionPane?
What is that shown on the right?

Have you tried putting the output text in a text area and including that text area in the JOptionPane?

9. Member
Join Date
Nov 2011
Posts
5
Rep Power
0

## Re: Battleship game - arrays

Right - this console in Eclipse. Left is "String datab = javax.swing.JOptionPane.showInputDialog" use that to input variable to define position of "ship". I want to add this "area of battle on the left box.

I didn t try, text area.
Maybe small hint how to use text area.
Last edited by makig; 11-17-2011 at 07:15 PM.

10. ## Re: Battleship game - arrays

Read the API doc for JOptionPane to see if you can add a component to it. If so, build a panel with a scroll pane and your text area and add the panel to the JOptionPane.

If you don't find a method of JOptionPane that does what you want, you may have to use the JDialog class to build a custom dialog. See the Java Tutorial.
Last edited by Norm; 11-17-2011 at 07:32 PM. Reason: added ref to JDialog

#### Posting Permissions

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