I’m making a tic tac toe game but it just puts X

enter image description here I’m doing a tic tac toe game but it just puts X. I do it on the web form without using the master page

 bool turn = true;
        int turn_count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void bclick(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (turn)
            {
                b.Text = "X";
            }
            else
            {
                b.Text = "O";
            }
            turn = !turn;
            b.Enabled = false;
        }
    }
}  

Answer

Something like this. Store turn in Session. Note you will also have the same problem with turn_count. I don’t really understand where you were going with the boolean. If you are expecting two different players on two browsers to play this game then you need a lot more work to track game state and what players are active in what game instance.

protected void bclick(object sender, EventArgs e)
{
    string whosTurn = "X";

    if (!string.IsNullOrEmpty(Session["turn"] as string))
    {
        whosTurn = Session["turn"].ToString();
    }

    if(whosTurn == "X")
    {
        Session["turn"] = "O";
    }
    else
    {
        Session["turn"] = "X";
    }

    Button b = (Button)sender;
    
    b.Text = whosTurn;

    b.Enabled = false;
}

Leave a Reply

Your email address will not be published. Required fields are marked *