-
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++;
}
}
}
-
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?
-
Cross-posted on the forums.sun.com