Question

In: Computer Science

Part (a) Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the...

Part (a) Write a number guessing game using System.Collections.Generic.Dictionary.

  • Generate 10 distinct random numbers in the range of 1 to 20. Each random number is associated with a prize money (from 1 to 10000). Use a Dictionary to store the mapping between the random number and the prize money.
  • Ask user for two distinct numbers, a and b, both from 1 to 20; if a and b are not distinct, or out of range, quit the program
  • Lookup the prize money from both numbers and print out the sum via "You earned XXXX in total!". If the table does not contain the number, assume the prize money is 0.

e.g. Suppose we generate the following random numbers and penalty money:

1

3

4

6

7

11

12

13

19

20

333

234

12

500

1569

9900

23

2

588

23

If a user guess 5 and 3, we output "You earned 234 in total!"

If a user guess 1 and 13, we output "You earned 335 in total!"

If a user guess 1 and 1, quit

I have done the part(a) and the code is below.

For the part(b),

If the user has earned absolutely 0 amount of prize money, tell user which number would have given them the highest prize money:

e.g. "You are unlucky! If you guessed X, you would have won the most money!"

Otherwise, if the prize money is non-zero, after telling user the prize money he/she earned, print out the list of numbers that would have caused the player to have 0 prize money.

e.g. "You are so lucky! You could have 0 money if you have guessed …."

using System;

using System.Collections.Generic;

class GuessingGame{

    public static void Main(){

        Dictionary<int, int> prizes = new Dictionary<int, int>();

        Random rndGen = new Random(0);

        int num, money;

        int i=0;

        while(i<10){

            num = rndGen.Next(1, 20);

            money = rndGen.Next(1, 10000);

            if(!prizes.ContainsKey(num)){

                prizes.Add(num, money);

                i++;

            }

        }

        Console.Write("Enter two numbers(space separated): ");

        string[] line = Console.ReadLine().Split(' ');

        int num1 = Int32.Parse(line[0]);

        int num2 = Int32.Parse(line[1]);

        if(num1==num2 || num1<1 || num1>20 || num2<0 || num2>20)

            Console.WriteLine("quit");

        else{

            int prizeMoney = 0;

            if(prizes.ContainsKey(num1))

                prizeMoney += prizes[num1];

            if(prizes.ContainsKey(num2))

                prizeMoney += prizes[num2];

            Console.WriteLine("You earned {0} in total!", prizeMoney);

        }

    }

}

Solutions

Expert Solution

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;


class GuessingGame
{

public static void Main()
{

Dictionary<int, int> prizes = new Dictionary<int, int>();

Random rndGen = new Random(0);

int num, money;

int i = 0;

while (i < 10)
{

num = rndGen.Next(1, 20);

money = rndGen.Next(1, 10000);

if (!prizes.ContainsKey(num))
{

prizes.Add(num, money);

i++;

}

}

Console.Write("Enter two numbers(space separated): ");

string[] line = Console.ReadLine().Split(' ');

int num1 = Int32.Parse(line[0]);

int num2 = Int32.Parse(line[1]);

if (num1 == num2 || num1 < 1 || num1 > 20 || num2 < 0 || num2 > 20)

Console.WriteLine("quit");

else
{

int prizeMoney = 0;

if (prizes.ContainsKey(num1))

prizeMoney += prizes[num1];

if (prizes.ContainsKey(num2))

prizeMoney += prizes[num2];

Console.WriteLine("You earned {0} in total!", prizeMoney);

if (prizeMoney == 0)
{
int maxValue = prizes.Max(aPair => aPair.Value);
KeyValuePair<int,int> maxPair = prizes.First(aPair=> (aPair.Value == maxValue));
Console.WriteLine($"You are unlucky!If you guessed {maxPair.Key},You would have won the most money!");
}
else
{
var list = new List<int>();
for (int k = 1;k <= 20;k++)
{
if (!prizes.ContainsKey(k))
{
list.Add(k);
}
}
Console.WriteLine("You are so lucky! You could have 0 money if you have guessed");

foreach (var item in list)
{
Console.WriteLine(item);
}
}
}

  

}

}

OUTPUT:


Related Solutions

JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Using pseudocode design a number guessing game program. The program should generate a random number and...
Using pseudocode design a number guessing game program. The program should generate a random number and then ask the user to guess the number. Each time the user enters his or her guess, the program should indicate it was too high or too low. The game is over when the user correctly guesses the number. When the game ends, the program should display the number of guesses that the user made.
Part1. Create a number guessing game in Python. Randomly generate a number from 1 to 10....
Part1. Create a number guessing game in Python. Randomly generate a number from 1 to 10. Have the user guess the number. If it is too high, tell the user to guess lower - it it is too low, tell the user to guess higher. Continue until she guesses the correct number. - Part 2. Allow the user to play a new game after they have guessed correctly.   Part 3. Ask for a different name and keep track of how...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
Write a C++ console application to simulate a guessing game. Generate a random integer between one...
Write a C++ console application to simulate a guessing game. Generate a random integer between one and 100 inclusive. Ask the user to guess the number. If the user’s number is lower than the random number, let the user know. If the number is higher, indicate that to the user. Prompt the user to enter another number. The game will continue until the user can find out what the random number is. Once the number is guessed correctly, display a...
guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT