What happens with the reference to the object?

0

My intention was to create a JForm in which to play the cat, every time you save that JForm in a list, at the end of a certain number of games won (4 in this code) show the windows that you save in the list where you can see how the game was won and if it was with X or O.

The problem is that if it does what I want, but I can not understand what happens internally in the object and the reference to it. In the checkForWin() method I pass to the list the reference to the object by this and it works correctly, unlike when instead of this I pass the reference to otro .

else {
        TicTacCollection.gatos.add(otro);

otro is an object of the same class instantiated within it in the setMarca() method as seen in the following code.

public class Board extends javax.swing.JFrame {

public JButton[][] board;
private char currentPlayerMark;
Tablero otro;

public Tablero() {

    board = new JButton[3][3];
    currentPlayerMark = 'X';
    initComponents();
    initializeBoard();
    setLocationRelativeTo(null);
}

private void initializeBoard() {
    board[0][0] = b1;
    board[0][1] = b2;
    board[0][2] = b3;
    board[1][0] = b4;
    board[1][1] = b5;
    board[1][2] = b6;
    board[2][0] = b7;
    board[2][1] = b8;
    board[2][2] = b9;
}

private void reset(){
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            board[i][j].setText("");
        }
    }
}

public boolean isBoardFull() {
    boolean isFull = true;

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (board[i][j].getText().equals("")) {
                isFull = false;
            }
        }
    }
    return isFull;
}

public boolean checkForWin() {
    boolean winner = (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin());

    if (winner) {
        if (TicTacCollection.gatos.isEmpty()) {
            TicTacCollection.gatos.add(this);
        } else {
            TicTacCollection.gatos.add(this);
        }
    }
    return winner;
}

private boolean checkRowsForWin() {
    for (int i = 0; i < 3; i++) {
        if (checkRowCol(board[i][0].getText(), board[i][1].getText(), board[i][2].getText()) == true) {
            return true;
        }

    }
    return false;
}

private boolean checkColumnsForWin() {
    for (int i = 0; i < 3; i++) {
        if (checkRowCol(board[0][i].getText(), board[1][i].getText(), board[2][i].getText()) == true) {
            return true;
        }
    }
    return false;
}

private boolean checkDiagonalsForWin() {
    return ((checkRowCol(board[0][0].getText(), board[1][1].getText(), board[2][2].getText()) == true) || (checkRowCol(board[0][2].getText(), board[1][1].getText(), board[2][0].getText()) == true));
}

private boolean checkRowCol(String c1, String c2, String c3) {
    return ((c1.length() > 0) && (c1.equals(c2)) && (c2.equals(c3)));
}

public void setMarca(JButton boton) {
    if (currentPlayerMark == 'X') {
        boton.setText("X");
        currentPlayerMark = 'O';
    } else {
        currentPlayerMark = 'X';
        boton.setText("O");
    }

    if (checkForWin()){
        JOptionPane.showMessageDialog(null, "El ganador fue el jugador con la marca: " + boton.getText());

        System.out.println("EN GATO HAY " + TicTacCollection.gatos.size());
        if (TicTacCollection.gatos.size() == 4) {
            TicTacCollection.showMatches();
        }
        if (TicTacCollection.gatos.size() >= 1 && TicTacCollection.gatos.size() < 4) {

            otro = new Tablero();
            this.hide();
            otro.setVisible(true);
            otro.setTitle("otro tablero");
        }

    } else if (isBoardFull()) {
        JOptionPane.showMessageDialog(null, "Empate");
        reset();
    }

}
    @SuppressWarnings("unchecked") //Código auto generado ...

 /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
Tablero tabla = new Tablero();
tabla.setVisible(true);

        }
    });
}

//Cada uno de los nueve botones tiene su 'ActionPerformed' donde se invoca 'setMarca(boton)'

private void b1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-  FIRST:event_b1ActionPerformed 
    setMarca(b1);
}//GEN-LAST:event_b1ActionPerformed    

In this class the boards are stored

public class TicTacCollection {
    public static ArrayList<Tablero> gatos = new ArrayList<Tablero>();    

    static int x =10,y=20;

    public ArrayList<Tablero> devuelveGatos(){
        return gatos;
    }

    public static void showMatches(){

        System.out.println("Estas son las partidas y los resultados");
        for (int i = 0; i < gatos.size(); i++) { 

            if(i != 0){
                gatos.get(i).setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
            }
            gatos.get(i).setTitle("Gato"+i);
            gatos.get(i).setLocation(x,y);x+=286;
            gatos.get(i).setVisible(true);            
        }
    }

}
    
asked by Alec Raya 17.01.2017 в 01:13
source

1 answer

2

What happens is the following:

You have a field Tablero otro in your class Tablero . If you pass this otro to ArrayList in TicTacCollection , the reference is passed to this variable and added to the list. The next time it is called checkForWin() , this field is over written with a new Tablero , also affecting the reference saved in the list.

I recommend you take a walk and read a bit about concepts of object-oriented programming and then start over from scratch. As you use fields declared as static in classes basically as global variables you risk confusion, and in any more complex project you will cause pure headaches.

    
answered by 17.01.2017 в 01:36