I'm new here (stackoverflow and programming in general), and am trying to create a battleship game in Java. I am quite happy with what I have, and am aware that the code might not be the most effective, but that is not the problem I want to solve here.
When inputing where to place your ship or where to attack, before I had it so that it would ask what the column was, and then separately what the row was. Now I am trying to make a method (called coordinates, it is the last one) that will allow you to input the coordinates together (in the format 'A1'). It worked perfectly, until the input was wrong (had either two letters, two numbers, nothing in it, etc...)
Another thing is that in the coordinates method, in the catch I had to put return 20 because it said missing return statement, but I don't actually need to return anything as that is in a do-while loop. What can I do to take that away?
This is my first question on this site, so I don't know if anyone will need this (I don't know if there are any rules against unnecessary code), but I will leave the whole program here, as well as an example output.
public static int firing(String[][] board, int hits, int torpedoes, int shipSize, int shipAmount, int whatPlayer)
{
Scanner userInput = new Scanner(System.in);
System.out.println("What coordinate do you want?");
String coordinate = userInput.nextLine();
int col = coordinates(coordinate, "col");
int row = coordinates(coordinate, "row");
while (col > 8 || col < 1 || row > 8 || row < 1)
{
System.out.println("That is not a valid coordinate.");
Scanner input = new Scanner(System.in);
System.out.println("What coordinate do you want?");
coordinate = input.nextLine();
col = coordinates(coordinate, "col");
row = coordinates(coordinate, "row");
}
}
public static int letterToNumber(String colLet)
//colLet = column in letter
//This method is to change the column letters to numbers
{
int num = 1;
while (!colLet.equalsIgnoreCase("A")&&
!colLet.equalsIgnoreCase("B")&&
!colLet.equalsIgnoreCase("C")&&
!colLet.equalsIgnoreCase("D")&&
!colLet.equalsIgnoreCase("E")&&
!colLet.equalsIgnoreCase("F")&&
!colLet.equalsIgnoreCase("G")&&
!colLet.equalsIgnoreCase("H"))
{
System.out.println("That is not a valid coordinate (not one of the letters)");
Scanner input = new Scanner(System.in);
System.out.println("Please enter a valid coordinate: "
+ "\nIt should be in the format 'A1' (column then row)");
String coordinate = input.nextLine();
colLet = "" + coordinate.charAt(0);
}
switch (colLet.toLowerCase())
{
case "a": num = 1; break;
case "b": num = 2; break;
case "c": num = 3; break;
case "d": num = 4; break;
case "e": num = 5; break;
case "f": num = 6; break;
case "g": num = 7; break;
case "h": num = 8; break;
default: System.out.println("That wasn't a letter!");
}
return num;
}
public static int coordinates(String coordinate, String RorC)
// RorC is for Row or column
{
boolean isValid;
if (RorC.equals("row"))
{
do
{
try
{
String rowStr = "" + coordinate.charAt(1);
int row = Integer.parseInt(rowStr);
isValid = true;
return row;
}
catch(Exception e)
{
System.out.println("Error");
Scanner userInput = new Scanner(System.in);
System.out.println("That is an invalid coordinate (row probably only had one character)"
+ "\nPlease enter a valid coordinate."
+ "\nIt should be in the format 'A1' (column then row)");
coordinate = userInput.nextLine();
isValid = false;
return 20;
}
}while(isValid = false);
}
else if (RorC.equals("col"))
{
do
{
try
{
String colLet = "" + coordinate.charAt(0);
int col = letterToNumber(colLet);
isValid = true;
return col;
}
catch (Exception e)
{
System.out.println("Error");
Scanner userInput = new Scanner(System.in);
System.out.println(""
+ "That is an invalid coordinate (col probably had nothing inside it)"
+ "\nPlease enter a valid coordinate."
+ "\nIt should be in the format 'A1' (column then row)");
coordinate = userInput.nextLine();
isValid = false;
return 20;
}
}while(isValid=false);
}
else
{
return 0;
}
}
}
I know my code isn't very efficient, but I will clean it up after I am done. Here is an example of output (I don't know how to put this is in a box without code format)
It is now player 1's turn to choose where to place their ships.
Can the other player please turn around.
You will now be placing ship number 1.
Please write 'H' if you want to create a horizontal ship,
or 'V' if you want it to be vertical.
h
________________________________
A B C D E F G H
1 - - - - - - - -
2 - - - - - - - -
3 - - - - - - - -
4 - - - - - - - -
5 - - - - - - - -
6 - - - - - - - -
7 - - - - - - - -
8 - - - - - - - -
________________________________
What coordinates do you want your ship to start on?
If it is horizontal, it will go right from there,
and if it is vertical, it will go down from there.
Please enter the coordinates in the format 'A1' (column then row).
If you enter anything after that, it will be ignored.
u9
That is not a valid coordinate (not one of the letters)
Please enter a valid coordinate:
It should be in the format 'A1' (column then row)
a1
That is not a valid coordinate (addship).
What coordinates do you want your ship to start on?
Please enter them in the format 'A1' (column then row).
If you enter anything after that, it will be ignored.
d7
________________________________
A B C D E F G H
1 - - - - - - - -
2 - - - - - - - -
3 - - - - - - - -
4 - - - - - - - -
5 - - - - - - - -
6 - - - - - - - -
7 - - - S S S - -
8 - - - - - - - -
________________________________
This is your board.
Would appreciate any help, even if it is about something different (not the problem I am addressing).