How to use struct to cover all integers in 2d array? - c#

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!

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:
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.

Related

The shortest way to initialize a point array?

I am looking for the shortest way in terms of writing to declare an array of points.
My problem is that I have humongous point data that I want to hardcode as an initialization.
These initializations repeat the 'new Point' multiple times:
Point[] points1 = new[] { new Point { X = 0, Y = 0 }, new Point { X = 20, Y = 120 }, new Point { X = 40, Y = 60 }, }; // kinda long typing
Point[] points2 = { new Point(0, 0), new Point(20, 120), new Point(40, 60) }; // better
Alternatively I could declare the array like so:
int[,] arr = new int[,] { { 0, 0 }, { 20, 120 }, { 40, 60 } }; // so far shortest typing
But how can I cast int[,] to Point[] ?
Are there other alternatives (like using lists) ?
You can change new[] to new Point[]. This way, you can use target-typed new in the array elements:
Point[] points1 = new Point[] {
new() { X = 0, Y = 0 },
new() { X = 20, Y = 120 },
new() { X = 40, Y = 60 },
};
If Point has a 2-parameter constructor, this can be even shorter:
Point[] points1 = new Point[] {
new(0, 0),
new(20, 120),
new(40, 160)
};
If points1 is a local variable, you can make it even shorter by making the variable implicitly typed:
var points1 = new Point[] {
new(0, 0),
new(20, 120),
new(40, 160)
};
If you want to make this really short in the long run, you can make a (int, int)[], then convert to Point[]:
Point[] points1 = new[] {
(0, 0),
(20, 120),
(40, 160)
}.Select(x => new Point(x.Item1, x.Item2)).ToArray();
Definitely not the shortest way, But this approach has its own advantages. and it's only a one-time effort.
Making it configuration-driven will help in modifying the points. if in future you want to add/delete/modify points. It will help you in testing and also provide different02 points.
second, you can manage different points based on the environment as well.
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
public class Root
{
public Point[] Points { get; set; }
}
string json = File.ReadAllText("inut.json");
Root obj = JsonConvert.DeserializeObject<Root>(json); //Use NewtonSoft.json library to deserialize the JSON to object.
Sample JSON:
{
"Points": [{
"X": 0,
"Y": 0
},
{
"X": 10,
"Y": 20
}
]
}

Load random scenes without repetition using c#

