none
Paper, Scissors, Rock

    Question

  • I am trying to create a paper scissors rock game using windows application in visual c# 2005

    My game works fine the user selects a button either paper, scissors or rock and a random number generator is compared to it as the computers choice to give the outcome.

    What I need though is a tally on the form saying the players wins

    could anybody help??? I think im on the right track I need to create a loop somehow

     

    [Code]

    public partial class frmComputer : Form

    {

    Random RandomClass = new Random();

    public frmComputer()

    {

    InitializeComponent();

    }

    private void btnPaper_Click(object sender, EventArgs e)

    {

    int A = RandomClass.Next(4);

    if (A == 1)

    {

    this.lblWinner.Text = " I pick ROCK aswell we Draw";

    }

    else if (A == 2)

    {

    this.lblWinner.Text = "I pick SCISSORS, You win";

    int count = + +1;

    this.lblTally.Text = "Wins " + count;

    }

    else if (A == 3)

    {

    this.lblWinner.Text = "I pick PAPER, You lose";

    }

    lblWinner.Show();

    }

    private void btnScissors_Click(object sender, EventArgs e)

    {

    int B = RandomClass.Next(4);

    if (B == 1)

    {

    this.lblWinner.Text = " I pick ROCK, You win";

    int count = + +1;

    this.lblTally.Text = "Wins " + count;

    }

    else if (B == 2)

    {

    this.lblWinner.Text = "I pick SCISSORS, You lose";

    }

    else if (B == 3)

    {

    this.lblWinner.Text = "I pick PAPER aswell we Draw";

    }

    lblWinner.Show();

    }

    private void btnRock_Click(object sender, EventArgs e)

    {

    int C = RandomClass.Next(4);

    if (C == 1)

    {

    this.lblWinner.Text = "I pick ROCK, You lose";

    }

    else if (C == 2)

    {

    this.lblWinner.Text = "I pick SCISSORS aswell we Draw";

    }

    else if (C == 3)

    {

    this.lblWinner.Text = "I pick PAPER, You win";

    int count = + +1;

    this.lblTally.Text = "Wins " + count;

    }

    lblWinner.Show();

    }

    private void btnExit_Click(object sender, EventArgs e)

    {

    Application.Exit();

    }

    }

    }

    [/Code]

    Wednesday, September 26, 2007 9:50 PM

Answers

  • Just like RandomClass is a variable in the scope of your Form class, declare your count there as well.  When the user wins increment the winCount and then update this.lblTally.Text.

     

    The reason your count isn't working is because it is in the scope of the button Click handler so it can not share the count across the different buttons.

    public partial class frmComputer : Form

    {

    Random RandomClass = new Random();

    int winCount = 0;

     

    ///

     

     

    Thursday, September 27, 2007 4:55 AM

All replies

  • Just like RandomClass is a variable in the scope of your Form class, declare your count there as well.  When the user wins increment the winCount and then update this.lblTally.Text.

     

    The reason your count isn't working is because it is in the scope of the button Click handler so it can not share the count across the different buttons.

    public partial class frmComputer : Form

    {

    Random RandomClass = new Random();

    int winCount = 0;

     

    ///

     

     

    Thursday, September 27, 2007 4:55 AM
  • Thanks mate all working now

    Thursday, September 27, 2007 6:53 AM