Link to home
Create AccountLog in
Avatar of shsh_shah
shsh_shah

asked on

Draw Functionality Halts in TicTac Toe

In TicTacToe When the draw occurs form became halts and stop down

Below is the code that all related to draw functionality

public void Draw()
{
lblDraw.Visible = true;
lblDraw.Enabled = true;
sta.compdraws++;
}

public void checkAll()
{

else if (box1.Image != null && box2.Image != null && box3.Image != null && box4.Image != null && box5.Image != null
&& box6.Image != null && box7.Image != null && box8.Image != null && box9.Image != null)
{
// DRAW occurs
Draw();

}
private void resetBtn_Click(object sender, EventArgs e)
{
reset();

}

public void reset()
{
lblDraw.Visible = false;
lblDraw.Enabled = false;
box1.Image = null;
box2.Image = null;
box3.Image = null;
box4.Image = null;
box5.Image = null;
box6.Image = null;
box7.Image = null;
box8.Image = null;
box9.Image = null;
displayAgain();
}

public void displayAgain()
{
box1.Enabled = true;
box2.Enabled = true;
box3.Enabled = true;
box4.Enabled = true;
box5.Enabled = true;
box6.Enabled = true;
box7.Enabled = true;
box8.Enabled = true;
box9.Enabled = true;
}

It stops when the game is done few times i mean when their is some winning occurs then it halts at when the draw label appears. And reset button is no more clickable.

There is no exception occurs at any point.
Avatar of Mikal613
Mikal613
Flag of United States of America image

public void checkAll()
{
// Where is the originating If statement?
else if (box1.Image != null && box2.Image != null && box3.Image != null && box4.Image != null && box5.Image != null
&& box6.Image != null && box7.Image != null && box8.Image != null && box9.Image != null)
{
Avatar of shsh_shah
shsh_shah

ASKER

there are many if statements to check to see who is winner thats why i didnt included anyway here is the following checkall() function look like
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();

            }
            else if (box1.Image != null && box2.Image != null && box3.Image != null && box4.Image != null && box5.Image != null
            && box6.Image != null && box7.Image != null && box8.Image != null && box9.Image != null)
            {
                Draw();
               
            }

        }

I have full TicTacToe game working apart from Draw so would be hard now to look at the program u asked me to look Mikal.

I have done on network,2 players and with computer but the draw functionality is some how not working not sure why?

Please help by looking at my code.
what variable is this?

sta.compdraws++;
This variable is from another class even if i remove this sta.compdraws still it halts. Remaining whole functionality works. And one thing i found which is wierd which is when ist time draw occurs i click reset button and it reset the pictureboxes when draw happend for 2nd time it halts then and no exception is thrown as i added few try catch as well.

But i would tell you abt this variable

Statistics.cs

public partial class Statistics : Form
    {
        public int userwin;
        public int computerwins;
        public int compdraws;
        public int comptotal;
        public int usertotal;

       
        public Statistics()
        {
            InitializeComponent();
        }

        private void resetButton_Click(object sender, EventArgs e)
        {
            compdraws = 0;
            comptotal = 0;
            userwin = 0;
            computerwins = 0;
            usertotal = 0;
            Map();    
        }
public void Map()
        {
            this.computerWinsLabel.Text = this.computerwins.ToString();
            this.userWinsLabel.Text = this.userwin.ToString();
            this.vsComputerDrawsLabel.Text = this.compdraws.ToString();
            this.computerTotalScoreLabel.Text = this.comptotal.ToString();
            this.userTotalScoreLabel.Text = this.usertotal.ToString();
            this.Refresh();
        }

tictactoe.cs
public void checkAll()
        {
            try
            {
                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();

                }
                else if (box1.Image != null && box2.Image != null && box3.Image != null && box4.Image != null && box5.Image != null
                && box6.Image != null && box7.Image != null && box8.Image != null && box9.Image != null)
                {
                    Draw();

                }
            }
            catch (Exception e3)
            {
                MessageBox.Show(e3.ToString());
            }

        }

public void Draw()
        {
            try
            {

                lblDraw.Visible = true;
                lblDraw.Enabled = true;
                //sta.compdraws++;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
 private void resetBtn_Click(object sender, EventArgs e)
        {
            reset();
           // displayAgain();
        }
 public void displayAgain()
        {
            box1.Enabled = true;
            box2.Enabled = true;
            box3.Enabled = true;
            box4.Enabled = true;
            box5.Enabled = true;
            box6.Enabled = true;
            box7.Enabled = true;
            box8.Enabled = true;
            box9.Enabled = true;
        }

        private void switchPlayers()
        {
            checkAll();

            if (player == 1)
            {
                player = 2;
            }
            else
            {
                player = 1;
            }
        }
public void reset()
        {
            try
            {
                lblDraw.Visible = false;
                lblDraw.Enabled = false;
                box1.Image = null;
                box2.Image = null;
                box3.Image = null;
                box4.Image = null;
                box5.Image = null;
                box6.Image = null;
                box7.Image = null;
                box8.Image = null;
                box9.Image = null;
                displayAgain();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString());
            }
        }


Try adding a lblDraw.Refresh();

public void Draw()
{
lblDraw.Visible = true;
lblDraw.Enabled = true;
lblDraw.Refresh();
sta.compdraws++;
}
It doesnt work.
How about enabling them first .
lblDraw.Enabled = true;
lblDraw.Visible = true;
lblDraw.Refresh();
No its not working. Mikal i have found when the draw function is se is finished it goes in SwitchPlayer function I am not sure why?

Have you seen my code above as a whole? Did you found any issue you think?
there must be an event that is triggering.

Is there one triggering when you click the Image?
i dont think so can i send here my whole code. I can remove some if conditions which i made for winning or checking different moves.
You are not using any events?
I am using Picturebox_Click events

        public int player = 1;
        static Random rnd = new Random();
        public PictureBox pressedPicBox;
        Statistics sta = new Statistics();
       


        public void DisplayWinner()
        {
            if (player == 1)
            {
                // add a bunch of stats to the list
                sta.userwin++;
                sta.usertotal++;
                MessageBox.Show("Player 1 win");
                reset1();
            }
            else if(player==2)
            {
                MessageBox.Show("Player 2 win");
                sta.computerwins++;
                sta.comptotal++;
                reset1();
            }
            sta.Map();
           
        }

       
        public void reset1()
        {
            try
            {
                lblDraw.Visible = false;
                lblDraw.Enabled = false;
                box1.Image = null;
                box2.Image = null;
                box3.Image = null;
                box4.Image = null;
                box5.Image = null;
                box6.Image = null;
                box7.Image = null;
                box8.Image = null;
                box9.Image = null;
                displayAgain();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString());
            }
        }

        public void displayAgain()
        {
            box1.Enabled = true;
            box2.Enabled = true;
            box3.Enabled = true;
            box4.Enabled = true;
            box5.Enabled = true;
            box6.Enabled = true;
            box7.Enabled = true;
            box8.Enabled = true;
            box9.Enabled = true;
        }

        private void switchPlayers()
        {
            checkAll();

            if (player == 1)
            {
                player = 2;
            }
            else
            {
                player = 1;
            }
        }

        private void TicTacToe_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile("C:\\Documents and Settings\\hahmed\\My Documents\\family pics\\Atif.JPG");
            pictureBox2.Image = Image.FromFile("c:\\WINDOWS\\system32\\setup.bmp");
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void resetBtn_Click(object sender, EventArgs e)
        {
            reset1();
        }

        public void Draw()
        {
            try
            {
                lblDraw.Enabled = true;
                lblDraw.Visible = true;
               
                lblDraw.Refresh();
                sta.compdraws++;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        // Random Functionality
        public string comPlaceRand()
        {
            const string result = "123456789";
            return result[rnd.Next(result.Length)].ToString();
        }

        private void box_Click(object sender, EventArgs e)
        {
            try
            {
                PictureBox pressedPicBox = (PictureBox)sender;
                pressedPicBox.SizeMode = PictureBoxSizeMode.StretchImage;
                pressedPicBox.Image = pictureBox1.Image;
                pressedPicBox.Enabled = false;
                switchPlayers();
                // Alot of if conditions .. skipped
                else
                {
                    int c = 1;

                    while (c > 0)
                    {
                        PictureBox pb = (PictureBox)this.Controls["box" + comPlaceRand()];
                        if (pb.Image == null)
                        {
                            pb.SizeMode = PictureBoxSizeMode.StretchImage;
                            pb.Image = pictureBox2.Image;
                            pb.Enabled = false;
                            c = 0;
                        }
                    }

                }

                switchPlayers();
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.ToString());
            }
}


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

        }

        public void checkAll()
        {
            try
            {
                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();

                }
                else if (box1.Image != null && box2.Image != null && box3.Image != null && box4.Image != null && box5.Image != null
                && box6.Image != null && box7.Image != null && box8.Image != null && box9.Image != null)
                {
               //     Draw();
                    MessageBox.Show("DRAW");
                    sta.compdraws++;
                    sta.Map();
                    reset1();
               
                }
            }
            catch (Exception e3)
            {
                MessageBox.Show(e3.ToString());
            }

        }


       
    }
}
bool ignoreEvent  = false;

