1. ## Logic Problem

I am having difficulty figuring this problem out. The program (may be familiar out there, from the Linux Format Magazine), it draws 5 randomly colored (red, green, and blue) fireworks on the screen. You can only choose fireworks of the same color with the mouse and hit the space bar to blow them up. The challenge is to program in a white firework that acts as a bridge to allow you choose more then one colored firework. Below is the method that determines this. I can't quite get my head wrapped around how to choose a third color, it currently will let me choose one color and white ones, but not a third color. Thoughts?

Code:
```private void CheckSelectFirework(Point point)
{
foreach(Firework firework1 in Fireworks)
{
if(PointOverRect(point.X, point.Y, firework1.X, firework1.Y,
FireworkTypes[firework1.Angle][firework1.Colour].Width,
FireworkTypes[firework1.Angle][firework1.Colour].Height))
{
foreach(Firework firework2 in Fireworks)
{
if(firework2.IsSelected && firework2.Colour != firework1.Colour &&
!firework1.IsWhite && !firework2.IsWhite)
{
firework2.IsSelected = false;
}
}
firework1.IsSelected = true;
}
}
}```

2. Phyxashun,

Try using the disjunction for the conditional comparing the color of firework1 and firework2. The conjunction 'and' seems out of place, because not both of them would be white according to the specification/design that you've given.

Best Regards,

New Ink -- Henry

3. ## Slowly but surely

Okay, so I added a static variable to track whether or not a white rocket has been selected. This allows me select all rockets if I have a white rocket selected. This, of course, isn't the solution to the problem, but I believe that it is a step in the right direction. My solutions to problems are never as elegant as others, so if you are disgusted by the code below, I apologize. However, anyone have any ideas on how to meet the real intent of the problem: using a white rocket as a bridge between two different colored rockets? Rather than being able to choose all colors when I have a white one selected?

Code:
```private void CheckSelectFirework(Point point)
{
foreach(Firework firework1 in Fireworks)
{
if(PointOverRect(point.X,
point.Y,
firework1.X,
firework1.Y,
FireworkTypes[firework1.Angle][firework1.Colour].Width,
FireworkTypes[firework1.Angle][firework1.Colour].Height))
{
foreach(Firework firework2 in Fireworks)
{
if(!WhiteSelected && firework2.IsSelected &&
firework2.Colour != firework1.Colour)
{
firework2.IsSelected = false;
}
}
firework1.IsSelected = true; // This line is the culprit, but can't figure out the problem

foreach(Firework firework in Fireworks)
{
if(firework.IsSelected && firework.IsWhite)
{
WhiteSelected = true;
break;
}
else
{
WhiteSelected = false;
}
}
}
}
}```

4. I would keep track of the color of the last firework selected. Note the hexadecimal for each color of the rockets: red (0xff0000), green (0x00ff00), blue (0x0000ff), and white (0xffffff). If you require that bitwise-and of the color of the last firework selected and the next selected rocket be non-zero, you should achieve the effect you're looking for.

For example, I first select a red rocket. My last_color = 0xff0000.
If I try to select a blue rocket, I see that I can't because my last_color and selected_color don't "match". 0xff0000 & 0x0000ff == 0.
If I select another red rocket, last_color and selected_color obviously "match". 0xff0000 & 0xff0000 == 0xff0000. last_color = 0xff0000.
If I select a white rocket, last_color and selected_color still "match". 0xff0000 & 0xffffff == 0xff0000. last_color = 0xffffff.
Now I can select any color rocket, because any of the colors will "match" with the white rocket. I'll select a blue rocket. 0xffffff & 0x0000ff == 0x0000ff. last_color = 0x0000ff.
Now if I try to select a red rocket, it no longer "matches". 0x0000ff & 0xff0000 == 0.
However, I can still select a blue rocket. 0x0000ff & 0x0000ff == 0x0000ff. last_color = 0x0000ff.

5. ## Got it

Thanks for the help, below is the "rough draft". Gonna break this stuff down into it's own method for later use. Thanks for the help. May not be pretty, but it works.

