Thread: Logic Problem

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    Exclamation 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. #2
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    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
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    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;
    				}
    			}
    		}
    	}
    }
    Last edited by Phyxashun; 10-30-2010 at 10:38 AM. Reason: Correct error.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    Lightbulb 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;
    			}
    		}
    	}
    }
    Last edited by Phyxashun; 10-30-2010 at 06:31 PM. Reason: Fixed spelling errors

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    Thumbs up

    Quote Originally Posted by pianorain View Post
    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. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    Lightbulb 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;
    }
    Last edited by Phyxashun; 10-30-2010 at 07:25 PM. Reason: Jargon fix

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Phyxashun View Post
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    Question

    Quote Originally Posted by pianorain View Post
    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. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Phyxashun View Post
    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. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    darren78,

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

    Thanks.

  12. #12
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Phyxashun View Post
    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();
    		}
    	}
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Pianorain,

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

  14. #14
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    There's probably a value in the alpha channel. If you mask with 0xffffff, it'll strip the alpha channel out.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by pianorain View Post
    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);
    		}
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

Tags for this Thread