I am making battleship in java. I'm at the very end of the game but I cannot figure out how to reduce playersShips/computersShips when they are 'hit'. Is there a way to modify x and y so if they are reduced to 4, the int value does not reset at 5?
I've looked around and I know that int values cannot be modified this way, but I read something about using a wrapper class? Did not know if that could be applied here? I also considered changing the int value to a char array and using something like (a-e), then converting the letter to the ascII table and reducing it from there, but that does not seem very efficient. At the current way I am trying to make the game I cannot return the int value because in my playersTurn method it has the possibility of returning either x or y, and you can only return 1 value (correct?).
If Tl:Dr, simply put I need a way to reduce x and y in a way that the values stays at the new integer. 5->4->3->2->1->Game over. Below is simplified code. Thank you!
public static void main(String[] args) {
int x = 5; //PlayersShips
int y = 5; //ComputersShips
play(field, x, y);
}
public static void play(char[][] field, int x, int y){
while(x != 0 || y != 0) {
playersTurn(field, x, y);
computersTurn(field, x, y);
}
}
public static void computersTurn(char[][] field, x, y){
//Computer randomly chooses an x and y coordinate to 'attack'.
//The '&' symbol is the player's ships
if(field[x][y] == '&') {
x--;
}