Calvin Weng
Calvin Weng

Reputation: 1

Issues with my rock, paper scissors game

I know this question has been posted before, I'm currently using some code that was given to me but after adding some things, I'm having issues with getting it to actually output what I want.

This is what's coming up on my console

What am I doing that’s not causing the program to proceed into the game?

using System;

class rockpsV2
{

public static void Main(string[]args)
{
   do
   {
       Console.WriteLine("Do you want to play rock,paper or scissors?");

       string userChoice = Console.ReadLine();

        Random r = new Random();
        int computerChoice = r.Next(4);

            if (computerChoice == 1)
            {
                if (userChoice == "rock")
                {
                    Console.WriteLine("The computer chose rock");
                    Console.WriteLine("It is a tie ");                     
                }
                else if (userChoice == "paper")
                {
                    Console.WriteLine("The computer chose paper");
                    Console.WriteLine("It is a tie ");

                }
                else if (userChoice == "scissors")
                {
                    Console.WriteLine("The computer chose scissors");
                    Console.WriteLine("It is a tie ");
                }
                else
                {
                    Console.WriteLine("You must choose rock,paper or scissors!");

                }

            }

            else if (computerChoice == 2)
            {
                if (userChoice == "rock")
                {
                    Console.WriteLine("The computer chose paper");
                    Console.WriteLine("Sorry you lose,paper beat rock");

                }
                else if (userChoice == "paper")
                {
                    Console.WriteLine("The computer chose scissors");
                    Console.WriteLine("Sorry you lose,scissors beat paper ");

                }
                else if (userChoice == "scissors")
                {
                    Console.WriteLine("The computer chose rock");
                    Console.WriteLine("Sorry you lose,rock beats scissors");                       
                }
                else
                {
                    Console.WriteLine("You must choose rock,paper or scissors!");        
                }
            }
            else if (computerChoice == 3)
            {
                if (userChoice == "rock")
                {
                    Console.WriteLine("The computer chose scissors");
                    Console.WriteLine("You win,rock beats scissors");

                }
                else if (userChoice == "paper")
                {
                    Console.WriteLine("The computer chose rock");
                    Console.WriteLine("You win,paper beats rock");

                }
                else if (userChoice == "scissors")
                {
                    Console.WriteLine("The computer chose paper");
                    Console.WriteLine("You win,scissors beat paper");

                }
                else
                {
                    Console.WriteLine("You must choose rock,paper or scissors!");

                }

            }

        } while(Console.ReadLine() == "yes");
    }
}

Upvotes: 0

Views: 340

Answers (2)

hpfs
hpfs

Reputation: 506

I would refactor the whole program this way

using System;

class rockpsV2
{

public static string[] choices = new string[]  { "rock", "paper", "scissor" };
public static int comparePlay(string player1, string player2) 
{
    if (player1 == player2)
        return  0;

    if (player1 == "rock" && player2 == "scissor" ||
        player1 == "paper" && player2 == "rock" ||
        player1 == "scissor" && player2 == "paper")
        return -1;

    return 1;
}


public static bool validChoice(string choice) 
{
    return choice == "rock" 
            || choice == "scissor"
            || choice == "paper";

}

public static void Main(string[] args)
{

    string userChoice = "";
   do
   {
       do {
        Console.WriteLine("Do you want to play rock,paper or scissors?");
        userChoice = Console.ReadLine();       

        if (!validChoice(userChoice)) {
            Console.WriteLine("You must choose rock,paper or scissors!");        
        }

       }
       while(!validChoice(userChoice));


        Random r = new Random();
        int computerChoiceRand = r.Next(100) % 3;
        string computerChoice = choices[computerChoiceRand];

        Console.WriteLine("The computer chose {0}", computerChoice);

        int whoWins = comparePlay(computerChoice, userChoice);

        if (whoWins < 0) {
            Console.WriteLine("Sorry you lose, {0} beats {1}", computerChoice, userChoice);
        }

        if (whoWins == 0) {
            Console.WriteLine("It is a tie");
        }

        if (whoWins > 0) {
            Console.WriteLine("You win,{0} beats {1}", userChoice, computerChoice);
        }

        Console.WriteLine("Want to play again?");
        } while(Console.ReadLine() == "yes");
    }
}

Upvotes: 0

Sjors Ottjes
Sjors Ottjes

Reputation: 1217

The question is: Console.WriteLine("Do you want to play rock,paper or scissors?"); (note the word 'or', you wrote it in your question as 'and') you answer that question with rock, paper or scissors, if you give a different answer, it doesn't play and goes to the while(Console.ReadLine() == "yes"); line, if you answer anything different than 'yes' here, it will leave the do..while loop and quit the program

Upvotes: 2

Related Questions