3 Replies - 1726 Views - Last Post: 25 October 2015 - 09:53 AM Rate Topic: -----

#1 allnex   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-October 15

Infinite loop when finding solution with backtracking

Posted 25 October 2015 - 08:48 AM

Hi, I made a Peg Solitaire solver using backtracking. The problem is that the backtrack part gives me an infinite loop. I tried differents methods but I always get the same result. Here is the code:

    private void buscarSolucion(Tablero tablero, ArrayList<Movimiento> solucion, Booleano exito) {

        if(tablero.esSolucion())
           exito.setValor(true);

        ArrayList<Movimiento> movs = tablero.obtenerMovimientos();
        for (Movimiento mov : movs) {
            if (tablero.esMovimientoValido(mov)) {
                tablero.anotarMovimiento(mov);
                solucion.add(mov);
                if(tablero.esSolucion()) {
                    exito.setValor(true);
                    return;
                } else
                    buscarSolucion(tablero, solucion, exito);
                if(!exito.getValor()) {
                    tablero.desanotarMovimiento(mov);
                    solucion.remove(mov);
                }
            }
        }
        exito.setValor(false);
    }


Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Infinite loop when finding solution with backtracking

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: Infinite loop when finding solution with backtracking

Posted 25 October 2015 - 09:12 AM

Can you post enough code so the program can be compiled and executed for testing?

How are you debugging the code? Where is the infinite loop? What value would stop the looping?

This post has been edited by NormR: 25 October 2015 - 09:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 allnex   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-October 15

Re: Infinite loop when finding solution with backtracking

Posted 25 October 2015 - 09:43 AM

View PostNormR, on 25 October 2015 - 09:12 AM, said:

Can you post enough code so the program can be compiled and executed for testing?

How are you debugging the code? Where is the infinite loop? What value would stop the looping?


Yes, here you have the classes: https://www.dropbox....acktracking.zip

I put a breakout point at the end of the for loop, but I don't know what is causing the loop. The loop should stop when tablero.esSolucion() or exito.getValor() is true.
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: Infinite loop when finding solution with backtracking

Posted 25 October 2015 - 09:53 AM

Sorry, I do not download from another site. Please make a small simple driver that calls your method and post it. It should compile and execute for testing.

Quote

I don't know what is causing the loop

Add some println statements that print out the values of the variables as they are used and changed. The print out should show you what the computer sees when it executes the code.

What conditions are needed to end the loop? Does the code make the needed changes so those conditions are true and allow the loop to exit?

This post has been edited by NormR: 25 October 2015 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1