Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rock paper scissors game with a window

Tags:

c#

wpf

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);
        }
like image 978
Jaruno Avatar asked Dec 17 '22 19:12

Jaruno


1 Answers

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")

like image 164
VB_Dojnaz Avatar answered Dec 28 '22 23:12

VB_Dojnaz