0

I've developed an android Travelling Salesman Problem (TSP) game application in which the user makes a path and plays against the computer, which uses the TSP algorithm to make a full path every time the user clicks to join two points with a line. Currently my method for the computer's path being draw is called whenever the user makes their move. However, my code is only allowing the first two points in the computer's path (stored in the arraylist called 'test') to be joined up.

public void CompDrawLine(List<Point> test) {
        int d = 0;
        int i;
        test.add(test.get(0));
        Point c = test.get(d);

        for (i=0;i<test.size();i++)

        {
            cPath.moveTo(c.x,c.y);
            c = test.get(d+1);
            cPath.lineTo(c.x,c.y);
            mCanvas.drawPath(cPath,cPaint);
            // String testIndex = "this is iteration" + i;
             // Toast.makeText(mContext, testIndex, LENGTH_SHORT).show();
        }
         cPath.reset();
}

How do I get the complete path drawn whenever the method is called??

1 Answers1

1

I think your error is in the second line of your for loop. The variable d is never being incremented, so you are always using points 0 and 1. Personally, I would get rid of the d variable and just use i like this:

c = test.get(i+1);

However, another option would be to use d and increment it each time:

c = test.get(++d);

It must be a pre-increment though, or else you will be going from point 0 to point 0, and then point 1 to point 1, etc. instead of point 0 to 1, since d is initialized to 0.

gla3dr
  • 2,179
  • 16
  • 29
  • Thank you, but that causes it to crash. I'm pretty sure I have tried it before (I've been stuck on this for a few days now:( ). Any other suggestion? – Amna Hussain Mar 09 '15 at 23:10
  • You are probably getting an array index out of bounds. Since you are going all the way to `i < test.size()`, `test.get(i+1)` will be out of bounds. You will have to stop your for loop at `i < test.size() - 1`. – gla3dr Mar 09 '15 at 23:12
  • so here is what it looks like now and it still crashes public void CompDrawLine(List test) { // int d = 0; int i=0; test.add(test.get(0)); Point c = test.get(i); for (i=0;i<(test.size()-1);i++) { cPath.moveTo(c.x,c.y); c = test.get(i+1); cPath.lineTo(c.x,c.y); mCanvas.drawPath(cPath,cPaint); LENGTH_SHORT).show(); } cPath.reset(); – Amna Hussain Mar 09 '15 at 23:17
  • And that doesn't work? Are there any error messages you are getting? – gla3dr Mar 09 '15 at 23:22
  • `it still crashes` - What do you mean by that? It throws an exception? What type of exception? – Julian Mar 09 '15 at 23:23
  • Ok so I re-compiled it and it works. However, I now need the path to clear for the next path to be drawn. At the moment a new path is being drawn onto the previous one. – Amna Hussain Mar 09 '15 at 23:29
  • 2
    That is an entirely separate problem. You should try to figure it out on your own and post another question if you run into issues. – gla3dr Mar 09 '15 at 23:30
  • I was referring to "cPath.reset()" not doing its job as it is part of this function. But thanks for the advice. – Amna Hussain Mar 09 '15 at 23:36