Results 1 to 7 of 7
  1. #1
    Matthieu105 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Unhappy Battleship field

    Hello everyone

    I have made a 2D array (board to place ships on) for my battleship game. As output i get this:

    0 x x x x x x x x x x
    1 x x x x x x x x x x
    2 x x x x x x x x x x
    3 x x x x x x x x x x
    4 x x x x x x x x x x
    5 x x x x x x x x x x
    6 x x x x x x x x x x
    7 x x x x x x x x x x
    8 x x x x x x x x x x
    9 x x x x x x x x x x

    Ofcourse for the players i need the first row to be filled with A,B,C,... But I don't know exactly how to do it. This is my code

    public class Field {
    private int row = 10;
    private int colomn = 10;

    private String field [][] = new String[row][colomn];

    public void makeField(){
    for(int i =0; i<row;i++){
    System.out.print(i);
    for(int j=0; j<colomn;j++){
    System.out.print(" x");
    }
    System.out.println("");
    }
    }
    }


    Thx in advance

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Battleship field

    First of all, when posting a code please use the [code][/code] tag.

    You question is how to print the letter A, B, C, etc? There are many ways to do it. You can define an array that contains these letters and print out using a for-loop by accessing the correct array index. Or you can convert the ASCII code of the letter and print it out as a character.

  3. #3
    Matthieu105 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default Re: Battleship field

    I don't know ho I can do that, thats the problem

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Battleship field

    Try creating code to do just that and nothing but that. Create a for loop that tries to print out the letters in a line, and let's work with that code attempt.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Battleship field

    Nah, if you know absolutely sure you have to print ten elements, why not just do this:

    Java Code:
    System.out.println("  A B C D E F G H I J");
    It's shorter than a silly loop ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Matthieu105 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default Re: Battleship field

    Because, i have to select for example 3A as a coordinate, and I don't know how to select a coordinate with a systm.out.println

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Battleship field

    Quote Originally Posted by Matthieu105 View Post
    Because, i have to select for example 3A as a coordinate, and I don't know how to select a coordinate with a systm.out.println
    You don't select a coordinate with an output statement; you have to read a String (typed by the user) and check a couple of things:

    1) does it contain two characters?
    2) is the first character a digit?
    2) is the second character in the range A ... J?

    If so, you have a valid coordinate in String form; you do have to transform it to its 'internal' form (x,y) though ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Battleship UML help
    By Lucid15 in forum New To Java
    Replies: 0
    Last Post: 03-12-2012, 04:15 AM
  2. Replies: 0
    Last Post: 07-22-2011, 12:47 PM
  3. front end display of field description when clicking the field name
    By neils in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-29-2010, 11:47 AM
  4. Replies: 1
    Last Post: 10-25-2010, 12:20 PM
  5. Replies: 4
    Last Post: 01-17-2010, 11:13 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
  •