access data from the array being in another class?

0

I have an array in a class called Logica.

And I'm manipulating it from another class called Game.

But when I pass a value from the Game class to the array in the Logic class and Czech if the value was stored in the array it returns me 0, as if I did not have "saved" in the array.

I have to leave the array in the main class so that it never misses and accepts past values?

Game class

public class Jogo {
 Logica logi =  new Logica();

public static void main(String[] args) throws Exception{

    //exibe Tabuleiro :) 
    new Tabuleiro();        

 }

    public boolean validaJogada(int lin, int col){        
    if(logi.devolveValor(lin, col) != 0){
        return false;
    }else{
        System.out.println("Valida Jogo Classe:JOGO " +logi.devolveValor(lin, col));
        return true;
    } 
 }

public void realizaJogada(int lin, int col, int jogador){
   logi.recebeJogada(lin, col, jogador);
   System.out.println(montaMatriz());

}

public String montaMatriz(){
    String matriz;
    int tabu[][] = new int [3][3];
    tabu = logi.devolveTabuleiro();

    matriz = tabu[0][0]+"|"+tabu[0][1]+"|"+tabu[0][2] + "\n";

    for(int i=1; i < 3; i++){
        for(int j=2; j < 3; j++){
           matriz += tabu[i][j]+"|"+tabu[i][j]+"|"+tabu[i][j] + "\n";
        }
    }

    return matriz;      

}

Logica class

public class Logica {
public int tab[][] = new int[3][3];
private int jogador;


public int[][] devolveTabuleiro(){
   int i,j;

   for(i=0; i <=2 ; i++){
       for(j=0; j<=2; j++){
           System.out.println(this.tab[i][j]);
       }
   }

   return tab;
}

public boolean recebeJogada(int lin, int col, int jogador){        
    tab[lin][col] = jogador;   

    System.out.println("Recebi: col: " + col + " Linha: " + lin+ " do Jogador: "+ jogador);


    return true;
}

public int devolveValor(int lin, int col){
    return tab[lin][col];
}

The problem I'm having with this code is that every time I add a new value to the array it loses its previous values. It is as if the array were created every time I add a value.

What am I doing wrong?

    
asked by anonymous 25.03.2015 / 19:45

1 answer

1

You must instantiate an object of type Logica in your class Jogo , so you can access its array. Example:

Logical class:

public class Logica{
  //declara a matriz
  private String [][]matriz = new String[2][2];

  public void setarValor(int x, int y, String valor)
  {
      matriz[i][j] = valor;
  }

  public String[][] retornaMatriz()
  {
      return matriz;
  }

  public String retornaValorPosicaoMatriz(int x, int y)
  {
      return matriz[x][y];
  }
}

Game Class:

public class Jogo{
  //declara objeto de logica, podendo assim acessar a matriz
  Logica logica = new Logica();

  //seta valores na matriz
  logica.setarValor(0,0,"X");

  //obtem a matriz de retorno
  String matriz[][] = logica.retornaMatriz();

  //obtem o valor de uma posicao da matriz
  String valor = logica.retornaValorPosicaoMatriz(0,0);

  //obtem o valor de uma posicao da matriz, outra forma
  String valor2 = matriz[0,0];
}
    
25.03.2015 / 21:11