In: Computer Science
Part (a) Write a number guessing game using System.Collections.Generic.Dictionary.
e.g. Suppose we generate the following random numbers and penalty money:
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);
}
}
}
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: