#Variables not updating after method

1 messages · Page 1 of 1 (latest)

pseudo quiverBOT
#

Detected code, here are some useful tools:

Formatted code
//Run Method
public static void main(String[] args) {
  boolean turn = true;
  boolean pWin = false;
  boolean cWin = false;
  boolean s1 = false;
  boolean s2 = false;
  boolean s3 = false;
  boolean s4 = false;
  boolean s5 = false;
  boolean s6 = false;
  boolean s7 = false;
  boolean s8 = false;
  boolean s9 = false;
  char [] [] board = {
      {
      '', '', ''}
    , {
      '', '', ''}
    , {
      '', '', ''}
  };
  System.out.println("Choose your spot");
  System.out.println("   " + 1 + "|" + 2 + "|" + 3);
  System.out.println("   " + "-+-+-");
  System.out.println("   " + 4 + "|" + 5 + "|" + 6);
  System.out.println("   " + "-+-+-");
  System.out.println("   " + 7 + "|" + 8 + "|" + 9);
  System.out.println("");
  System.out.println("");
  while (pWin == false && cWin == false) {
    boolean pValid = false;
    boolean cValid = false;
    if (turn) {
      pMove(board, pValid, s1, s2, s3, s4, s5, s6, s7, s8, s9);
      printBoard(board);
      pWin = checkPWin(board);
      turn = false;
      if (!pWin) {
        System.out.println("Bot is now going...");
      }
      System.out.println("s1 = " + s1 + " s2 = " + s2 + " s3 = " + s3 + " s4 = " + s4 + " s5 = " + s5 + " s6 = " + s6 + " s7 = " + s7 + " s8 = " + s8 + " s9 = " + s9);
      System.out.println("");
    }
    else if (!turn) {
      if (pWin == false && cWin == false) {
        cMove(board, cValid, s1, s2, s3, s4, s5, s6, s7, s8, s9);
        cWin = checkCWin(board);
        printBoard(board);
        turn = true;
        System.out.println("Your turn!");
        System.out.println("s1 = " + s1 + " s2 = " + s2 + " s3 = " + s3 + " s4 = " + s4 + " s5 = " + s5 + " s6 = " + s6 + " s7 = " + s7 + " s8 = " + s8 + " s9 = " + s9);
        System.out.println("");
      }
    }
  }
  if (pWin) {
    System.out.println("You won!");
  }
  else if (cWin) {
    System.out.println("You lost!");
  }
}
}
#

I uploaded your attachments as Gist.

#

<@&987246399047479336> please have a look, thanks.

#

You can use </chatgpt:1108714622413963314> to ask ChatGPT about your question while you wait for a human to respond.

#

An error has occurred while trying to communicate with ChatGPT.
Please try again later.

neon trout
#

The s1 you pass in your pMove is a separate s1 variable than the one in your main method. It is a COPY of the s1 in main method. Java method parameters are 'pass by value'. So if the type is primitive like int, long, char, byte, double, float, short, boolean then it's copied. If it's an object then you get a copy of a reference to that object in memory.

#

You currently have two representations of the game board, The double char[][] array which seems like it's just for output AND then the individual booleans representing whether someone has picked that position. You can combine those into a single char[] board array of length 9, and then pass that around to your move methods. If the position in the char[] array is zero then it hasn't be chosen yet. If it has been chosen it will have the appropriate X, or O in it. You will then create a printBoard(char[] board) which does the conversion of the board array to a printed string.

#

You can pass an array to a method, and do operations on it, because it is an Object, and thus the copied parameter is a different reference but it points to the same array in memory.

#

But passing an primitive to a method is just a copy of the primitive, and doing operations on it won't reflect back on the original value from where it came.

neon trout
#
public static void main(String[] args){
  int myValue = 10;
  tryToChangeMyValue(myValue);
  System.out.println(myValue);
}
public void tryToChangeMyValue(int myValue){
  myValue = 5;
}

what do you think this prints?

#

It does not show five

#

The variable named myValue in the tryToChangeMyValue method is just coincidentally named the same, it is not the same, it is a copy.

#

So setting it to five only changes the LOCAL value, not the value in the SCOPE of the main method.

#

This is in reference to all the s1=true, s2=true, bits in your move methods, they only set the local value in THAT method.

#

They are different variable, just named the same.

#

So if your board was modeled just with char [] board = new char[9];

#

You could pass that around to methods, because the board variable is a reference.

#
public static void main(String[] args){
    char[] board = new char[9];
    board[0] = 'X';  // set top left square to X
    if(setPositionOnBoard(board, 0, 'O')){
       System.out.println("player chooses O");
    }else{
       throw IllegalStateException("someone tried to change a move");
    }
}
public static boolean setPositionOnBoard(char[] brd, int pos, char value){
    if(brd[pos] == 0){ // not set yet
       brd[pos] = value;
       return true; // return true if something was set
    }
    // if you made it this far then someone has already chose this spot
    return false; // let them know nothing was done.
}
#

yes

#

9 spots, positions for them are 0-8

#

It specifies the size of the array.

#

No you have to set the size of an array at creation.

#

The point of that example is that I passed an object, not a primitive type. The object was an array.

#

Therefore you still got a copy of the lookup to that array in memory and so you can use it to make changes to it.

#

sorry, change it to boolean. It is common if you add something to a collection you return true if you actually added it or false if you could not.

#

That way in main I could check the return type, if it came back false then I know someone tried to change a square that was already set.

#

updated the example

#

It's an error, it throws and error and you program usually stops.

#

As you are writing the code, you can make these types of assertions about what you think the program is/should be doing. If you think it should never be able to change a position already played, then you won't get those errors.

#

if you did it right.

#

But if you get the error, then you know it was something is wrong and were it happened.

#

It's way to help yourself. Consider them as 'sanity checks'.

#

I often point people to the Basic Computer Games github. Lot's of little terminal games that are exactly 100-300 lines of code.

#

Most of these are ports from a BASIC program to other languages but the games are simple. Most of them have already been ported to Java..... but some of them have errors or are broken. Try to fix one.