0

this is my first post. I'm taking a programming course right now and my current assignment is to create an integer (we will call it the ant) which will move around to all integers in a 2d array (randomized path). Here is my code thus far:


namespace Ant
{
    class Program
    {
        static void Main(string[] args)
        {
            int ant;
            int i = 0;
            int[,] numberGrid =
            {
                {1, 2},
                {3, 4},
                {5, 6},
                {7, 8},
                {9, 10},
                {10, 11},
                {11, 12},
                {13, 14},
                {15, 16},
                {17, 18},
                {19, 20},
            };
            do
            {

                Random rand = new Random();
                ant= rand.Next(numberGrid[10, 1]);
                Console.WriteLine(ant);
                i++;
            } while (i !=110);
            Console.WriteLine("It took {0} steps for the ant to cover all spaces!", i);
        }
    }
}

I have the 2d array and I have temporarily set the ant up for a randomized path which will go on for 110 times before it stops. I'm supposed to integrate struct into this so that the ant will only go until it has visited all the integers of the 2d array instead of a set amount of times, but I'm absolutely clueless as to how I'm supposed to do this. If anyone could help me understand that would be great, thank you!

antman27
  • 3
  • 1
  • Do you want ant to perform 110 steps? Currently your code just spawn ant 110 times at ([not even](https://stackoverflow.com/q/767999/1997232)) random cell of the map.. or what is this array exactly? It can be replaced with function. – Sinatr May 06 '20 at 07:05
  • It sounds like you're supposed to create a struct called 'Ant' that stores both the x and y co-ordinates instead of creating an int like you've done. In terms of the spaces you could simply remove the co-ordinates from a list or something once they've been visited, so you're only left with un-visited spaces. Once that list is empty, you've visited them all. You could wrap that logic in a while loop or something. – sr28 May 06 '20 at 07:44

1 Answers1

0

Without more detail of how your expected to do this it sounds like you need to make the Ant a struct and record where the Ant has been (or not been). Here's a way you could do it, though I'm sure performance-wise there's a better way:

static void Main(string[] args)
{
    var unvisitedSpaces = new List<Coordinates>
    {
        //I've used your numbers but should this be a full matrix i.e. [1,1], [1,2], [1,3] etc.?
        new Coordinates(1, 2),
        new Coordinates(3, 4),
        new Coordinates(5, 6),
        new Coordinates(7, 8),
        new Coordinates(9, 10),
        new Coordinates(11, 12),
        new Coordinates(13, 14),
        new Coordinates(15, 16),
        new Coordinates(17, 18),
        new Coordinates(19, 20) 
    };

    var ant = new Ant();
    int counter = 0;
    var r = new Random();
    var min = Math.Min(unvisitedSpaces.Min(x => x.X), unvisitedSpaces.Min(y => y.Y));
    var max = Math.Max(unvisitedSpaces.Max(x => x.X), unvisitedSpaces.Max(y => y.Y)) + 1;

    do
    {
        ant.X = r.Next(min, max);
        ant.Y = r.Next(min, max);
        counter++;

        //check if the ant hasn't visited this space by checking the unvisitedSpaces list.
        if (unvisitedSpaces.Any(c => c.X == ant.X && c.Y == ant.Y))
        {
            //if it hasn't visited (the list contains that set of coordinates) then remove it from the list as it's now visited it.
            var coord = unvisitedSpaces.FirstOrDefault(c => c.X == ant.X && c.Y == ant.Y);
            unvisitedSpaces.Remove(coord);
        }
    } while (unvisitedSpaces.Count() > 0);

    Console.WriteLine("It took {0} steps for the ant to cover all spaces!", counter);
    Console.ReadLine();
}

public struct Coordinates
{
    public int X { get; }
    public int Y { get; }

    public Coordinates(int x, int y)
    {
        X = x;
        Y = y;
    }
}

public struct Ant
{
    public int X { get; set; }
    public int Y { get; set; }
}

Result: enter image description here

UPDATE

Added in the ability for it to automatically adjust the max and min values to be used by the 'random' by getting them from the coordinates matrix. Any adjustments to the matrix should therefore be included in the spaces the 'Ant' visits.

sr28
  • 4,728
  • 5
  • 36
  • 67
  • 1
    Yes, thank you! This is exactly what I asked for. I have adjusted the coordinates aswell so I have [1, 1], [1, 2].. etc all the way up to [1, 30]. It works exactly as needed and I wouldn't have been able to do it without your help. – antman27 May 06 '20 at 09:43
  • @Anthony - if you've increased the matrix to 30 just be sure to increase max random to 31. Better yet, set the values used in the random to be retrieved from min & max from the List – sr28 May 06 '20 at 10:23