Jaruno
Jaruno

Reputation: 51

Rock paper scissors game with a window

I'm working on a Rock, paper, scissors game for a school assignment, But when I try to run this script the game doesn't work properly. When I click Rock for example the computer only picks the same or picks paper. But the player never wins. I don't know how to fix this, Ive been trying for hours. This is my code:

public partial class MainWindow : Window
{
    string Computer;
    string[] computer = { "Rock", "Paper", "Scissors" };
    Random random = new Random();
    int RandomType;
    string PlayerPicks;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void StoneButton_Click(object sender, RoutedEventArgs e)
    {
        PlayerPicks = "Rock";
        RandomType = random.Next(0, 2);
        Computer = computer[RandomType];
        Game();
    }

    private void PaperButton_Click(object sender, RoutedEventArgs e)
    {
        PlayerPicks = "Paper";
        RandomType = random.Next(0, 2);
        Computer = computer[RandomType];
        Game();
    }

    private void ScissorsButton_Click(object sender, RoutedEventArgs e)
    {
        PlayerPicks = "Scissors";
        RandomType = random.Next(0, 2);
        Computer = computer[RandomType];
        Game();
    }

    void Game()
    {
        string message = "The winner is: ";
        string computerWins = "Computer!";
        string playerWins = "Player!";
        string draw = "N-Nobody?";


        if (PlayerPicks == "Rock" && Computer == "Paper") // Player: Rock, Computer: paper = computer wins
        {
            MessageBox.Show(message + computerWins);
        }
        else if (PlayerPicks == "Rock" && Computer == "Scissors") // Player: Rock, Computer: Scissors = Player wins
        {
            MessageBox.Show(message + playerWins);

        }
        else if (PlayerPicks == "Paper" && Computer == "Scissors") // Player: Paper, Computer: Scissors = Computer wins
        {
            MessageBox.Show(message + computerWins);

        }
        else if (PlayerPicks == "Paper" && Computer == "Rock") // Player: Paper, Computer: Rock = Player wins
        {
            MessageBox.Show(message + playerWins);

        }
        else if (PlayerPicks == "Scissors" && Computer == "Rock") // Player: Scissors, Computer: Rock = Computer wins
        {
            MessageBox.Show(message + computerWins);

        }
        else if (PlayerPicks == "Scissors" && Computer == "Paper") // Player: Scissors, Computer: Paper = Player wins
        {
            MessageBox.Show(message + playerWins);

        }
        if (PlayerPicks == "Scissors" && Computer == "Scissor")
        {
            MessageBox.Show(message + draw);

        }
        if (PlayerPicks == "Paper" && Computer == "Paper")
        {
            MessageBox.Show(message + draw);
        }
        if (PlayerPicks == "Rock" && Computer == "Rock")
        {
            MessageBox.Show(message + draw);
        }

Upvotes: 4

Views: 1550

Answers (4)

Flydog57
Flydog57

Reputation: 7111

Here's another approach. It has been greatly refactored from your code. First I added a private System.Random member field to the form class:

private Random _rand = new Random();

Then, I set up two enums, one for the "hand sign" (rock, paper, scissors) and the other for the result:

public enum HandSign
{
    Rock,           // rock beats scissors
    Paper,          // paper beats rock
    Scissors,       // scissors beats paper
}

public enum Result
{
    Draw,
    ComputerWins,
    PlayerWins
}

Then I create some helper functions, the first to determine the computer's next move:

private HandSign GetRandomPlay()
{
    var result = _rand.Next(0, Enum.GetValues(typeof(HandSign)).Length);
    return (HandSign) result;
}

and the next one, the rules engine to determine the winner:

private Result DetermineWinner(HandSign player, HandSign computer)
{
    if (player == computer)
    {
        return Result.Draw;
    }

    if (player == HandSign.Scissors && computer == HandSign.Rock)
    {
        return Result.ComputerWins;
    }

    if (player == HandSign.Rock && computer == HandSign.Scissors)
    {
        return Result.PlayerWins;
    }

    if (player > computer)
    {
        return Result.PlayerWins;
    }

    //finally, otherwise
    return Result.ComputerWins;
}

Finally, one to "play the game", showing the results of the play in a handful of labels on the form:

 private void PlayGame(HandSign playerPicks)
 {
     PlayerPicksLbl.Text = playerPicks.ToString();
     var computerPicks = GetRandomPlay();
     ComputerPicksLbl.Text = computerPicks.ToString();
     WinnerLbl.Text = DetermineWinner(playerPicks, computerPicks).ToString();
 }

At that point, my three button click handlers simply look like this (this is the Rock handler):

 private void RockBtn_Click(object sender, EventArgs e)
 {
     PlayGame(HandSign.Rock);
 }

One result of structuring things this way, is that to extend this to "Rocks, Paper, Scissors, Lizard, Spock", all you'd need to do is add two more entries to the HandSign enum, add a bit more logic to DetermineWinner and two more buttons/button handlers.

Also note that there's no way to spell "Scissors" wrong and still have this compile!

Note:
A Console app version of this can be found in my answer to this: rock, paper, scissors gone wrong

Upvotes: 3

VB_Dojnaz
VB_Dojnaz

Reputation: 269

Problem 1

RandomType = random.Next(0, 2) only generates a number between 0 and 1, you would need to do RandomType = random.Next(0, 3) This will generate a number between 0 and 2

Problem 2

if (PlayerPicks == "Scissors" && Computer == "Scissor") checks if computer picked Scissor instead of Scissors Just change this to if (PlayerPicks == "Scissors" && Computer == "Scissors")

Upvotes: 4

Peter Chikov
Peter Chikov

Reputation: 151

On Random.Next the upper bound is exclusive so you should do RandomType = random.Next(0, 3);

In addition in:

if (PlayerPicks == "Scissors" && Computer == "Scissor") you have "Scissor" instead of "Scissors"

Upvotes: 2

uwieuwe4
uwieuwe4

Reputation: 153

First, you need to use random.Next(0,3) (as Anu Viswan mentioned in the comment) Furthermore you have a typo at the third "if" from below where you typed Scissor instead of Scissors. I would suggest to use variables for those strings - then you don't have problems with typos.

Upvotes: 1

Related Questions