Results 1 to 4 of 4
  1. #1
    tylerkung is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Help with Battleship program! 2D arrays

    My assignment is to create a battleship board (6x6) and ask the user to guess a position and the output will display hit or miss. right now my ship is just 1x1 so far, but my teacher also wants a text based board to be displayed with occuring misses on them. I need help configuring the 2d array code. Here's what I have:

    PHP Code:
    import java.util.*;
    public class Battleship
    {
        public static void main(String [] args)
        {
            int ship = 1;
            Random generator = new Random();
            int xShip = generator.nextInt(6);
            int yShip = generator.nextInt(6);
            int sea[][] = new int[6][6]; //****Need help here <<<
            System.out.println("   0  1  2  3  4  5  ");
            System.out.println("  ___________________");
            System.out.println("0 |  |  |  |  |  |  |");
            System.out.println("  ___________________");
            System.out.println("1 |  |  |  |  |  |  |");
            System.out.println("  ___________________");
            System.out.println("2 |  |  |  |  |  |  |");
            System.out.println("  ___________________");
            System.out.println("3 |  |  |  |  |  |  |");
            System.out.println("  ___________________");
            System.out.println("4 |  |  |  |  |  |  |");
            System.out.println("  ___________________");
            System.out.println("5 |  |  |  |  |  |  |");
            System.out.println("  ___________________");
            
            sea[xShip][yShip] = ship; //need help here <<<
            while (ship == 1)
            {
                System.out.println("Guess X Value: ");
                Scanner guess = new Scanner(System.in);
                int xAttack = guess.nextInt();
                System.out.println("Guess Y Value: ");
                int yAttack = guess.nextInt();
                boolean xHit = (xShip == xAttack);
                boolean yHit = (yShip == yAttack);
                
            
                if (xHit)
                {
                    if (yHit)
                    {
                        System.out.println("Hit!");
                        System.out.println("You win!");
                        ship = ship - 1;
                    }
                
            
                else
                {
                     System.out.println("Miss!");
                }
                }
                else 
                {
                    System.out.println("Miss!");
                }
                if (xAttack == 69)
                {
                    System.out.println(xShip + ", " + yShip);
                }
            
            
            
            }
        }//end main method
    }//end class Battleship

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Help with Battleship program! 2D arrays

    Can you explain the problem you are having?
    If you get errors, please copy and paste the full text of the messages here.

  3. #3
    tylerkung is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: Help with Battleship program! 2D arrays

    I want to display a hit or miss on the "game board" everytime the user enters a guess, but I don't know how to do that.

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

    Default Re: Help with Battleship program! 2D arrays

    Where on the board do you want to display the "hit or miss"? Do you want to have a status area where a message is displayed? You could use a JLabel for that and set its text with the message.

    Whoops, I see you're not using GUI.
    Then you'd show the message with a println to the console.

Similar Threads

  1. need help creating a battleship like program
    By ss1 in forum New To Java
    Replies: 12
    Last Post: 08-19-2011, 12:00 AM
  2. Battleship 2D Array Program
    By AaronHopkins in forum New To Java
    Replies: 6
    Last Post: 04-05-2011, 04:38 AM
  3. Converting Program from ArrayLists to Arrays
    By Java-Guy in forum New To Java
    Replies: 3
    Last Post: 01-26-2011, 09:02 AM
  4. Replies: 1
    Last Post: 03-11-2010, 12:49 AM
  5. Need help with program: arrays
    By dangerzone9k in forum New To Java
    Replies: 2
    Last Post: 04-02-2009, 05:59 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
  •