Search code examples
c#tic-tac-toe

Public void int increment by 1 every loop (Beginner)


I'd basically like to know how I can increase Player.SetPoints(); by 1 every time it loops. I got this in my class public void SetPoints(int p) { points = p; }, just thought that would be worth mentioning. So far the only way I've gotten this to work is to use Player.SetPoints(+1); but that only puts 1 to the scoreboard and never increases again. Sorry If I'm not clear enough, I'm very new to programming. This is a sample of my code so you might be able to understand it a bit more, I'm making a tic tac toe game as an assignment for school.

if (Btn1.Text != "" && Btn2.Text != "" && Btn3.Text != "")
            {
                if (Btn1.Text == Btn2.Text && Btn1.Text == Btn3.Text)
                {
                    Btn1.BackColor = Color.Green;
                    Btn1.ForeColor = Color.White;
                    Btn2.BackColor = Color.Green;
                    Btn2.ForeColor = Color.White;
                    Btn3.BackColor = Color.Green;
                    Btn3.ForeColor = Color.White;
                    if (Btn1.Text == "X")
                    {
                        MessageBox.Show(lblpl1.Text + " Has Won!");
                        Player1.SetPoints(+0);
                        playerscore1.Text = Player1.GetPoints().ToString();
                    }
                    else
                    {
                        MessageBox.Show(lblpl2.Text + " Has Won!");
                        Player2.SetPoints(+0);
                        playerscore2.Text = Player2.GetPoints().ToString();
                    }

                    click1++;
                    click2++;
                    click3++;
                    click4++;
                    click5++;
                    click6++;
                    click7++;
                    click8++;
                    click9++;

                    restartbtn.Visible = true;
                }

            }

Solution

  • In order increase something in each button click you need to save the previous value in a variable. You are doing a similar think with clicks:

    click1++;
    

    Your method sets the points to a given value. If you give it 1 it sets it to 1. It looks like you don't want to set points to an individual value, you want to increase it each time, so instead of declaring a method like that you can change it to:

    public void IncreasePoints()
    {
        points++;
    }
    

    And simply call it without passing any value.

    Player1.IncreasePoints();