Collision through for loop [closed] - c#

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
for (int x = bulletBounds.Count - 1; x > -1; x-- )
{
for (int y = alienPosition.Count - 1; y > -1; y--)
{
if (bulletBounds[x].Intersects(alienBounds[y]))
{
alienPosition.RemoveAt(y);
bulletBounds.RemoveAt(x);
hit++;
}
}
}
What I'm trying to do with this code is go through both lists of objects and see if they collide. I saw in another question that the only way to do this is iterate backwards through the list so I did that. The issue now is that when a bullet collides with an alien, every alien before it in the list is also getting deleted. So if I have 6 aliens on the screen and I hit the one on the far left, every alien to the right of it gets deleted! How do I fix this?

After removing the alien and bullet you need to break out of the inner loop.
if (bulletBounds[x].Intersects(alienBounds[y]))
{
alienPosition.RemoveAt(y);
alienBounds.RemoveAt(y);
bulletBounds.RemoveAt(x);
hit++;
break;
}

Add break; to your if loop. By the way, what's the difference between alienBounds and alienPosition? You're looping through alienPosition, however, accessing data in alienBounds. In the example below, I'm just looping through alienBounds. If I misunderstood anything, please let me know, and I will update my answer accordingly.
foreach (var bullet in bulletBounds)
{
foreach (var alien in alienBounds)
{
if (bullet.Intersects(alien))
{
bulletBounds.Remove(bullet);
alienBounds.Remove(alien);
break;
}
}
}
Hope this helps!

Related

Out of bounds exception on incrementing variable [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I've been while out of C# and MVC. And I am really struggling with the following error, I really don't see it. I have a list of restrictions and i want to add their keys to an string[].
int cntr = 0;
//loop through restrictions and add to array
foreach (var Restriction in this.admingroupRepository.Context.AdminRestrictions.ToList())
{
currentRestrictionKeys[cntr] = Restriction.Key;
cntr += 1;
}
This is the error i get on the cntr += 1 line:
Index was outside the bounds of the array.
I don't understand where this comes from, the foreach breaks before the cntr is out of the array's bounds right?
You have allocated too little space for currentRestrictionKeys. But you don't really need to preallocate it at all; you can just use a trivial projection with LINQ:
var currentRestrictionKeys = this.admingroupRepository.Context.AdminRestrictions
.Select(r => r.Key)
.ToArray();

Arrays and multiple methods [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to make a c# program with a main() method holds an array of 8 integers and the second method asks the user for numbers that fill the array.
I'm lost on how to get the second method to pass the integers into the array in the main method.
My hunch is that you don't know how to work with parameters or return values.(How methods communicate together).If it's the case, you should really read about it.I'm going to imply you are using a console application. Look at this :
private void SomeMethod()
{
int[] myArray = new int[8];
for(int i = 0; i < myArray.Lenght - 1; i++)
myArray[i] = GetInputNumber();
}
private int GetInputNumber()
{
return Convert.ToInt32(Console.ReadLine());
}

Looking for SpanIncluding equivalent in C# [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm porting a c++ class to C# and i have a difficulty.
I would like to find an equivalent of SpanIncluding.
Here is my cpp code :
while (Notes.Mid(j,1).SpanIncluding("0123456789").IsEmpty()!=NULL){}
Anyone can help me please ?
I believe SpanIncluding starts matching from the start of the string, stopping when the first non-matching character is found.
So one formulation in the general case would be this:
string match = new string(someString.ToCharArray().
TakeWhile(c => "0123456789".Contains(c)).ToArray());
(or an equivalent using a regular expression).
However, in the example given in the question there's only one character so the whole thing probably boils down to a test of whether this character is >= '0' and <= '9':
while(char.IsDigit(Notes[j])) { ... };
I found the MSDN page for SpanIncluding, and it seems like a ridiculously specific function. I can't really understand what it tries to solve, since it has some strange caveats.
LINQ would be one way of implementing it:
string text = "2334562";
IEnumerable<char> spannedChars = text.TakeWhile(c => "1234567890".Contains(c));
This is a more direct port of SpanIncluding than queen3's option, if I understand the MSDN page correctly, because the result set should stop the minute it hits a character not in the spanning string.

Trouble with using Reflection [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
So, what I am trying to do is iterate through an array of x values, and for each iteration, get the current text value of label "plabel" + x (the labels are all already created and named. I am relatively new to using reflection, but from what I've read the following should work:
PropertyInfo pI;
pI = this.GetType().GetProperty("plabel" + count + ".Text"); //count is the current iteration #
MessageBox.Show(pI.Name);
But this is throwing a runtime exception. Can somebody please show me the correct way of doing this?
Try something like this:
//Gets the label (includes private fields)
FieldInfo fi = this.GetType().GetField("plabel" + count,
BindingFlags.NonPublic | BindingFlags.Instance);
Label label = fi.GetValue(this) as Label;
if (label != null)
{
MessageBox.Show(label.Text);
}

IOrderedQueryable<T> and Take() [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
What I need is to order IQueriable (the result of LINQ to SQL method) and return first count elements if count is defined. I use the following code:
IOrderedQueryable<MainLog> list = logs.OrderByDescending(item => item.Time);
if (count > 0) list = list.Take(count).OrderByDescending(item=>item.Time);
It works fine, but I don't like calling OrderByDescending twice. Can I make it any better without breaking the order and also without double-ordering?
There is absolutely no need for the second OrderByDescending. Simply remove it:
IQueryable<MainLog> list = logs.OrderByDescending(item => item.Time);
if (count > 0)
list = list.Take(count);

Categories

Resources