Java - Method is changing other instances of the same object type
<p>The method evalBoard() below takes brd, and copies it's parameters into pos. It then iterates through some possible values, making changes to pos by calling makeMove().  These calls to pos.makeMove are somehow changing the values inside brd as well.  It can be seen clearly by stepping through the area I've marked in evalBoard. Why is this happening?</p>

<pre><code>public Move evalBoard(Board brd) {
    // evaluates current board and returns point of best move

    Move best;
    Move tmp = new Move(-1, -1, LOSE, brd.getPlayer());
    if (brd.getPlayer() == PLAYER1) {
        best = new Move(-1, -1, LOSE, PLAYER1);
    } else {
        best = new Move(-1, -1, WIN, PLAYER2);
    }
    // check if board is empty if so return optimal move
    //

    if (brd.isEmpty()) {

        return (new Move(0, 0, 0, PLAYER1));

    }

    // iterate through possible moves
    for (int x = 0; x &lt; 3; x++) {
        for (int y = 0; y &lt; 3; y++) {
            if (brd.getSpace(x, y) == ' ') {
                // copy all of brd to pos

                pos=new Board(brd.getBoard(),brd.getPlayer());

                tmp.setX(x);
                tmp.setY(y);
                tmp.setPlayer(brd.getPlayer());
/*
 *   problem can be seen by stepping through the next line.  brd is changed at the same time.
*/
                pos.makeMove(tmp);//Problem is here this call is changing brd as well

                // evaluate for immediate win or loss
                if (brd.getPlayer() == PLAYER1) {
                    if (pos.win()) {// immediate win
                        tmp.setValue(WIN);
                        return tmp;
                    } else { // not a winning move check it's recursive
                                // value
                        pos.setPlayer(PLAYER2);
                        tmp.copyFrom(evalBoard(pos));
                        if (tmp.getValue() &gt;= best.getValue()) {
                            best.copyFrom(tmp);
                        }

                    }
                } else {
                    if (pos.win()) {// immediate loss
                        tmp.setValue(LOSE);
                        return tmp;
                    } else {// not a losing move check it's recursive value
                        pos.setPlayer(PLAYER1);
                        tmp.copyFrom(evalBoard(pos));
                        if (tmp.getValue() &lt;= best.getValue()) {
                            best.copyFrom(tmp);
                        }
                    }
                }

            }
        }
    }
    return best;

}
</code></pre>

<p>Here's the Board class:</p>

<pre><code>public class Board {
final boolean PLAYER1=true;
final boolean PLAYER2=false;
public char[][] board = new char[3][3];
public boolean Turn;

public Board(char[][] brd,boolean plr1){
    board=brd;
    Turn=plr1;

}
public char getPlayerChar(){
    if (Turn==PLAYER1){
        return 'X';
    }else{
        return 'O';
    }
        }
public char[][] getBoard(){
    return board;
}
public void setBoard(char[][] brd){
    board=brd;
}
public void displayBoard(){
    for(int y=0;y&lt;3;y++){
        for(int x=0;x&lt;3;x++){
            System.out.print(board[x][y]);
            if (x&lt;2){
                System.out.print(" | ");
            } 

        }
        System.out.println("");
        if (y&lt;2){
            System.out.println("----------");

        }

    }
}
public void makeMove(Move mv){
    //System.out.println("hit");

    if (mv.getPlayer()==PLAYER1){
        board[mv.getX()][mv.getY()]='X';
    }else{
        board[mv.getX()][mv.getY()]='O';
    }

}
public void setPlayer(boolean plr){
    Turn=plr;
}
public boolean getPlayer(){
    return Turn;
}
public char getSpace(int x,int y){
    return board[x][y];

}
public boolean isEmpty(){
    for(int x=0;x&lt;3;x++){
        for(int y=0;y&lt;3;y++){

            if(!(board[x][y]==' ')){
                return false;
            }
        }
    }
    return true;
}
public boolean win(){
    char plrChar;
    //returns true if board is winning position for current player
    if (Turn==PLAYER1){
        plrChar='X';
    }else {
        plrChar='O';
    }
    //Across
    for(int y=0;y&lt;3;y++){
        if (board[0][y]+board[1][y]+board[2][y]==(plrChar+plrChar+plrChar)){
            return true;
        }
    }
    //Up/Down
    for(int x=0;x&lt;3;x++){
        if (board[x][0]+board[x][1]+board[x][2]==(plrChar+plrChar+plrChar)){
            return true;
        }
    }
    //Diagonals
        //top left to bottom right
    if (board[0][0]+board[1][1]+board[2][2]==(plrChar+plrChar+plrChar)){
        return true;
    }
        //bottom left to top right
    if (board[0][2]+board[1][1]+board[2][0]==(plrChar+plrChar+plrChar)){
        return true;
    }
    return false;
}


}
</code></pre>
