Click to See Complete Forum and Search --> : Logical Problem in Picturebox
shah123
May 31st, 2007, 11:41 AM
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.
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();
}
if (CompareImages(box4.Image, box5.Image, box6.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();
}
wildfrog
May 31st, 2007, 11:51 AM
Please explain the logic here:
public void DisplayWinner()
{
if (player == 2)
player = 1;
else
player = 2;
MessageBox.Show("Player" + player + "wins" );
reset();
displayAgain();
}
In 'my world' a function named DisplayWinner should do exactly that - Display the winner. But your DisplayWinner has some state logics added? Why is that? Why are you modifying the player variable?
- petter
shah123
May 31st, 2007, 12:05 PM
yes you are right. My mistake. I removed that logic in DisplayWinner()
Now i put the breakpoint in SwitchPlayers and it doesnt go further from "if condition" and player2 never comes in action.
private void switchPlayers()
{
if(player == 1 ) // It stops here
player=2;
What can be the reason?
wildfrog
May 31st, 2007, 12:22 PM
private void switchPlayers()
{
if(player == 1 ) // It stops here
player=2;
What can be the reason?
It could be 'anything'. For instance your person could be a property with an endless loop or a blocking call.
You should post your complete code, or a smaller compilable piece that reproduces the problem.
- petter
shah123
May 31st, 2007, 12:30 PM
Here is my part of code.. Thanks in advance. Please help
private void switchPlayers()
{
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}
checkAll();
}
private void box1_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box1.SizeMode = PictureBoxSizeMode.StretchImage;
box1.Image = pics[player - 1].Image;
box1.Enabled = false;
switchPlayers();
}
}
private void box2_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box2.SizeMode = PictureBoxSizeMode.StretchImage;
box2.Image = pics[player-1].Image;
box2.Enabled = false;
switchPlayers();
}
}
private void box3_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box3.SizeMode = PictureBoxSizeMode.StretchImage;
box3.Image = pics[player - 1].Image;
box3.Enabled = false;
switchPlayers();
}
}
private void box4_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box4.SizeMode = PictureBoxSizeMode.StretchImage;
box4.Image = pics[player - 1].Image;
box4.Enabled = false;
switchPlayers();
}
}
private void box5_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box5.SizeMode = PictureBoxSizeMode.StretchImage;
box5.Image = pics[player - 1].Image;
box5.Enabled = false;
switchPlayers();
}
}
private void box6_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box6.SizeMode = PictureBoxSizeMode.StretchImage;
box6.Image = pics[player - 1].Image;
box6.Enabled = false;
switchPlayers();
}
}
private void box7_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box7.SizeMode = PictureBoxSizeMode.StretchImage;
box7.Image = pics[player - 1].Image;
box7.Enabled = false;
switchPlayers();
}
}
private void box8_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box8.SizeMode = PictureBoxSizeMode.StretchImage;
box8.Image = pics[player - 1].Image;
box8.Enabled = false;
switchPlayers();
}
}
private void box9_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box9.SizeMode = PictureBoxSizeMode.StretchImage;
box9.Image = pics[player - 1].Image;
box9.Enabled = false;
switchPlayers();
}
}
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();
}
}
public void DisplayWinner()
{
// if (player == 2) player = 1;
//else player = 2;
MessageBox.Show("Player" + player + "wins" );
reset();
displayAgain();
}
shah123
May 31st, 2007, 12:33 PM
oK IT WORKED JUST NEEDED FOLLOWING in compareImages() function
if ((image1 == null) || (image2 == null) || (image3 == null))
{
return false;
}
Talikag
May 31st, 2007, 12:38 PM
Edit: Oh ok lol :P
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.