public void Draw()
        {
             ignoreEvent   = true;
            try
            {
                lblDraw.Enabled = true;
                lblDraw.Visible = true;
               
                lblDraw.Refresh();
                sta.compdraws++;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                 ignoreEvent   = false;
             }
        }


private void box_Click(object sender, EventArgs e)
        {
           if(ignoreEvent == false)
          {
            try
            {
                PictureBox pressedPicBox = (PictureBox)sender;
                pressedPicBox.SizeMode = PictureBoxSizeMode.StretchImage;
                pressedPicBox.Image = pictureBox1.Image;
                pressedPicBox.Enabled = false;
                switchPlayers();
                // Alot of if conditions .. skipped
                else
                {
                    int c = 1;

                    while (c > 0)
                    {
                        PictureBox pb = (PictureBox)this.Controls["box" + comPlaceRand()];
                        if (pb.Image == null)
                        {
                            pb.SizeMode = PictureBoxSizeMode.StretchImage;
                            pb.Image = pictureBox2.Image;
                            pb.Enabled = false;
                            c = 0;
                        }
                    }

                }

                switchPlayers();
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.ToString());
            }
      }
}
Still the same. It halts
im so stuck . Can you step through it?
What you mean step through it?
I did and as i said after draw it goes in switchplayer() and then in random functionality and goes through
following code continously and doesnt come out.

  while (c > 0)
                    {
                        PictureBox pb = (PictureBox)this.Controls["box" + comPlaceRand()];
                        if (pb.Image == null)
                        {
                            pb.SizeMode = PictureBoxSizeMode.StretchImage;
                            pb.Image = pictureBox2.Image;
                            pb.Enabled = false;
                            c = 0;
                        }
If there all checked then it will never come out. Your only setting it to 0 if one of the boxes are null.
so what could be the solution?
only execute the loop if there  not all checked.
I am now confused. I think I have workaround which is not very good but atleast i just want to make it working thats all which will be

else if (box1.Image != null && box2.Image != null && box3.Image != null && box4.Image != null && box5.Image != null
                && box6.Image != null && box7.Image != null && box8.Image != null && box9.Image != null)
                {

                    MessageBox.Show("DRAW");
                    sta.compdraws++;
                    sta.Map();
                    reset1();
               
                }

Adding reset option here and without clicking any reset button it will reset itself.
Your loop is looking for the unclicked box. If there are non then it will be an infinite loop.
when the draw happens all boxes are clicked that what was meant to do.

 "if there are none then it will be an infinite loop"??
ASKER CERTIFIED SOLUTION
Avatar of Mikal613
Mikal613
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Mikal Thanks anyway as i cant do any more changes i did the work around and it works. But thanks a million anyway.
No problem.