lundi 26 octobre 2015

Tic Tac Toe turn issues C#

I'm in a basic programming class and right now we're suppose to code in a program that simulates playing tic tac toe. In my code I plan to let the person choose if they want to watch the simulated game or if they want to play, in which it'll then be a two person game I'm not ready to try to make a game to play against the computer.

I got stuck on the simulated part so moved onto the game play part. Player one goes, but the way my if statement is working it does the action for player one, but then does it for player 2 as well. The code it long, but each button is a set up of the same basic code so I'm just posting one button worth.

The "lblTest.Text = OneDown.ToString();" code was added so I could see the math its doing, each click is counting for both player 1 and 2, showing the end number as 8. How would I go about making it only run for player one or player two?

private void btn1_Click(object sender, EventArgs e)
{
    if (PlayerTurn == 1)
    {
        OneDown = OneDown + 5;
        OneAcross = OneAcross + 5;
        SlashOne = SlashOne + 5;
        btn1.Text = "X";
        btn1.Enabled = false;
        PlayerTurn = PlayerTurn - 1;
        lblTurn.Text = "O";
    }

    if (SlashOne == 15 || OneAcross == 15 || OneDown == 15)
    {
        MessageBox.Show("Player 1 Wins!");
    }
    else if (PlayerTurn == 0)
    {
        OneDown = OneDown + 3;
        OneAcross = OneAcross + 3;
        SlashOne = SlashOne + 3;
        btn1.Text = "O";
        btn1.Enabled = false;
        PlayerTurn = PlayerTurn + 1;
        lblTurn.Text = "X";
        lblTest.Text = OneDown.ToString();
    }

    if (SlashOne == 9 || OneAcross == 9 || OneDown == 9)
    {
        MessageBox.Show("Player 2 Wins!");
    }

Aucun commentaire:

Enregistrer un commentaire