I made a battleship game and its gone well except for checking if the ship is sunk... Any help? Here's what I have so far, thanks in advance.
public class BattleShip {
private static final int dimensions = 10;
private String[][] board = new String[dimensions][dimensions];
private String[][] piecesBoard = new String[dimensions][dimensions];
public BattleShip()
{
createBoard();
initialize();
directions();
showBoard();
play();
}
public void play() {
Scanner in;
in = new Scanner(System.in);
int row;
int column;
int guess = 0;
int hit = 0;
do {
System.out.println("Please input row");
row = Integer.parseInt(in.nextLine());
System.out.println("Please input column");
column = Integer.parseInt(in.nextLine());
if(((row >= 0 && row < dimensions) && (column >= 0 && column < dimensions)) && (board[row][column].equals("#"))) {
if(piecesBoard[row][column].equals("O")){
System.out.println("HIT!");
board[row][column] = "%";
hit++;
showBoard();
}
else if(piecesBoard[row][column].equals("#")) {
System.out.println("MISS!");
board[row][column] = "X";
showBoard();
}
guess++;
}
else {
if(row == 50 && column == 50) {
displayPiecesBoard();
}
else {
System.out.println("Invalid Input");
}
}
}while(hit != 18);
System.out.println("Thanks for playing!");
System.out.println("Number of guess: " + guess);
}
public void directions() {
System.out.println("The name of the game is Battleship. The goal is to");
System.out.println("Guess and find ships to sink them.");
System.out.println("You will be asked to give a column and row combination to guess.");
System.out.println("The first number will be the column");
System.out.println("and the second number will be the row.");
System.out.println("If you guess a hit you will see this message: HIT!");
System.out.println("If you guess a miss you will see this message: MISS!");
System.out.println("If you guess a sink you will see this message: HIT AND SUNK!");
System.out.println("To see the answer board, type in 50, 50 as your two numbers");
System.out.println("# = Not guess, O = hit, X = miss");
}
public void createBoard(){
for (int column = 0; column < dimensions; column++) {
for (int row = 0; row < dimensions; row++) {
board[row][column] = "#";
piecesBoard[row][column] = "#";
}
}
}
public void showBoard() {
for (int column = 0; column < dimensions; column++) {
System.out.print(column);
for (int row = 0; row < dimensions; row++) {
System.out.print(board[row][column]);
System.out.print(" ");
}
System.out.println();
}
}
public void displayPiecesBoard() {
for (int column=0; column<dimensions; column++) {
for (int row = 0; row < dimensions; row++) {
System.out.print(piecesBoard[row][column]);
System.out.print(" ");
}
System.out.println();
}
}
/**This class randomizes the ships and sets them on the board according
* to direction and whether or not there is room for that ship to set there.
* Based on the size of the ship you'll know which ship you're going
* to be adding coordinates too. For direction of ship, (North/South,
* East/West): 0 = up, 1 = right, 2 = down, 3 = left.
*/
public void initialize(){
Random r = new Random();
Random d = new Random();
for(int places = 5; places > 0; places--)
{
int ships = places;
if (places == 2 || places == 1)
{
ships = places++;
}
boolean done = false;
do{
int column = r.nextInt(dimensions);
int row = r.nextInt(dimensions);
int temporaryRow = row;
int temporaryColumn = column;
int direction = d.nextInt(4);
int checkFree = 0;
if(direction == 0)
{
if(row - ships >= 0)
{
for(int checking = ships; checking>0; checking--)
{
if(piecesBoard[temporaryRow][column].equals("#"))
{
checkFree++;
temporaryRow--;
}
}
if(checkFree == ships)
{
temporaryRow = row;
for (int place = ships; place >0; place--)
{
piecesBoard[temporaryRow][column] = "O";
temporaryRow--;
}
done = true;
}
}
}
if(direction == 1)
{
if(column + ships < dimensions)
{
for(int checking = ships; checking>0; checking--)
{
if(piecesBoard[row][temporaryColumn].equals("#"))
{
checkFree++;
temporaryColumn++;
}
}
if(checkFree == ships)
{
temporaryColumn = column;
for (int place = ships; place >0; place--)
{
piecesBoard[row][temporaryColumn] = "O";
temporaryColumn++;
}
done = true;
}
}
}
if(direction == 2)
{
if(row + ships < dimensions)
{
for(int checking = ships; checking > 0; checking--)
{
if(piecesBoard[temporaryRow][column].equals("#"))
{
checkFree++;
temporaryRow++;
}
}
if(checkFree == ships)
{
temporaryRow = row;
for (int place = ships; place > 0; place--)
{
piecesBoard[temporaryRow][column] = "O";
temporaryRow++;
}
done = true;
}
}
}
if(direction == 3)
{
if(column - ships >= 0)
{
for(int checking = ships; checking > 0; checking--)
{
if(piecesBoard[row][temporaryColumn].equals("#"))
{
checkFree++;
temporaryColumn--;
}
}
if(checkFree == ships)
{
temporaryColumn = column;
for (int place = ships; place >0; place--)
{
piecesBoard[row][temporaryColumn] = "O";
temporaryColumn--;
}
done = true;
}
}
}
}
while(!done);
}
}
public static void main(String[] args) {
BattleShip BattleShip = new BattleShip();
}
}