Puzzle8 Matrizes - Ordenação

1 resposta
java
K

Ola caros!
Tenho um algoritmo que precisa ordenar uma matriz de acordo com o jogo Puzzle8 (jogo neste link:
http://mypuzzle.org/sliding )

Porém estou obtendo o erro:

" Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

at java.util.ArrayList.rangeCheck(ArrayList.java:653)

at java.util.ArrayList.get(ArrayList.java:429)

at puzzle8.Puzzle8.main(Puzzle8.java:38)

Java Result: 1 " "

Já verifiquei o código, todas as funções mas não encontro o erro. Caso alguém me de uma solução ficarei muito grato!

Postei o codigo no Pastebin caso alguem ache melhor por la.
http://pastebin.com/Z0Tfyg2G -> tabuleiro
http://pastebin.com/qMLewjmr -> puzzle8

ou código Segue abaixo:

package puzzle8;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

/**
*

  • @author Kelvin
    */
    public class Tabuleiro implements Comparable {

    public int[][] posicoes = new int[3][3];

    public Integer tabInicial() {
    
    List numeros = new ArrayList();
    
    int j = 0;
    
    for (int q = 0; q < 9; q++) {
    
    numeros.add(q);
    
    }
    
    Collections.shuffle(numeros);
    
    for (int i = 0; i < 3; i++) {
    
    for (int x = 0; x < 3; x++) {
    
    this.posicoes[i][x] = numeros.get(j);
    
    j++;
    
    }
    
    }
    
    return null;
    
    }
    
    public List geraFilhos() {
    
    List novoTab = new ArrayList();
    
    int[][] novaPos = null;
    
    int[] posZero = achaZero();
    
    int x = posZero[0];
    
    int y = posZero[1];
    
    if (x == 0 && y == 0) {
         novaPos = new int[][]{{1, 0}, {0, 1}};
     } else if (x == 1 && y == 0) {
         novaPos = new int[][]{{0, 0}, {2, 0}, {1, 1}};
     } else if (x == 2 && y == 0) {
         novaPos = new int[][]{{1, 0}, {2, 1}};
     } else if (x == 0 && y == 1) {
         novaPos = new int[][]{{0, 0}, {1, 1}, {0, 2}};
     } else if (x == 1 && y == 1) {
         novaPos = new int[][]{{0, 1}, {1, 0}, {2, 1}, {1, 2}};
     } else if (x == 2 && y == 1) {
         novaPos = new int[][]{{1, 1}, {2, 0}, {2, 2}};
     } else if (x == 0 && y == 2) {
         novaPos = new int[][]{{0, 1}, {1, 2}};
     } else if (x == 1 && y == 2) {
         novaPos = new int[][]{{0, 2}, {1, 1}, {2, 2}};
     } else if (x == 2 && y == 2) {
         novaPos = new int[][]{{1, 2}, {2, 1}};
     }
     for (int z = 0; z < novaPos.length; z++) {
         Tabuleiro tabuNovo = this.copiaTabuleiro();
         tabuNovo.trocaPos(posZero, novaPos[z]);
         novoTab.add(tabuNovo);
     }
     return novoTab;
    

    }

    public void imprimiTab(int[][] tab) {
    
    for (int x = 0; x < 3; x++) {
    
    for (int i = 0; i < 3; i++) {
    
    System.out.print(tab[x][i]);
    
    }
    
    System.out.println();
    
    }
    
    }
    
    public int verificaAcertos(int[][] tab) {
    
    int cont = 0;
    
    int acertos = 0;
    
    int[] corretos = {1, 2, 3, 4, 5, 6, 7, 8, 0};
    
    for (int x = 0; x < 3; x++) {
         for (int y = 0; y < 3; y++) {
             if (tab[x][y] == corretos[cont]) {
                 acertos++;
             }
             cont++;
         }
     }
     return acertos;
    

    }

    private Tabuleiro copiaTabuleiro() {
    
    Tabuleiro novoTab = new Tabuleiro();
    
    novoTab.posicoes = this.posicoes.clone();
    
    return novoTab;
    
    }
    
    private void trocaPos(int[] posZero, int[] destino) {
    
    int valorAtual = this.posicoes[posZero[0]][posZero[1]];
    
    int valorDestino = this.posicoes[destino[0]][destino[1]];
    
    this.posicoes[posZero[0]][posZero[1]] = valorDestino;
    
    this.posicoes[destino[0]][destino[1]] = valorAtual;
    
    }
    
    public int[] achaZero() {
    
    for (int x = 0; x < 3; x++) {
    
    for (int y = 0; y < 3; y++) {
    
    if (this.posicoes[x][y] == 0) {
    
    return new int[]{x + 1, y + 1};
    
    }
    
    }
    
    }
    
    return null;
    
    }
    
    public boolean igual(int tab[][]) {
    
    for (int i = 0; i < 3; i++) {
    
    for (int x = 0; x < 3; x++) {
    
    if (this.posicoes[i][x] != tab[i][x]) {
    
    return false;
    
    }
    
    }
    
    }
    
    return true;
    
    }
    
    <a class="mention" href="/u/override">@Override</a>
    
    public int compareTo(Tabuleiro tabu) {
    
    if (verificaAcertos(this.posicoes) > verificaAcertos(tabu.posicoes)) {
    
    return -1;
    
    } else {
    
    return 1;
    
    }
    
    }
    
    }
    

package puzzle8;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

/**
*

  • @author Kelvin
    */
    public class Puzzle8 {

    public static void main(String[] args) {
    
    boolean result = true;
    
    List abertos = new ArrayList();
    
    List fechados = new ArrayList();
    
    List aux = new ArrayList();
    
    Tabuleiro tab = new Tabuleiro();
    
     tab.tabInicial();
     tab.imprimiTab(tab.posicoes);
    
     if (tab.igual(tab.posicoes) == false) {
         aux = tab.geraFilhos();
         for (int k = 0; k < aux.size(); k++) {
             if (tab.igual(aux.get(k).posicoes) == false) {
                 fechados.add(aux.get(k));
                 Collections.sort(fechados);
             }
         }
     }
     while (tab.igual(fechados.get(0).posicoes) == false) {
         abertos.add(fechados.get(0));
         tab.imprimiTab(aux.get(0).posicoes);
         fechados.remove(fechados.get(0));
         Collections.sort(fechados);
         if (result == true) {
             System.out.println("achou a resolucao");
         }
     }
    

    }
    }

1 Resposta

D

Provavelmente o problema está aí. A cada loop que passa, um item da lista é removido até que o tamanho da lista se torne zero. Quando não tem elementos da lista e é chamado fechados.get(0), a exceção IndexOutOfBoundsException é disparada.

Criado 2 de março de 2016
Ultima resposta 4 de mar. de 2016
Respostas 1
Participantes 2