I am new to programming and I have to generate a random tour. In the first step I have to generate a reandom index, which is not visited yet. However my cannot figure out how to write the List, which will give me the random coordinates.
This is what I have so far:
class TSP
{
class Point //Class point that holds a x and y coordinate
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
public Point() { }
public override string ToString()
{
return $"Point(X:{X}/Y:{Y})";
}
}
class City
{
public static Random rand = new Random();
public City(int x, int y)
{
X = rand.Next();
Y = rand.Next();
}
public double X { get; private set; }
public double Y { get; private set; }
public City () { }
public override string ToString()
{
return $"City(X: {X}/Y:{Y})";
}
}
static void Main(string[] args)
{
// takes the coordinates from my file
List<Point> points = new List<Point>();
string[] lines = File.ReadAllLines("C:/Users/t/Documents/Coordinates.txt");
for (int i = 6; i < lines.Length; i++)
{
string[] splitLine = lines[i].Split(' ');
int x = Convert.ToInt32(splitLine[1]);
int y = Convert.ToInt32(splitLine[2]);
points.Add(new Point(x, y));
}
foreach (Point p in points)
{
Console.WriteLine(p);
}
// Writes down the random coordinates for new Cites
List<City> cities = new List<City>();
for (int i = 3; i < lines.Length; i++)
{
string[] splitLine = lines[i].Split(' ');
int x = Convert.ToInt32(splitLine[1]);
int y = Convert.ToInt32(splitLine[2]);
points.Add(new Point(x, y));
cities.Add(new City(x, y));
}
foreach (City c in cities )
{
Console.WriteLine(c);
}
}
}
I think my problem is in the Main Program (List cities = new List();) - if I am right it is in the for loop but I don't know how to write it down differently.
Aucun commentaire:
Enregistrer un commentaire