I want to load scenes randomly without repetition using c#. Any help would do.
Thanks.
int[] array = new int[] { 1, 2, 3, 4, 6, 8, 9, 10, 11, 12 };
List<int> list = new List<int>();
void Start()
{
list.AddRange(array);
}
int GetUniqueRandom(bool RemoveFromTheList)
{
if (list.Count == 0)
{
if (RemoveFromTheList)
{
list.AddRange(array);
}
else
{
return -1; // never repeat
}
}
int rand = Random.Range(0, 10);
int value = list[rand];
list.RemoveAt(rand); return value;
}
A nice clean way is to shuffle the array, then put all the elements in a stack. All you need to get a random element is to pop an item off the stack.
You will want to remove the list in the list of fields and replace with this;
Stack remainingScenes = new Stack();
Remove the content of the Start() method - you don't need it.
In your method to get a new number;
if (remainingScenes.Count == 0) {
int n = array.Length;
while (n > 1)
{
int k = rng.Next(n--);
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
foreach(var element in array) {
remainingScenes.Push(element);
}
}
return remainingScenes.Pop();
The shuffle method is from here.
Uhmmm, this looks very straightforward. Judging from your code, you only need a little modification to make it work..
List<int> list = new List<int>() { 1, 2, 3, 4, 6, 8, 9, 10, 11, 12 };
int GetUniqueRandom(bool RemoveFromTheList)
{
if (list.Count == 0)
{
return -1;//nothing in the list so return negative value
}
//generate random index from list
int randIndex = Random.Range(0, list.Count - 1);
int value = list[rand];
if(RemoveFromTheList)
{
list.RemoveAt(randIndex);
}
return value;
}
Try this:
int[] array = new int[] { 1, 2, 3, 4, 6, 8, 9, 10, 11, 12 };
Stack<int> stack = null;
Then initialize like this:
var rnd = new Random();
stack = new Stack<int>(array.OrderBy(x => rnd.Next()));
Now you just keep getting values from the stack until it is empty:
var value = stack.Pop();

Generate random number from two List<>

i want to generate random number from two list. i want to create a function where i pass how much random number from two list.
List<int> integers = new List<int>() { 54, 23, 76, 123, 93, 7, 3489 };
List<int> value2 = new List<int>() { 1, 3, 4, 6, 8, 17, 40 };
i want my result = List<int> result = {54,40,123,17,3,1,3489,76...etc}
When i run again the set of result will be change.
Presently i am using this function that return List
public static List<int> GenerateRandom(int count)
{
// generate count random values.
HashSet<int> candidates = new HashSet<int>();
while (candidates.Count < count)
{
// May strike a duplicate.
candidates.Add(random.Next(1,30));
}
// load them in to a list.
List<int> result = new List<int>();
result.AddRange(candidates);
// shuffle the results:
int i = result.Count;
while (i > 1)
{
i--;
int k = random.Next(i + 1);
int value = result[k];
result[k] = result[i];
result[i] = value;
}
return result;
}
i am calling the function
List<int> vals = GenerateRandom(20);
But i want that the random number from two List<> List<int> integers and List<int> value2 . so how can i do this .
You can do something like this:
var result =
integers.Concat(value2)
.OrderBy(x => random.Next())
.Take(count)
.ToList();
You could write a general-purpose function to give you a random ordering of any number of sequences, like so:
public static IReadOnlyCollection<T> InRandomOrder<T>(Random rng, params IEnumerable<T>[] lists)
{
return lists
.SelectMany(x => x)
.OrderBy(y => rng.Next())
.ToList();
}
You can then pass as many lists as you like and get the contents back in a fully randomised order:
var list1 = new[] {1, 2, 3, 4, 5};
var list2 = new[] {6, 7, 8};
var list3 = new[] {9, 0};
Random rng = new Random();
for (int i = 0; i < 10; ++i)
{
var randomisedFirst5 = InRandomOrder(rng, list1, list2, list3).Take(5);
Console.WriteLine(string.Join(", ", randomisedFirst5));
}
There's a less efficient approach you can use that avoids the need for an instance of Random, but you should only use this for short lists or where you really don't care about performance. It uses Guid.NewGuid() to generate random numbers:
public static IReadOnlyCollection<T> InRandomOrder<T>(params IEnumerable<T>[] lists)
{
return lists
.SelectMany(x => x)
.OrderBy(y => Guid.NewGuid())
.ToList();
}
Even the more efficient approach isn't the fastest. A faster way would be to use reservoir sampling to take the first N items that you want, and put them into an array which you shuffle using Knuth. That would make it a lot faster, at the expense of more complicated code - meaning you should only do it the fast way if it's really needed.
If what you want is to select a number that exists either in list A or B, randomly, you can do:
List<int> integers = new List<int>() { 54, 23, 76, 123, 93, 7, 3489 };
List<int> value2 = new List<int>() { 1, 3, 4, 6, 8, 17, 40 };
List<int> allInOne = new List<int>(integers.Concat(value2));
Random r = new Random(DateTime.Now.Millisecond);
/********************************
For demonstration purposes
********************************/
for(int i = 0; i < 5; i++)
{
var randomListIndex = r.Next(0, allInOne.Count - 1);
Console.WriteLine(allInOne[randomListIndex]);
}
Use KeyValuePair
static void Main(string[] args)
{
List<KeyValuePair<int, int>> results = GenerateRandom(100);
}
static List<int> integers = new List<int>() { 54, 23, 76, 123, 93, 7, 3489 };
static List<int> value2 = new List<int>() { 1, 3, 4, 6, 8, 17, 40 };
static Random random = new Random();
public static List<KeyValuePair<int,int>> GenerateRandom(int count)
{
List<KeyValuePair<int,int>> result = new List<KeyValuePair<int,int>>();
for(int i = 0; i < count; i++)
{
int firstValue = integers[random.Next(0, integers.Count - 1)];
int seconValue = value2[random.Next(0, value2.Count - 1)];
result.Add(new KeyValuePair<int,int>(firstValue,seconValue));
}
return result;
}​
I actually made a library a while back that handles some of this stuff : Underscore.cs
It's a nuget package so easy to install, the code to shuffle or take a sample randomly of two lists is :
var ls1 = GenerateRandom(10);
var ls2 = GenerateRandom(20);
var mixer = ls1.Concat(ls2).ToList();
//if you want all of the items shuffled use shuffle
var result = _.List.Shuffle(mixer);
//or if you want a subset randomly sorted use sample
result = _.List.Sample(mixer);

c# - How can I add a value to a multi array?

I have this array here:
float[, ,] vectors;
int pos = 0;
void Start() {
vectors = new float[,,] { {
{ 0, 1, 1 },
{ 0, 2, 2 } }
};
}
This works. I fill the array with numbers.
Now I want to add some values again to a given position. But how?
This are not working:
vectors[pos] = new float[,,] { { { 33, 44, 55 } } };
or
vectors[pos] = { { { 33, 44, 55 } } };
I searched, but not found the right answer.
EDIT:
I want something like this:
[0]+
[0] {1, 2, 3},
[1] {4, 5, 6}
[1]+
[0] {11, 22, 33},
[1] {44, 55, 66},
[2] {77, 88, 99}
...
etc.
Now, e.g. I want add values {10,10,10} to pos = 0. But how?
If you want to add values I suggest using generic lists instead of arrays. And you should create your own Vector class or find one that is suitable to your needs like this.
public class Vector
{
public float X { get; private set; }
public float Y { get; private set; }
public float Z { get; private set; }
public Vector(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
}
Then you can do the following
var vectors = new List<List<Vector>>
{
new List<Vector>{
new Vector(0, 1, 1),
new Vector(0, 2, 2)
}
};
vectors[0].Add(new Vector(33,44,55));
And your vectors will contain
[0]
[0] {0, 1, 1}
[1] {0, 2, 2}
[2] {33, 44, 55}
Note that if you need to add to the first dimention you have to do this.
vectors.Add(new List<Vector>());
vectors[1].Add(new Vector(1, 2, 3));
And now you have
[0]
[0] {0, 1, 1}
[1] {0, 2, 2}
[2] {33, 44, 55}
[1]
[0] {1, 2, 3}
You should determine the other positions within the array, you are just specifying one. If your problem cannot be solved within lists
you can try array of arrays as follows
float [][][] x = new float [n][m][];
// initialize the third dimension until m is reached
x[0] = new float {1,2,3,4,5}; // specify whatever value you want
x[1] = new float {3,2,4};
x[2] = new float [3];
// etc until m is reached
// do the same for the n dimension
This will work for you, the array is assigned, you cannot change it you must expand the array as is.
float[, ,] vectors;
int pos = 0;
vectors = new float[,,]
{
{
{ 0, 1, 2 }, { 0, 3, 4 }
}
};
vectors = new float[,,]
{
{
{vectors[0,0,0], vectors[0,0,1], vectors[0,0,2]}, { vectors[0,1,0], vectors[0,1,1], vectors[0,1,2] }, { 33,44,55}
}
};

C# Iterating an If statement with new value each time

Is it possible to iterate nested if statements with a new value with every single iteration? I am trying to build a 1-dimensional cellular automata (for homework, I cannot deny it) and I'm completely new to C# as the following code will no doubt assure. I have tried to create this program using the most straightforward, basic, DIY methods available and have run myself into a rut.
I've got a string of 1's and 0's of length 8, say
string y;
y = "11110000";
I want to break this set up in 8 substring sets of 3 with each set comprising of a value in y together with a single value on either side of it. So counting from 0, the 3rd set would be 110, the 7th would be 001. However substrings will only provide the 1st to 6th set as I can't loop them around y to my liking so I defined the following-
y1=y.Substring(7,1)+y+y.Substring(0,1);
Using y1 I was able to get out all the substrings necessary. These were defined pretty basically as follows-
string a0, a1, a2, a3, a4, a5, a6, a7;
a0 = y1.Substring(0, 3);
a1 = y1.Substring(1, 3);
a2 = y1.Substring(2, 3);
a3 = y1.Substring(3, 3);
a4 = y1.Substring(4, 3);
a5 = y1.Substring(5, 3);
a6 = y1.Substring(6, 3);
a7 = y1.Substring(7, 3);
The rules for the next generation of cellular automata are up to the user in this program- that is to say the user can choose whether or not a substring, say 111->0 or 1 for all iterations. I used (an awful lot of) if tables in the following way for each substring
{
if (a0=="000")
{
Console.Write(a);
}
else if (a0=="001")
{
Console.Write(b);
}
else if (a0 =="010")
{
Console.Write(c);
}
else if (a0 == "011")
{
Console.Write(d);
}
else if (a0 == "100")
{
Console.Write(e);
}
else if (a0 == "101")
{
Console.Write(f);
}
else if (a0 == "110")
{
Console.Write(g);
}
else if (a0 == "111")
{
Console.Write(h);
}
}
where a,b,c,d,e,f,g,h are ints and are rules chosen by the user. So say for instance the user decides that each set 000 should result in a 1 value, then a=1. b corresponds to {0,0,1}, c to {0,1,0} and so on. However the fairly obvious problem with this method is that I end up with only 1 generation in ints that I can't get at. I'd love to replace y1 with this new generation (converted into a string). If this isn't possible let me know!
This link might also clear things up a bit
and here's how you COULD have gotten an A+ :D
private static int[,] HipPriestsHomework()
{
string y = "11110000";
Console.WriteLine(y);
var rules = new[]
{
new {pattern = 0, result = 0},
new {pattern = 1, result = 1},
new {pattern = 2, result = 1},
new {pattern = 3, result = 1},
new {pattern = 4, result = 1},
new {pattern = 5, result = 0},
new {pattern = 6, result = 0},
new {pattern = 7, result = 0},
};
Dictionary<int, int> rulesLookup = new Dictionary<int, int>();
foreach(var rule in rules)
{
rulesLookup.Add(rule.pattern, rule.result);
}
int numGenerations = 10;
int inputSize = y.Length;
int[,] output = new int[numGenerations, inputSize];
int[] items = new int[y.Length];
for(int inputIndex = 0; inputIndex< y.Length; inputIndex++)
{
string token = y.Substring(inputIndex, 1);
int item = Convert.ToInt32(token);
items[inputIndex] = item;
}
int[] working = new int[items.Length];
items.CopyTo(working, 0);
for (int generation = 0; generation < numGenerations; generation++)
{
for (uint y_scan = 0; y_scan < items.Length; y_scan++)
{
int a = items[(y_scan - 1) % items.Length];
int b = items[y_scan % items.Length];
int c = items[(y_scan + 1) % items.Length];
int pattern = a << 2 | b << 1 | c;
var match = rules[pattern];
output[generation, y_scan] = match.result;
working[y_scan] = match.result;
Console.Write(match.result);
}
working.CopyTo(items, 0);
Console.WriteLine();
}
return output;
}

Categories

Resources