Search code examples
c#winformstic-tac-toe

How to check if player wins in tic-tac-toe game?


Hi, Im working on Visual Studio Winforms and I'm trying to build tic-tac-toe. I got stuck in the Check if player win method. I have tried to do that in activated. little example:

    private void StartForm_Activated(object sender, EventArgs e)
    {
        if ((_pcb1.Image == imagecircle) && (_pcb2.Image == imagecircle) && (_pcb3.Image == imagecircle))
        {
           MessageBox.Show("You Win!");

        }
        if ((_pcb4.Image == imagecircle) && (_pcb5.Image == imagecircle) && (_pcb6.Image == imagecircle))
        {
            MessageBox.Show("You Win!");
        }

        if ((_pcb7.Image == imagecircle) && (_pcb8.Image == imagecircle) && (_pcb9.Image == imagecircle))
        {
            MessageBox.Show("You Win!");
        }
    }

(I know that there are more situations to win). It's never get into the method, I trying to find a method that active always when the form is open. please help :)


Solution

  • You should check player's victory after each move of that player. I assume, that your players make their moves by clicking on "Make a Move" button or clicking on the game field. Also I assume, that you have Click event of your button or game field. So you should paste your check code in Click event of your button or game field.