Results 1 to 5 of 5
  1. #1
    dumbo is offline Member
    Join Date
    Jan 2015
    Posts
    1
    Rep Power
    0

    Default battleship game help in java?!

    Okay so I'm taking a course called computer studies and I suck at it. For our final project we're making a game. I'm "trying" to do battleship in 2D, but its not going well. at all. I need help. I've started it but i'm having trouble on how to get the arrays which are my rows in the game to printout, and i need to put them into a seperate method and all them. please help me, I'm like desperate but not that desperate that i would copy someone elses code from the internet to turn in as my own.
    this is my code, help me internet people!

    Scanner input = new Scanner(System.in);

    String userN;
    String B1;
    String H1;
    H1 = "onethree";

    System.out.println(" Welcome to BattleShip! ");
    System.out.println ("");
    System.out.println("Please enter your name:");
    userN = input.nextLine ();
    System.out.println("Welcome " + userN);
    System.out.println ("");

    // rules of the game that the player will follow.
    System.out.println("Rules of the game:");
    System.out.println("There are 5 hiddenships in the water made of stars.");
    System.out.println("Your mission as captain is to find all 5 hidden enemy ships, in other words HIT them with your cannon ball!");
    System.out.println("You will be given 6 cannon balls to find the hidden ships. Inorder to choose a place to throw ur ball, type in the row number and the column");
    System.out.println("Example: onethree, the one is for row 1, and the three is for the column.");
    System.out.println ("");
    System.out.println("May the odds be in your favour!");
    System.out.println ("");

    // row 1 of the game
    String [] row1 = {"|" , "*" , "*" , "*" , "*" , "*" , "|"};
    String [] row12 = {"|" , "/" , "/" , "H" , "/" , "/" , "|" };


    System.out.println("Please choose a place to fire your first cannon:");
    B1 = input.nextLine();

    if (B1 == H1 ) {
    row1 [3] = "H";
    System.out.println("You have hit a ship!");

    }


    // row 2 of the game
    String [] row2 = {"|" , "*" , "*" , "*" , "*" , "*" , "|"};
    String [] row22 = {"|" , "/" , "H" , "/" , "/" , "H" , "|"};

    //row 3 of the game
    String [] row3 = {"|" , "*" , "*" , "*" , "*" , "*" , "|"};
    String [] row32 = {"|" , "H" , "/" , "/" , "H" , "/" , "|"};

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: battleship game help in java?!

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    50
    Rep Power
    0

    Default Re: battleship game help in java?!

    Just a quick tip.
    It may be useful to hold two separate 2D arrays. One the player sees, and the other with the ships in it. perhaps something like:

    Java Code:
    playerGrid[][] = {{ "*","*","*","*","*","*"},
                           { "*","*","*","*","*","*"},
                           { "*","*","*","*","*","*"},
                           { "*","*","*","*","*","*"},
                           { "*","*","*","*","*","*"}};
    
    shipGrid[][] = {{ " ","S"," "," "," "," "},
                        { " "," "," "," "," "," "},
                        { " "," "," "," ","S"," "},
                        { " "," "," "," "," "," "},
                        { " "," "," "," S," "," "}};
    S = a ship location.
    both grids need the same dimensions as they represent differing views of the same data.

    As a player guesses, you modify the playergrid with whatever character to represent a miss or a hit,
    and in the background check if its actually something other than a 'space' character to see if it hit.

    See if this steers you in the right direction

  4. #4
    AAChaoshand's Avatar
    AAChaoshand is offline Member
    Join Date
    Jan 2015
    Location
    Texas
    Posts
    22
    Rep Power
    0

    Default Re: battleship game help in java?!

    I was going to take a poke at this since I am learning this stuff at my own pace but multidimensional arrays are not my strong point. I started working on your project just to see how far I could get and I ended up just building out two classes with the appropriate grid arrays as sibernewf had shown above and then used some for loops and got lost from there. Don't take what I did as the correct way, you kinda got me interested in doing this so I can learn 2d arrays better.

    Also, as already posted please use the # CODE tool in advanced mode, it makes your coding easier to read.

    EDIT:

    Scratch the whole class idea, it seems easier to throw it all in the main method. Still learning this stuff to.
    Last edited by AAChaoshand; 01-20-2015 at 05:41 AM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: battleship game help in java?!

    Quote Originally Posted by AAChaoshand View Post
    Scratch the whole class idea, it seems easier to throw it all in the main method. Still learning this stuff to.
    If one is testing a feature of Java like arrays or using some of the Double or Integer methods, then stick them in main. But I often see folks using static methods and main and all they are doing is functional programming.

    If someone is at the point where they can program a Battleship game then they should also be using OO concepts. At a minimum, they could create a class (call it Ship) to hold the hidden value, the unhidden value and any other values as necessary. Then create your 2D arrays of type Ship. But then, I don't know where the students are in the course so this all may be inappropriate. The instructor may have other ideas.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. End battleship game
    By redblack in forum New To Java
    Replies: 2
    Last Post: 07-18-2013, 04:32 AM
  2. Help with battleship game, please!
    By goodvibes in forum New To Java
    Replies: 5
    Last Post: 07-25-2012, 11:04 AM
  3. Help with Battleship Game
    By Lucid15 in forum New To Java
    Replies: 59
    Last Post: 04-11-2012, 07:37 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

Tags for this Thread

Posting Permissions

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