access matrix data while in another class?

Asked

Viewed 673 times

0

I have a matrix in a class called Logica.

And I’m manipulating her from another class called Game.

But when I pass a value of the Game class to the matrix in the Logica class and check if the value was saved in the matrix it returns me 0, as if I had not "saved" in the matrix .

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

class game

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 that is occurring to me in this code is that every time I add a new value to the matrix it loses the previous values. It’s like the matrix is created every time I add a value.

what am I doing wrong ??

  • Where are you calling the class methods Jogo?

1 answer

1

You must instantiate an object of the type Logica in your class Jogo, then you can access your matrix. Example:

Classe Logica:

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];
}
  • I am following this, but I believe I must be doing something wrong. pq after set a value to the matrix and call aqla position it accepts. but when set another value at a different position returns the matrix with the last value I "set", as if deleting the other values.

  • I’m asking too much if I ask you to analyze my code ? is that I no longer know what to do to fix this ... :/

  • edit your question and enter the code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.