Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

We are making a slidepuzzle for a project for school. We succeeded in making the puzzle but now we want to make an ' ARTIFICIAL INTELLEGINCE'. THis means the computer can calculate the shortest solution and solve the puzzle.

We are making a tree so he can look for the solution. We always get the 'nosuchelement' fault.

ArrayList positiesVoorHint = this is the position of the pieces in an arraylist shuffled, we made our own shuffle.

oplossingsposities.add(i) = these are the names of the pieces on index 0 it is piece number 1

We also defined the UP,DOWN,LEFT,RIGHT methods in another class and they are working perfectly.

testPuzzelOpgelost = testpuzzlesolved

We also had to clone the list because of our methods UP, DOWN,LEFT RIGHT. Don't worry about that.

(Our native language is not english).


This is the code which we use when we click on a button, so he makes 1 step closer to the shortest solution:

public ArrayList<Integer> HintUitvoeren()
 {
  ArrayList<Integer> positiesNaHint;
  HintBerekenen2 hint = new HintBerekenen2(posities, dimensie);
  ArrayList<ArrayList<Integer>> positieMatrix = hint.oplossingspad;
  afbeeldingenPlaatsen();
  positiesNaHint = positieMatrix.get(1);
  return positiesNaHint;
 }

This code should be perfectly fine

Problem should be here:

Here is the code; (for some reason it wont display as code, also with the 4 spaces)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

public class HintBerekenen2 extends Bewegingen
{
 Queue<ArrayList<Integer>> wachtrij;
 Map<ArrayList<Integer>, ArrayList<Integer>> boom;
 int dimensie;
 Bewegingen bew;
 ArrayList<ArrayList<Integer>> oplossingspad;
 ArrayList<Integer> meestRecentePosities, oplossingsposities;

 public HintBerekenen2 (ArrayList<Integer> positiesVoorHint, int dimensie)
 {
  System.out.println("HINT START HIER");
  this.dimensie = dimensie;

  wachtrij = new LinkedList<ArrayList<Integer>>();
  boom = new HashMap<ArrayList<Integer>,ArrayList<Integer>>();
  bew = new Bewegingen();


  //Oplossings ArrayList maken. ([1, 2, ..., (dimensie*dimensie) - 1,         (dimensie*dimensie)]
  oplossingsposities = new ArrayList<Integer>();

  for (int i = 1; i < (dimensie*dimensie) + 1; i++)
  {
   oplossingsposities.add(i);
  }

  voegToeAanBoom(positiesVoorHint, null);
  berekenen();
 }

 void voegToeAanBoom(ArrayList<Integer> nieuwePositie, ArrayList<Integer> oudePositie)
 {
  if (!boom.containsKey(nieuwePositie))
  {
   wachtrij.add(nieuwePositie);
   boom.put(nieuwePositie, oudePositie);
  }
 }

 void berekenen()
 {
  while (oplossingspad == null)
  {
   System.out.println(wachtrij + " voor remove");
   ArrayList<Integer> lijst = this.wachtrij.remove();
   System.out.println(wachtrij + " na remove");
   this.UP (lijst);
   this.DOWN (lijst);
   this.LEFT (lijst);
   this.RIGHT (lijst); 
  }
 }

 private void testPuzzelOpgelost(ArrayList<Integer> positiesVoorBeweging,     ArrayList<Integer> positiesNaBeweging)
 {
  System.out.println("Test wordt uitgevoerd");
  System.out.println(positiesVoorBeweging + " positiesVoorBeweging in de testPuzzel");
  System.out.println(positiesNaBeweging + " positiesNaBeweging in de testPuzzel");

  voegToeAanBoom (positiesNaBeweging, positiesVoorBeweging);

  if (positiesNaBeweging.equals(oplossingsposities))
  {
   ArrayList<Integer> zoekPositie = positiesNaBeweging;
   oplossingspad = new ArrayList<ArrayList<Integer>>();
   while (zoekPositie != null)
   {
    oplossingspad.add(0, zoekPositie);
    zoekPositie = boom.get(zoekPositie);
   }
  }
 }