Code:
```private void CheckSelectFirework(Point point)
{
int SelectedColor = 0;

// Go through each firework
foreach(Firework firework in Fireworks)
{
// Did the user click on a firework?
if(PointOverRect(point.X,
point.Y,
firework.X,
firework.Y,
FireworkTypes[firework.Angle][firework.Colour].Width,
FireworkTypes[firework.Angle][firework.Colour].Height))
{
if(firework.Colour == Color.Red)
{
SelectedColor = RED;
}
else if(firework.Colour == Color.Green)
{
SelectedColor = GREEN;
}
else if(firework.Colour == Color.Blue)
{
SelectedColor = BLUE;
}
else if(firework.IsWhite)
{
SelectedColor = WHITE;
}

foreach(Firework firework2 in Fireworks)
{
if((LastColorSelected & SelectedColor) == 0)
{
firework2.IsSelected = false;
}
}

firework.IsSelected = true;

if(firework.Colour == Color.Red)
{
LastColorSelected = RED;
}
else if(firework.Colour == Color.Green)
{
LastColorSelected = GREEN;
}
else if(firework.Colour == Color.Blue)
{
LastColorSelected = BLUE;
}
else if(firework.IsWhite)
{
LastColorSelected = WHITE;
}
}
}
}```

6. Originally Posted by pianorain
I would keep track of the color of the last firework selected. Note the hexadecimal for each color of the rockets: red (0xff0000), green (0x00ff00), blue (0x0000ff), and white (0xffffff). If you require that bitwise-and of the color of the last firework selected and the next selected rocket be non-zero, you should achieve the effect you're looking for.

For example, I first select a red rocket. My last_color = 0xff0000.
If I try to select a blue rocket, I see that I can't because my last_color and selected_color don't "match". 0xff0000 & 0x0000ff == 0.
If I select another red rocket, last_color and selected_color obviously "match". 0xff0000 & 0xff0000 == 0xff0000. last_color = 0xff0000.
If I select a white rocket, last_color and selected_color still "match". 0xff0000 & 0xffffff == 0xff0000. last_color = 0xffffff.
Now I can select any color rocket, because any of the colors will "match" with the white rocket. I'll select a blue rocket. 0xffffff & 0x0000ff == 0x0000ff. last_color = 0x0000ff.
Now if I try to select a red rocket, it no longer "matches". 0x0000ff & 0xff0000 == 0.
However, I can still select a blue rocket. 0x0000ff & 0x0000ff == 0x0000ff. last_color = 0x0000ff.

I do have to add that this was a quasi-genius solution. I haven't used hex since the days of the Color Computer, so this idea would never have crossed my mind. It took me a little bit to figure out how to do it. I ended up setting static integer variables that are equal to the hex values for their respective color. Is there a better way to do this in C#? I am only familiar with the System.Drawing.Color class.

Thanks again!

7. ## Two Separate functions

I have settled on two separate methods:

Code:
```private void CheckSelectFirework(Point point)
{
foreach(Firework firework in Fireworks)
{
int SelectedColor = SetSelectedColor(firework);

if(PointOverRect(point.X,
point.Y,
firework.X,
firework.Y,
FireworkTypes[firework.Angle][firework.Colour].Width,
FireworkTypes[firework.Angle][firework.Colour].Height))
{
foreach(Firework firework2 in Fireworks)
{
if((LastColorSelected & SelectedColor) == 0)
{
firework2.IsSelected = false;
}
}

firework.IsSelected = true;
LastColorSelected = SelectedColor;
}
}
}```
Code:
```private static int SetSelectedColor(Firework firework)
{
int selectedColor = 0;

if(firework.Colour == Color.Red)
{
selectedColor = RED;
}
else if(firework.Colour == Color.Green)
{
selectedColor = GREEN;
}
else if(firework.Colour == Color.Blue)
{
selectedColor = BLUE;
}
else if(firework.IsWhite)
{
selectedColor = WHITE;
}

return selectedColor;
}```

8. Originally Posted by Phyxashun
Is there a better way to do this in C#?
You can call Color.ToArgb() to get the int value you want for the bitwise-and.

