Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
This is my code and I'm wanting to make it where I can keep track of the number of wins for the user and the computer, I will attach an image of what the program looks like
http://imgur.com/SbxVB5T[^]

I want to keep track of the score with the labels that have that 3D effect, any help is appreciated

C#
const int ROCK = 1;
const int PAPER = 2;
const int SCISSORS = 3;
int userWinsCount;

private void rockButton_Click(object sender, EventArgs e)
{
    int userChoice = ROCK;
    userPictureBox.Image = Properties.Resources.Rock;

    Random randomNumberGenerator = new Random();
    int computerChoice = randomNumberGenerator.Next(1, 4);
    switch (computerChoice)
    {
        case ROCK:
            computerPictureBox.Image = Properties.Resources.Rock;
            break;

        case PAPER:
            computerPictureBox.Image = Properties.Resources.Paper;
            break;

        case SCISSORS:
            computerPictureBox.Image = Properties.Resources.Scissors;
            break;
    }


    if (userChoice == 1 && computerChoice == 2)
        MessageBox.Show("You lose!");

    else if (userChoice == 1 && computerChoice == 1)
    {
        MessageBox.Show("Tie!");
    }
    else if (userChoice == 1 && computerChoice == 3)
    {
        MessageBox.Show("You win!");
    }
}

private void paperButton_Click(object sender, EventArgs e)
{
    int userChoice = PAPER;
    userPictureBox.Image = Properties.Resources.Paper;

    Random randomNumberGenerator = new Random();
    int computerChoice = randomNumberGenerator.Next(1, 4);
    switch (computerChoice)
    {
        case ROCK:
            computerPictureBox.Image = Properties.Resources.Rock;
            break;

        case PAPER:
            computerPictureBox.Image = Properties.Resources.Paper;
            break;

        case SCISSORS:
            computerPictureBox.Image = Properties.Resources.Scissors;
            break;

    }
    if (userChoice == 2 && computerChoice == 1)
        MessageBox.Show("You win!");

    else if (userChoice == 2 && computerChoice == 3)
    {
        MessageBox.Show("You lose!");
    }
    else if (userChoice == 2 && computerChoice == 2)
    {
        MessageBox.Show("Tie!");
    }
}

private void scissorsButton_Click(object sender, EventArgs e)
{
    int userChoice = SCISSORS;
    userPictureBox.Image = Properties.Resources.Scissors;

    Random randomNumberGenerator = new Random();
    int computerChoice = randomNumberGenerator.Next(1, 4);
    switch (computerChoice)
    {
        case ROCK:
            computerPictureBox.Image = Properties.Resources.Rock;
            break;

        case PAPER:
            computerPictureBox.Image = Properties.Resources.Paper;
            break;

        case SCISSORS:
            computerPictureBox.Image = Properties.Resources.Scissors;
            break;

    }
    if (userChoice == 3 && computerChoice == 1)
        MessageBox.Show("You lose!");

    else if (userChoice == 3 && computerChoice == 2)
    {
        MessageBox.Show("You win!");
    }
    else if (userChoice == 3 && computerChoice == 3)
    {
        MessageBox.Show("Tie!");
    }
Posted

Just a basic idea:
Create one variable for score, and have one label for it to keep track of the score.
Something like,
C#
int score = 0;
if (userChoice == 3 && computerChoice == 1)
{
    MessageBox.Show("You lose!");
    score--;
}
else if (userChoice == 3 && computerChoice == 2)
{
    MessageBox.Show("You win!");
    score++;
}
else if (userChoice == 3 && computerChoice == 3)
{
    MessageBox.Show("Tie!");
}

scoreLabel.Text = score + "";

And for the 3d effect, you can google about it. You'll get the suggestion about animations for windforms, you can use it :)

-KR
 
Share this answer
 
Comments
Member 12099053 30-Oct-15 0:54am    
And for the computer score to work I would need another int variable correct?
0. suggestion: move your declaration of 'randomNumberGenerator outside the 'rockButton_Click method. If you leave it in the method, it's going to generate the same seqence every time the method is called until the underlying default "seed" changed. Look up 'Random in the docs, and use a 'seed value that's based on some value that changes, like:
C#
private Random randomNumberGenerator = new Random(System.Environment.TickCount);
1. Create separate top-level variables for UserScore and ComputerScore:

private int UserScore { set; get; }

private int ComputerScore { set; get; }

I used properties here because I expect in the future you may wish to save (serialize) the game. and ...

2. Another use for properties is to use any change in the property value to update UI elements; for example:
C#
// assuming two Labels
public Label lblUserScore;
public Label lblComputerScore;

private int _userscore;

public int UserScore
{
    set 
    {
        if(value != _userscore)
        {
            _userscore = value;
            lblUserScore.Text = _userscore.ToString();
        }
    }
    get { return _userscore; }   
}


private int _computerscore;

public int ComputerScore
{
    set 
    {
        if(value != _computerscore)
        {
            _computerscore= value;
            lblComputerScore.Text = _computerscore.ToString();
        }
    }
    get { return _computerscore; }   
}


2. for your 3d-effect buttons you can use a button with a BackGround Image, or a Panel with a background Image. These CodeProject articles will show you a variety of ways to achieve "fancy" buttons: [^]
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900