 void UP (ArrayList<Integer> positiesVoorBeweging)
 {
  int indexLeegStukje = positiesVoorBeweging.indexOf(dimensie*dimensie);

  ArrayList<Integer> cloneLijst = (ArrayList<Integer>) positiesVoorBeweging.clone();
  meestRecentePosities = bew.UP(dimensie, positiesVoorBeweging, indexLeegStukje);

  if(meestRecentePosities != cloneLijst)
  {
   testPuzzelOpgelost(cloneLijst, meestRecentePosities);
  }
 }

 void DOWN (ArrayList<Integer> positiesVoorBeweging)
 {
  int indexLeegStukje = positiesVoorBeweging.indexOf(dimensie*dimensie);

  ArrayList<Integer> cloneLijst = (ArrayList<Integer>) positiesVoorBeweging.clone();
  meestRecentePosities = bew.DOWN(dimensie, positiesVoorBeweging, indexLeegStukje);

  if(meestRecentePosities != cloneLijst)
  {
   testPuzzelOpgelost(cloneLijst, meestRecentePosities);
  }
 }

 void LEFT (ArrayList<Integer> positiesVoorBeweging)
 {
  int indexLeegStukje = positiesVoorBeweging.indexOf(dimensie*dimensie);

  ArrayList<Integer> cloneLijst = (ArrayList<Integer>) positiesVoorBeweging.clone();
  meestRecentePosities = bew.LEFT(dimensie, positiesVoorBeweging, indexLeegStukje);

  if(meestRecentePosities != cloneLijst)
  {
   testPuzzelOpgelost(cloneLijst, meestRecentePosities);
  }
 }

 void RIGHT (ArrayList<Integer> positiesVoorBeweging)
 {
  int indexLeegStukje = positiesVoorBeweging.indexOf(dimensie*dimensie);

  ArrayList<Integer> cloneLijst = (ArrayList<Integer>) positiesVoorBeweging.clone();
  meestRecentePosities = bew.RIGHT(dimensie, positiesVoorBeweging, indexLeegStukje);

  if(meestRecentePosities != cloneLijst)    
  {
   testPuzzelOpgelost(cloneLijst, meestRecentePosities);
  }
 }
}

Here is the error code

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
 at java.util.LinkedList.remove(LinkedList.java:788)
 at java.util.LinkedList.removeFirst(LinkedList.java:134)
 at java.util.LinkedList.remove(LinkedList.java:481)
 at Mozaiq.HintBerekenen2.berekenen(HintBerekenen2.java:56)
 at Mozaiq.HintBerekenen2.<init>(HintBerekenen2.java:39)
 at Mozaiq.Schuifpuzzelpaneel.HintUitvoeren(Schuifpuzzelpaneel.java:74)
 at Mozaiq.Gamepaneel.actionPerformed(Gamepaneel.java:126)
 at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
 at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
 at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
 at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
 at java.awt.Component.processMouseEvent(Component.java:6382)
share|improve this question
Try !A.equals(B) instead of A != B, because == and != check reference (un)equality. – jlordo yesterday
You should supply the stack trace so that people can help more quickly and easily. Just post it in the same way you did your code. – wmorrison365 yesterday
Do you mean you get a NoSuchElementException? If so, it's likely to be in one of the calls to ArrayList#get or ArrayList#remove. Unfortunately, you mayhave to find out why the element isn't there. It's worth checking if a value is present rather than getting it blindly. – wmorrison365 yesterday
Hello, thank you for the help already. I added the error code in my question. I'm gonna try and do what you said. Thank you – Laurens Segers yesterday
It's not working. The problem is he goes UP,DOWN,LEFT,RIGHT,UP,DOWN,LEFT,RIGHT,UP,DOWN,LEFT,RIGHT. Exactly 3 times ( we printed it). So the original arraylist is again that arraylist. He moves 1 piece in a circle, and finally comes to its original state. Then the program crashes. – Laurens Segers yesterday
show 1 more comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.