9. Originally Posted by pianorain
You can call Color.ToArgb() to get the int value you want for the bitwise-and.
Is this a technique?:

Code:
```private void CheckSelectFirework(Point point)
{
foreach(Firework firework in Fireworks)
{
if(PointOverRect(point.X,
point.Y,
firework.X,
firework.Y,
FireworkTypes[firework.Angle][firework.Colour].Width,
FireworkTypes[firework.Angle][firework.Colour].Height))
{
foreach(Firework firework2 in Fireworks)
{
if((LastColorSelected &
Convert.ToInt32(ColorTranslator.ToWin32(firework.Colour))) == 0)
{
firework2.IsSelected = false;
}
}

firework.IsSelected = true;
LastColorSelected =
Convert.ToInt32(ColorTranslator.ToWin32(firework.Colour));
}
}
}```

10. Originally Posted by Phyxashun
I have settled on two separate methods:

Code:
```private void CheckSelectFirework(Point point)
{
foreach(Firework firework in Fireworks)
{
int SelectedColor = SetSelectedColor(firework);

if(PointOverRect(point.X,
point.Y,
firework.X,
firework.Y,
FireworkTypes[firework.Angle][firework.Colour].Width,
FireworkTypes[firework.Angle][firework.Colour].Height))
{
foreach(Firework firework2 in Fireworks)
{
if((LastColorSelected & SelectedColor) == 0)
{
firework2.IsSelected = false;
}
}

firework.IsSelected = true;
LastColorSelected = SelectedColor;
}
}
}```
Code:
```private static int SetSelectedColor(Firework firework)
{
int selectedColor = 0;

if(firework.Colour == Color.Red)
{
selectedColor = RED;
}
else if(firework.Colour == Color.Green)
{
selectedColor = GREEN;
}
else if(firework.Colour == Color.Blue)
{
selectedColor = BLUE;
}
else if(firework.IsWhite)
{
selectedColor = WHITE;
}

return selectedColor;
}```
Have a look at using switch statements as well. They are often the better choice compared to multiple if-else statements.

11. darren78,

Those two methods were replaced by the single method in my last post, precluding the use of if-then or switch.

Thanks.

12. Originally Posted by Phyxashun
Is this a technique?:
You can do it that way, but if you're using System.Drawing.Color, I'm not sure why you wouldn't do this:
Code:
```private void CheckSelectFirework(Point point)
{
foreach(Firework firework in Fireworks)
{
if(PointOverRect(point.X,
point.Y,
firework.X,
firework.Y,
FireworkTypes[firework.Angle][firework.Colour].Width,
FireworkTypes[firework.Angle][firework.Colour].Height))
{
foreach(Firework firework2 in Fireworks)
{
if((LastColorSelected & firework.Colour.ToArgb()) == 0)
{
firework2.IsSelected = false;
}
}

firework.IsSelected = true;
LastColorSelected = firework.Colour.ToArgb();
}
}
}```

13. Pianorain,

For some reason, doing it the way you suggest, allows me to select any color.

14. There's probably a value in the alpha channel. If you mask with 0xffffff, it'll strip the alpha channel out.

15. Originally Posted by pianorain
There's probably a value in the alpha channel. If you mask with 0xffffff, it'll strip the alpha channel out.
Thanks, doing it this way works and is somewhat easier to read than the way I had it. I'll just add the static variable for 0xffffff back into the code.

Code:
```private void CheckSelectFirework(Point point)
{
foreach(Firework firework in Fireworks)
{
if(PointOverRect(point.X,
point.Y,
firework.X,
firework.Y,
FireworkTypes[firework.Angle][firework.Colour].Width,
FireworkTypes[firework.Angle][firework.Colour].Height))
{
foreach(Firework firework2 in Fireworks)
{
if((LastColorSelected & (firework.Colour.ToArgb() & 0xffffff)) == 0)
{
firework2.IsSelected = false;
}
}

firework.IsSelected = true;
LastColorSelected = (firework.Colour.ToArgb() & 0xffffff);
}
}
}```