algorithm - Knights Tour recursive C# Im getting something wrong in my way of doing the stuff -


class knight {     public static readonly double legaldistance = math.sqrt(5);     public stack<field> steps { get; set; }      private static readonly list<field> board = board.gameboard;     private static list<field> fields;             private static readonly random random = new random();     private static readonly object synlock = new object();       public knight(field initial)     {         steps = new stack<field>();         steps.push(initial);     }      public void move()     {         field destination = choose();          if (destination == null)         {             return;         }          console.writeline("moving " + getposition().getfieldname() + " " + destination.getfieldname());         steps.push(destination);     }      public field back()     {         field = steps.pop();         console.writeline("moving " + from.getfieldname() + " " + getposition().getfieldname());          return from;     }      public field choose()     {         list<field> legalmoves = behaviour();          legalmoves.removeall(field => steps.contains(field, new fieldvaluecomparer()));          if (legalmoves.count == 0)         {             return null;         }          field thechoosenone;          int index;         lock (synlock)         {             index = random.next(0, legalmoves.count);         }          thechoosenone = legalmoves.elementat(index);           return thechoosenone;     }      private list<field> behaviour()     {         fields = new list<field>();         fields.addrange(board);          (int = fields.count - 1; >= 0; i--)         {             double actualdistance = fields[i].getdistance(getposition());             if (!actualdistance.equals(legaldistance))             {                 fields.remove(fields[i]);             }         }          return fields;     }     public list<field> getsteps()     {         return steps.tolist();     }      public field getposition()     {         return steps.peek();     } } 

so how i'd stuff. problem is, missing key functionality, because on low given stepcount backtracks start, on high stepcount, causes stackoverflow.

here other functions let understand want do: calculating distance:

public double getdistance(field other) {        return math.sqrt(math.pow(other.x - x, 2) + math.pow(other.y - y, 2)); } 

finding path:

class pathfinder     {         public static void findpath(knight knight)         {              if (knight.steps.count != 20)             {                 knight.move();                  findpath(knight);                  knight.back();             }         }     } 

your path search random walk. on large board, may take while anyway.

now stackoverflow: notice don't push on move() when there no places go. so, on recursive call findpath() there still same knight.steps.count, same position, same null return on choose()... , on, until you're out of stack space.

obvious fix add bool return value move() indicating if there was move. unless there actual reason behind using random moves, more deterministic search algorithm recommended.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -