Results 1 to 3 of 3
Thread: Battleship game
- 02-26-2009, 05:56 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 16
- Rep Power
- 0
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 06:04 AM.
- 02-26-2009, 08:53 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
-
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM -
Battleship help..im confused
By stepjerd1 in forum Java 2DReplies: 4Last Post: 01-23-2009, 01:35 AM -
Java Battleship Game Help PLEASE
By mars_red in forum New To JavaReplies: 0Last Post: 02-12-2008, 01:09 AM -
Java BattleShip game help
By mars_red in forum Advanced JavaReplies: 0Last Post: 02-12-2008, 12:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks