Quantcast
Channel: Logical Problem in Picturebox
Viewing all articles
Browse latest Browse all 3

Logical Problem in Picturebox

$
0
0
I have following piece of code but some how something is wrong in logic in CheckAll() function.

The logical problem in the below code is that when user click on any picturebox it shows message "player 1 is winner".

I debugged the code what it does that when the user click any picturebox it check on CompareImages() function and if user clicked e.g. picturebox1 it will skip Ist comparison in CheckAll() function and when it reaches second comparison it will go in displaywinner() function.

Code Snippet

public bool CompareImages(Image image1,Image image2,Image image3)
        {
            if(!Object.Equals(image1,image2))
            {
                return false;
            }
            if(!Object.Equals(image2,image3))
            {
                return false;
            }
            
            return true;
            
        }

        public void checkAll()
        {
            if (CompareImages(box1.Image, box2.Image, box3.Image))
            {
                DisplayWinner();
              
            }
            else if (CompareImages(box4.Image, box5.Image, box6.Image))
            {
                DisplayWinner();
                
            }
            else if (CompareImages(box7.Image, box8.Image, box9.Image))
            {
                DisplayWinner();
                
            }
            else if (CompareImages(box1.Image, box5.Image, box9.Image))
            {
                DisplayWinner();
                
            }
            else if (CompareImages(box3.Image, box5.Image, box7.Image))
            {
                DisplayWinner();
               
            }
            else if (CompareImages(box1.Image, box4.Image, box7.Image))
            {
                DisplayWinner();
               
            }
            else if (CompareImages(box2.Image, box5.Image, box8.Image))
            {
                DisplayWinner();
                
            }
            else if (CompareImages(box3.Image, box6.Image, box9.Image))
            {
                DisplayWinner();
                
            }

        }

        public void DisplayWinner()
        {
            if (player == 2) player = 1;
            else player = 2;
            MessageBox.Show("Player"  + player +  "wins" );
            reset();
            displayAgain();
        }
    }
private void switchPlayers()
        {
            if(player == 1 )
                player=2;
            else if(player==2)
                player=1;

            checkAll();
        }


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images