Ajuda com Condiçao do If?

1 resposta
L
Boa noite gostaria de uma ajudinha com a condiçao do IF , pois preciso atualizar um arquivo em txt com a coordenada atual . quando digo coordenada atual ,quero saber em qual linha e coluna ele esta no momento e preecher com "|o" . segue o codigo abaixo . import java.io.*;
public class Matriz {

    private String[] vet;
    private Boolean[][] matriz;
    private Integer x;
    private Integer y;

    public Integer getX() {
        return x;
    }

    public Integer getY() {
        return y;
    }

    public Boolean[][] CriaMatriz(int lin, int col) {
        this.matriz = new Boolean[lin][col];

        return this.matriz;

    }

    public Boolean[][] preecheMatriz() throws FileNotFoundException, IOException {
        FileReader fileReader = new FileReader("B:\\Downloads\\refontesexercciodolabirinto\\configuracaoLabirinto.txt");
        BufferedReader buffer = new BufferedReader(fileReader);

        int cont = 0;
        int j, col, lin = -1;
        String linha;
        String[] vet = null;
        do {
            linha = buffer.readLine();

            if (cont >= 1 && linha != null) {
                j = 0;
                col = 0;
                vet = linha.split("\\|");
                while (cont >= 1 && j <= vet.length - 1) {

                    if (vet[j].equalsIgnoreCase("x")) {
                        this.matriz[lin][col] = false;

                    }
                    if (vet[j].equalsIgnoreCase(" ")) {
                        this.matriz[lin][col] = true;

                    }
                    if (vet[j].equalsIgnoreCase("o")) {

                        this.matriz[lin][col] = true;
                        this.x = col;
                        this.y = lin;
                    }
                    if (j >= 1) {
                        col++;
                    }
                    j++;

                }
            }
            cont++;
            lin++;
            if (linha != null) {
            }

        } while (linha != null);


        buffer.close();
        return this.matriz;

    }

    public void imprimeMatriz() {
        for (int l = 0; l < this.matriz.length; l++) {
            for (int c = 0; c < this.matriz[l].length; c++) {
                System.out.print(this.matriz[l][c] + "\t");
            }
            System.out.println("");
        }
    }

    public Coordenada buscarSaida(Coordenada origem) throws IOException {

        return buscarSaida(origem, null);
    }

    private Coordenada buscarSaida(Coordenada pontoAtual, Coordenada pontoAnterior) throws IOException {
        FileWriter fileWriter = new FileWriter("B:\\Downloads\\refontesexercciodolabirinto\\LogEventos.txt", true);
        BufferedWriter buffer = new BufferedWriter(fileWriter);
        String log = "";
        for (int lin = 0; lin < this.matriz.length; lin++) {
            for (int col = 0; col < this.matriz[lin].length; col++) {
                if (this.matriz[lin][col] == true) {
                    log += "| ";

                }
                if (this.matriz[lin][col] == false) {
                    log += "|x";
                }
                if(){
                     log+="|o";
                }
               
            }
            log += "|";
            log += "\r\n";
        }


        
        if (verificarSaida(pontoAtual)) {
            
            return pontoAtual;
        } else {
            
            Coordenada pontoDireita = new Coordenada(pontoAtual.getX() + 1, pontoAtual.getY());
            Coordenada pontoEsquerda = new Coordenada(pontoAtual.getX() - 1, pontoAtual.getY());
            Coordenada pontoCima = new Coordenada(pontoAtual.getX(), pontoAtual.getY() - 1);
            Coordenada pontoBaixo = new Coordenada(pontoAtual.getX(), pontoAtual.getY() + 1);

            Coordenada[] pontos = new Coordenada[]{pontoBaixo, pontoDireita, pontoEsquerda, pontoCima};

            for (Coordenada ponto : pontos) {
                if (validarCoordenada(ponto)
                        && (pontoAnterior == null || !ponto.equals(pontoAnterior))) {
                    
                    Coordenada teste = buscarSaida(ponto, pontoAtual);
                    if (teste != null) {
                       
                        return teste;
                    }
                }
            }
        }
        buffer.write("Log de eventos:");
        buffer.newLine();
        buffer.write(log);
        buffer.close();
        return null;
        
    }

    private Boolean validarCoordenada(Coordenada ponto) {

        return ponto.getX() >= 0 && ponto.getY() >= 0
                && ponto.getY() < this.matriz.length
                && ponto.getX() < this.matriz[0].length
                && this.matriz[ponto.getY()][ponto.getX()];
    }

    private Boolean verificarSaida(Coordenada ponto) {

        return (ponto.getX().equals(0) || ponto.getY().equals(0)
                || ponto.getY().equals(this.matriz.length - 1)
                || ponto.getX().equals(this.matriz[0].length - 1)) && (this.matriz[ponto.getY()][ponto.getX()]);
    }
}

1 Resposta

L

OBS : IF da linha 101

Criado 10 de março de 2013
Ultima resposta 10 de mar. de 2013
Respostas 1
Participantes 1