-1

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--;
}
  • 1
    Did you test your code? `x--` will set `x` from 5 to 4... isn't that what you want? – sleepToken Dec 18 '19 at 19:04
  • 4
    Also, why not rename `x` to `numShips`? *Use meaningful variable names.* – sleepToken Dec 18 '19 at 19:05
  • It does set it to 4, but then it 'resets' and it will only ever change it from 5->4, then 5->4 again. And in my program they are named playersShips and computersShips, but for ease of reading I changed it here. – ericmcgrandle Dec 18 '19 at 19:06
  • 5
    https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1 – Tyler Dec 18 '19 at 19:07
  • @ericmcgrandle It is not possible to change the variable/value on the "caller" side from inside a method of a method call. But you can use classes and fields to store values which can be used/changed by more than one component. However, in this case you don't need such information as you can simply look on the board to see how many ships a player have. – Progman Dec 18 '19 at 19:16
  • @Progman I never considered looking at the board. I will go and try that. As for Tyler and Max Vollmer, that confuses me more to be honest, but I think those are only good for objects(?) and I am dealing with a primitive data type(?). I'll read up more on the pass-by-reference though. – ericmcgrandle Dec 18 '19 at 19:33

1 Answers1

3

This is what you are doing: changing the value of x within the scope of your method.

public static void main(String[] args) {
  int x = 5;

  decrement(x);
}

public static void decrement(int new_x) {
  new_x--;
  // now, new_x = 4... but if we don't return a value,
  // x remains unchanged
}

To solve this issue, you could have your method return an int:

public static void main(String[] args) {
  int x = 5;

  x = decrement(x); // we now return x-1, and set x to this new value
}

public static void decrement(int new_x) {
  return x--;
}

Alternatively (but not recommended), you could declare x at the class level:

static int x = 5;

public static void main(String[] args) {        
  decrement(); // no need to pass in anything now
}

public static void decrement() {
  x--; // x is now 4 in every method in this class
}

sleepToken
  • 1,866
  • 1
  • 14
  • 23
  • I understand the first section of code, only changing it within the scope of the method. I struggle with returning the value because in my actual program in one of the methods there are 2 possible return values. Either the player hits his own ship, playersShips--, or they hit the computers ships, computersShips--. Problem with the declaring it at the class level is that it wants it to be a static reference (which I do not fully understand and will read up on), but when I tried that it still did not change it beyond 4. IE, it never went to 3 or 2 or 1. Thanks for the replies! – ericmcgrandle Dec 18 '19 at 19:30
  • @ericmcgrandle then just declare it as static: `static int x = 5;` – sleepToken Dec 18 '19 at 19:32
  • I could be doing something wrong, but even when declared at the class level (as static) it will only ever go from 5->4 then resets at 5. Also, just out of curiosity, why is it not recommended to declare it at class level? – ericmcgrandle Dec 18 '19 at 19:40
  • 1
    @ericmcgrandle if you modify your question to include code in a [reprex] (something anyone can copy/paste and run), it will be easier to help. Also, check out: https://softwareengineering.stackexchange.com/questions/148108/why-is-global-state-so-evil – sleepToken Dec 18 '19 at 19:42
  • I uploaded the full code to github. Link is in main question. – ericmcgrandle Dec 18 '19 at 20:02