Results 1 to 10 of 10
  1. #1
    makig is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default 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. #3
    makig is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Battleship game - arrays

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

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default 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. #5
    makig is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default 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. #7
    makig is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Battleship game - arrays

    I want to show last section or everything in one box.
    Battleship game - arrays-bez-nazwy-1.jpg

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default 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. #9
    makig is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default 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

Similar Threads

  1. Help with Battleship program! 2D arrays
    By tylerkung in forum New To Java
    Replies: 3
    Last Post: 09-23-2011, 01:45 PM
  2. Battleship Game - really need help
    By thrashsynergy in forum Java Applets
    Replies: 5
    Last Post: 04-25-2011, 07:21 PM
  3. Battleship game
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 09:42 PM
  4. Java Battleship Game Help PLEASE
    By mars_red in forum New To Java
    Replies: 0
    Last Post: 02-12-2008, 01:09 AM
  5. Java BattleShip game help
    By mars_red in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 12:58 AM

Posting Permissions

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