Results 1 to 3 of 3

Thread: Battleship game

  1. #1
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default Battleship game

    hi im trying to create part of the battleship game using java. My teacher created the class GameBoard and told us the methods included in it which are:
    public char getPosition(char row, int column): This method will return the char value currently stored within the denoted position of the game board.
    public boolean guessPosition(char row, int column): This method will update the game board by recorded the denoted position as a guess from the user. It will return true if the guess is a hit and false if the guess is a miss.

    I created the board but i dont how to use the methods and how to display the user guess to the board.

    here is what i have so far


    import java.util.Scanner;

    class P3 {
    public static void main(String args[]) {
    GameBoard gameBoard= new GameBoard();

    System.out.println("Katherine Moreno");
    System.out.print("Section: 6766");

    Scanner scanner = new Scanner(System.in);
    int column, i;
    char row;
    String data;

    System.out.println();

    i=0;
    while (i< 17){


    // Printing a column heading above the table
    System.out.print(" ");

    for (column=0; column <10; column++) {
    System.out.print(column+ " ");
    }

    System.out.println();
    // Printing the border of the table
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.print("+");

    System.out.println();
    for (row=0; row<1; row++) {
    System.out.print("A");
    }

    for (column=0; column<11; column++) {
    System.out.print(" | ");
    }

    //first row
    System.out.println();
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.print("+");

    System.out.println();
    for (row=0; row<1; row++) {
    System.out.print("B");
    }

    for (column=0; column<11; column++) {
    System.out.print(" | ");
    }


    //second row
    System.out.println();
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.print("+");

    System.out.println();
    for (row=0; row<1; row++) {
    System.out.print("C");
    }

    for (column=0; column<11; column++) {
    System.out.print(" | ");
    }

    //third row
    System.out.println();
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.print("+");

    System.out.println();
    for (row=0; row<1; row++) {
    System.out.print("D");
    }

    for (column=0; column<11; column++) {
    System.out.print(" | ");
    }

    //fourth row
    System.out.println();
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.print("+");

    System.out.println();
    for (row=0; row<1; row++) {
    System.out.print("E");
    }

    for (column=0; column<11; column++) {
    System.out.print(" | ");
    }

    //fifth row
    System.out.println();
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.print("+");

    System.out.println();
    for (row=0; row<1; row++) {
    System.out.print("F");
    }

    for (column=0; column<11; column++) {
    System.out.print(" | ");
    }

    //sixth row
    System.out.println();
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.print("+");

    System.out.println();
    for (row=0; row<1; row++) {
    System.out.print("G");
    }

    for (column=0; column<11; column++) {
    System.out.print(" | ");
    }

    //seventh row
    System.out.println();
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.print("+");

    System.out.println();
    for (row=0; row<1; row++) {
    System.out.print("H");
    }

    for (column=0; column<11; column++) {
    System.out.print(" | ");
    }

    //eight row
    System.out.println();
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.print("+");

    System.out.println();
    for (row=0; row<1; row++) {
    System.out.print("I");
    }

    for (column=0; column<11; column++) {
    System.out.print(" | ");
    }

    //ninth row
    System.out.println();
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.print("+");

    System.out.println();
    for (row=0; row<1; row++) {
    System.out.print("J");
    }

    for (column=0; column<11; column++) {
    System.out.print(" | ");
    }


    //last row
    System.out.println();
    System.out.print(" ");
    for (row=0; row<10; row++) {
    System.out.print("+");
    for (column=0; column<3; column++) {
    System.out.print("-");
    }
    }
    System.out.println("+");

    System.out.print("Enter the row letter of your guess: ");
    data= scanner.next();
    row= data.charAt(0);
    System.out.print("Enter the column number of your guess: ");
    column= scanner.nextInt();


    i++;
    }


    }
    }
    Last edited by kathyla18; 02-26-2009 at 07:04 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, what you have tried so far? Can you specifically ask your question. I don't think anyone wants to do your homework.

    By the way, if you are still don't know about methods in Java, better to look at it first. Did you learn those things in your class?

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

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM
  3. Battleship help..im confused
    By stepjerd1 in forum Java 2D
    Replies: 4
    Last Post: 01-23-2009, 02:35 AM
  4. Java Battleship Game Help PLEASE
    By mars_red in forum New To Java
    Replies: 0
    Last Post: 02-12-2008, 02:09 AM
  5. Java BattleShip game help
    By mars_red in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 01: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
  •