Results 1 to 13 of 13
Like Tree3Likes
  • 1 Post By Junky
  • 1 Post By Junky
  • 1 Post By Junky

Thread: need help creating a battleship like program

  1. #1
    ss1
    ss1 is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default need help creating a battleship like program

    so basically the game starts with a blank board, which looks like this:

    A B C D E
    1 - - - - -
    2 - - - - -
    3 - - - - -
    4 - - - - -
    5 - - - - -

    Then the user is supposed to fire a shot by telling the program which column (A-E) and row (1-5) values. If it shot hits the ship we're supposed to mark it with a '*' and if it isn't it will be an 'x'. We're supposed to pick 4 spots that the hit will be in, like, 1 hit per row. I know that it's supposed to have an array, but I have no idea how to input the '*' and 'x'. We're not supposed to display it on the board yet.

    It's like the user will pick random spots until they get the right one. How do I put the '*''s and the 'x' on the program without the board appearing so many times? It says that we can create a separate array to hold the location of the '*'s.

    It's supposed to be a 2-D array that lets the user input coordinates into it.

    So far my progam looks like this:

    Java Code:
    import java.util.Scanner;
    public class project {
    
    
    
    	public static void main(String[] args) {
    		Scanner in = new Scanner (System.in);
    		System.out.println("Menu");
    		System.out.println("Press 1 to Fire Shot");
    		System.out.println("Press 2 to Give up");
    		System.out.println("Press 3 to Quit ");
    		int num = in.nextInt();
    		while (num!=3) {				
    			switch (num) {
    			case 1: 
    				final int ROWS = 5;
    				final int COLS = 5;
    				int [] [] values = new int [ROWS] [COLS];
    				for (int i = 0; i<ROWS; i++) { 
    					for (int j = 0; j<COLS; j++) {
    						values [1][4] = 5;
    					}
    				}
    				System.out.println("  A  B  C  D  E");
    				System.out.println("1 -  -  -  -  -");
    				System.out.println("2 -  -  -  -  -");
    				System.out.println("3 -  -  -  -  -");
    				System.out.println("4 -  -  -  -  -");
    				System.out.println("5 -  -  -  -  -");	
    				System.out.println("You chose choice 1, fire shot");
    				System.out.println("Enter a letter A, B, C, D, E");
    				
    			}
    		}
    		
    			}
    
    	{
    	}
    
    
    }
    Last edited by ss1; 08-18-2011 at 02:59 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    When posting code use code tags. Place [ code ] before and [ \code ] (without the spaces) after your code. Or use the # button.

    Are you getting errors? Then copy and paste the full and exact error message and indicate on which line it occurs.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,443
    Rep Power
    25

    Default

    How do I put the '*''s and the 'x' on the program without the board appearing so many times?
    Won't you have to show the board after every shot so the use will know if he has hit anything?
    You could use a 2 dim array, with one dim corresponding to A-E and the other dim corresponding to 1-5.
    A quick technique for mapping characters to int values is to subtract the first letter from the given letter.
    If you are given 'B' (upper case it) and subtract 'A' you get a 1 (Remember arrays are 0 based).
    If you read in a String the String class has methods for getting its contents as char values.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java is an Object Oriented language. Exploit it. Use classes and methods. Don't cram all your code into the main method. For starters you can have a method to display the board which has nested loops to iterate over your 2D array to display the values. Yes you should use a 2D array. You can also have another method to update the board each time the user takes a shot. This means changing the '-' at the correct co-ordinates to a '*' or 'X' depending upon if the user makes a hit or not.
    Fubarable likes this.

  5. #5
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    I see that it compiles and runs. Do you like what it does? If not, can you describe exactly what it does, and how this differs from what you want?

    I see that you have a "5x5 array of integers." There are several states a single cell could be in, such as there being a ship in that cell, or not. And the user having shot into that cell or not. I can think of several ways to put all of this data in this one place. Or you can have a separate array for some of it, if you'd like. It's up to you.

    [P.S.]
    Oh; and I guess that after entering "1" to fire, they'll have to enter something like "C3" to fire into that cell.

    There; that should help you make progress.
    Last edited by JeffGrigg; 08-18-2011 at 04:06 AM.

  6. #6
    ss1
    ss1 is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    it's supposed to let the user input coordinates into a 5x5 2D array to locate and destroy a hidden enemy ship. I've modified the code, but i still don't like what it does. here's what i have so far:

    Java Code:
    import java.util.Scanner;
    public class project {
    
    
    
    	public static void main(String[] args) {
    		Scanner in = new Scanner (System.in);
    		System.out.println("Menu");
    		System.out.println("Press 1 to Fire Shot");
    		System.out.println("Press 2 to Give up");
    		System.out.println("Press 3 to Quit ");
    		int num = in.nextInt();
    		while (num!=3) {				
    			switch (num) {
    			case 1: 
    				final int ROW1=1;
    				final int ROW2=1;
    				final int ROW3=1;
    				final int ROW4=1;
    				final int ROW5=1;
    				final int COLS = 5;
    				int  [][] board = new int [5][5];
    				board [1][1] = ROW1;
    				System.out.println("");
    				System.out.println("  A  B  C  D  E");
    				System.out.println("1 -  -  -  -  *");
    				System.out.println("2 -  -  -  * -" );
    				System.out.println("3 -  -  * -  -" );
    				System.out.println("4 -  *  -  -  -");
    				System.out.println("5 *  -  -  -  -");	
    				{break;
    
    				}
    			}
    			
    				switch (num) {
    				case 2: 
    					System.out.println("You want to give up?");
    					System.out.println("Press 5 to reset the game");
    					int num1 = in.nextInt();
    					{break;
    					}
    				}
    				
    					switch (num) {
    					case 3:
    						System.out.println("You are now quitting the game");
    						{break;
    						}
    					}
    				}
    			}
    	{
    	}

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    It's nice to see you heeded the advice given so far.
    </sarcasm>

    Why should I waste my time trying to help if you don't listen?
    JeffGrigg likes this.

  8. #8
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    (It's like watching a slow train wreak; I just can't look away. But I'll catch some anyways. ;-)

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    switch (num) {
    case 1:
    }
    switch (num) {
    case 2:
    }
    switch (num) {
    case 3:
    ARRRRrrrrggh!

    Make the bad code go away.

  10. #10
    ss1
    ss1 is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    public class project {
    
    
    
    	public static void main(String[] args) {
    		Scanner in = new Scanner (System.in);
    		System.out.println("Menu");
    		System.out.println("Press 1 to Fire Shot");
    		System.out.println("Press 2 to Give up");
    		System.out.println("Press 3 to Quit ");
    		int num = in.nextInt();
    		while (num!=3) {				
    			
    				final int ROW1=1;
    				final int ROW2=1;
    				final int ROW3=1;
    				final int ROW4=1;
    				final int ROW5=1;
    				final int COLS = 5;
    				int  [][] board = new int [5][5];
    				board [1][1] = ROW1;
    				System.out.println("  A  B  C  D  E");
    				System.out.println("1 x  x  x  x  *");
    				System.out.println("2 x  x  x  *  x");
    				System.out.println("3 x  x  *  x  x" );
    				System.out.println("4 x  *  x  x  x");
    				System.out.println("5 *  x  x  x  x");	
    				{break;
    
    				}
    		}
    	}
    }
    cases removed.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    :headdesk:
    JeffGrigg likes this.

  12. #12
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    @ss1: It's not that using switch-case was wrong for the problem. It's more that your use of switch-case was sloppy and confused. And your indention was misleading.

    Well the good newses are...
    1. It no longer displays the board an infinate number of times.
    2. The columns line up again, in the output.


    So what does it do or fail to do that you wish to change?

    Maybe some thinking about how the thing should work, overall, would help. Like...
    • We're going to hide four ships in the grid.
    • And then we're going to ask the user where to shoot, until they find all four, or until they give up.
    • After each shot, we'll show them <something> so that they know if they've hit or missed
    • etc...

  13. #13
    ss1
    ss1 is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    mmkay, got it to work after thinking through it. Sorry for the all trouble.

Similar Threads

  1. Battleship Game - really need help
    By thrashsynergy in forum Java Applets
    Replies: 5
    Last Post: 04-25-2011, 07:21 PM
  2. Java Battleship
    By lolerskates2695 in forum New To Java
    Replies: 3
    Last Post: 04-23-2011, 12:03 AM
  3. Battleship 2D Array Program
    By AaronHopkins in forum New To Java
    Replies: 6
    Last Post: 04-05-2011, 04:38 AM
  4. I need help creating this program
    By LostinJavaLand in forum New To Java
    Replies: 14
    Last Post: 04-29-2010, 10:41 AM
  5. Battleship game
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 09:42 PM

Posting Permissions

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