c# - Objects in list seem to share dictionary keys and vaules -


im coding small program tries calculate how many robots can escape hole within 5, 10 , 20 seconds, robots travel speed of 10 m/s.

the robots , holes have 2 floating point numbers coordinates , figure out distance between holes , robots use pythagoras theorem.

to keep track of robot closest hole equiped each hole dictionary robots id key , distance hole value.

there can different scenarios, maxium 10, different numbers , holes.

input looks this, first number n number of robots in field followed n lines of x/y coordinates. second number m number of holes followed m lines of x/y coordinates.

the input ends 0.

3 0.0 0.0 10.0 0.0 0.0 10.0 3 99.0 0.0 0.0 1000.0 1000.0 1000.0 3 0.0 0.0 100.0 0.0 200.0 0.0 2 95.0 50.0 105.0 50.0 0 

theese classes

    class robot {     public int id { get; private set; }     public double posx { get; private set; }     public double posy { get; private set; }     public bool ishiding { get; set; }      public robot(double posx, double posy, int id)     {         posx = posx;         posy = posy;         id = id;     } }  class hole {     public dictionary<int, double> candidates { get; set; }             public double posx { get; private set; }     public double posy { get; private set; }     public bool isoccupied { get; set; }     public hole(double posx, double posy)     {         posx = posx;         posy = posy;     } }  class scenario {     public int id { get; private set; }     public list<duration> durations { get; set; }      public scenario(int id)     {         id = id;     } }  class duration {     public int time { get; private set; }     public int survivors { get; set; }     public list<robot> robots { get; set; }     public list<hole> holes { get; set; }      public duration(int time)     {         time = time;     }  } 

i collect input this, , belive might problem. although me seems objects instanciated properly.

void collectinputs() {     (int = 0; < 10; i++)     {         scenario scenario = new scenario(i + 1);         list<duration>  durations = new list<duration>();          list<robot> robots = new list<robot>();         list<hole> holes = new list<hole>();          int newrobots = int.parse(console.readline());         if (newrobots == 0)         {             break;         }          (int iii = 0; iii < newrobots; iii++)         {             string[] inputcoordsrobot = console.readline().split(' ');             robot robot = new robot(double.parse(inputcoordsrobot[0], cultureinfo.invariantculture), double.parse(inputcoordsrobot[1], cultureinfo.invariantculture), iii + 1);             robots.add(robot);         }          int newholes = int.parse(console.readline());         (int iii = 0; iii < newholes; iii++)         {             string[] inputcoordshole = console.readline().split(' ');             hole hole = new hole(double.parse(inputcoordshole[0], cultureinfo.invariantculture), double.parse(inputcoordshole[1], cultureinfo.invariantculture));             holes.add(hole);                      }          (int ii = 5; ii <= 20; ii = ii + ii)         {              duration duration = new duration(ii);              duration.robots = new list<robot>(robots);             duration.holes = new list<hole>(holes);              durations.add(duration);              scenario.durations = new list<duration>(durations);          }         _scenarios.add(scenario);     }  } 

this distance calculation.

void unleashstorm()         {             foreach (var scenario in _scenarios)             {                 foreach (var duration in scenario.durations)                 {                     double movementcapacity = duration.time * 10.0f;                      foreach (var hole in duration.holes)                     {                         dictionary<int,double> robotsinrange = new dictionary<int, double>();                          foreach (var robot in duration.robots)                         {                             double distancetox = robot.posx > hole.posx ? robot.posx - hole.posx : hole.posx - robot.posx;                             double distancetoy = robot.posy > hole.posy ? robot.posy - hole.posy : hole.posy - robot.posy;                              double distancetohole = math.sqrt(math.pow(distancetox, 2) + math.pow(distancetoy, 2));                              if (distancetohole <= movementcapacity)                             {                                 robotsinrange.add(robot.id, distancetohole);                             }                          }                          hole.candidates = new dictionary<int, double>(robotsinrange);                     }                 }             }         } 

i noticed in debugger each duration if added specific holes dictoinary key , value seem shared between same hole in duration calculations.

im thinking problem me throwing pointers around in bad way either in input collection method or in calculating distance method cant seem figure 1 out.

any solving appreciated.

the problem not dictionary, it's reference copy in duration.holes = new list<hole>(holes); durations.holes share reference of holes list created in collectinputs

to avoid this, need assign values of holes duration.holes. 1 option make hole class cloneable

        class hole : icloneable         {             public dictionary<int, double> candidates { get; set; }             public double posx { get; private set; }             public double posy { get; private set; }             public bool isoccupied { get; set; }             public hole(double posx, double posy)             {                 posx = posx;                 posy = posy;              }              public object clone()             {                 var hole = new hole(this.posx, this.posy)                 {                     isoccupied = this.isoccupied,                     candidates = this.candidates                 };                 return hole;             }         }      // , replace      // duration.holes = new list<hole>(holes);     //      duration.holes = new list<hole>(holes.select(x=>x.clone() hole)); 

you may have other places use reference, check it.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -