Question

Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...

Write a C# program that allows one user to play Rock-Paper-Scissors with computer.

The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same.

The application must be able to record the score for number of wins for both players. Also, when you change your move, the application must clear out the current win/lose message and the computer's move.

The following codes will create one number randomly from 1, 2 or 3 and assign it to the variable num.

int num = 0;
Random rnd = new Random();
num = rnd.Next(1, 4);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System;

namespace Rextester

{

    public class Program

   {

        public int playGame(int playerChoice, int computerChoice)

        {

            int ans = -2;

            switch(playerChoice)

            {

                // if player choice is scissor

                case 0 :

                                // if computer choose Rock

                                if(computerChoice == 1)

                                    ans = 0;

                                // if computer choose Paper

                                else if(computerChoice == 2)

                                    ans = 1;

                                // if computer choose Scissor

                                else

                                    ans = -1;

                                break;

                // if player choice is Rock

                case 1 :

                                // if computer choose Paper

                                if(computerChoice == 2)

                                    ans = 0;

                                // if computer choose Scissor

                                else if(computerChoice == 0)

                                    ans = 1;

                                // if computer choose Rock

                                else

                                    ans = -1;

                                break;

                // if player choice is Paper

                case 2 :

                                // if computer choose Rock

                                if(computerChoice == 1)

                                    ans = 1;

                                // if computer choose Scissor

                                else if(computerChoice == 0)

                                    ans = 0;

                                // if computer choose Paper

                                else

                                    ans = -1;

                                break;

                //default :   // for invalid choice

                //            ans = -2;

            }

            return ans;

        }

       

        public static void Main(string[] args)

        {  

            Program ob = new Program();

       

            int i;

            // create a Rando object

            Random rand = new Random();

            int playerPoints = 0, computerPoints = 0;

            Console.WriteLine("Welcome to the rock paper scissors game!\n\n");

                // generate random number in range 0 to 2

                int computerChoice = rand.Next(0, 3);

                int playerChoice;

                // infinite loop

                while(true)

                {

                    Console.WriteLine("Enter either \"rock\", \"paper\", or \"scissors\" to compete\n");

                    String pc = Console.ReadLine();

                    if(String.Compare(pc, "scissors") == 0)

                    {

                        playerChoice = 0;

                        break;

                    }

                    else if(String.Compare(pc, "rock") == 0)

                    {

                        playerChoice = 1;

                        break;

                    }

                    else if(String.Compare(pc, "paper") == 0)

                    {

                        playerChoice = 2;

                        break;

                    }

                    else

                        Console.WriteLine("Error! Please enter a valid choice.\n\n");

                }

                Console.WriteLine("The computer picked ");

                if(computerChoice == 0)

                    Console.WriteLine("scissor\n");

                else if(computerChoice == 1)

                    Console.WriteLine("rock\n");

                else

                    Console.WriteLine("paper\n");

                int result = ob.playGame(playerChoice, computerChoice);

                if(result == 1)

                {

                    Console.WriteLine("You Won!\n");

                    playerPoints++;

                }

                else if(result == 0)

                {

                    Console.WriteLine("You Loose!\n");

                    computerPoints++;

                }

                else if(result == -1)

                    Console.WriteLine("It's a draw\n");

                else

                    Console.WriteLine("Error!!\n");

        }

    }

}


Sample Output

Welcome to the rock paper scissors game! Enter either rock, paper, or scissors to compete rock The computer picked rock Its a draw

Add a comment
Know the answer?
Add Answer to:
Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user...

    In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user to pick rock, paper, or scissors. Then, you’ll have the computer randomly choose one of the options. After that, print out the winner! You should keep playing the game until the user hits enter. Note: You’ll need to implement a method called String getWinner(String user, String computer). Luckily, you just wrote that in an earlier program! Here is a sample run of the program....

  • In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

    In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT