From b48634cccf50ed37ab2b4154e64bc92520d20a4a Mon Sep 17 00:00:00 2001 From: Rick van Lieshout Date: Wed, 17 Feb 2021 13:44:02 +0100 Subject: [PATCH] C# Top-Level Application of 47 Hi-Lo --- 47 Hi-Lo/csharp/hi-lo.cs | 68 ++++++++++++++++++++++++++++++++++++ 47 Hi-Lo/csharp/hi-lo.csproj | 8 +++++ 2 files changed, 76 insertions(+) create mode 100644 47 Hi-Lo/csharp/hi-lo.cs create mode 100644 47 Hi-Lo/csharp/hi-lo.csproj diff --git a/47 Hi-Lo/csharp/hi-lo.cs b/47 Hi-Lo/csharp/hi-lo.cs new file mode 100644 index 00000000..7ec48ca2 --- /dev/null +++ b/47 Hi-Lo/csharp/hi-lo.cs @@ -0,0 +1,68 @@ +using System; + +WL(Tab(34) + "HI LO"); +WL(Tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); +WL(); +WL(); +WL(); +WL("THIS IS THE GAME OF HI LO."); +WL(); +WL("YOU WILL HAVE 6 TRIES TO GUESS THE AMOUNT OF MONEY IN THE"); +WL("HI LO JACKPOT, WHICH IS BETWEEN 1 AND 100 DOLLARS. IF YOU"); +WL("GUESS THE AMOUNT, YOU WIN ALL THE MONEY IN THE JACKPOT!"); +WL("THEN YOU GET ANOTHER CHANCE TO WIN MORE MONEY. HOWEVER,"); +WL("IF YOU DO NOT GUESS THE AMOUNT, THE GAME ENDS."); +WL(); + +// rnd is our random number generator +Random rnd = new(); + +bool playAgain = false; +int totalWinnings = 0; + +do +{ + int jackpot = rnd.Next(100) + 1; // [0..99] + 1 -> [1..100] + int guess = 1; + + while (true) + { + W("YOUR GUESS "); + int amount = int.Parse(Console.ReadLine().Trim()); + + if (amount == jackpot) + { + WL($"GOT IT!!!!!!!!!! YOU WIN {jackpot} DOLLARS."); + totalWinnings += jackpot; + WL($"YOUR TOTAL WINNINGS ARE NOW {totalWinnings} DOLLARS."); + break; + } + else if (amount > jackpot) + { + WL("YOUR GUESS IS TOO HIGH."); + } + else + { + WL("YOUR GUESS IS TOO LOW."); + } + + guess++; + if (guess > 6) + { + WL($"YOU BLEW IT...TOO BAD...THE NUMBER WAS {jackpot}"); + break; + } + } + + WL(); W("PLAY AGAIN (YES OR NO) "); + playAgain = Console.ReadLine().ToUpper().StartsWith("Y"); + +} while (playAgain); + +// Tab(n) returns n white spaces +static string Tab(int n) => new String(' ', n); +// W is an alias for Console.Write +static void W(string text) => Console.Write(text); +// WL is an alias for Console.WriteLine +static void WL(string text = "") => Console.WriteLine(text); + diff --git a/47 Hi-Lo/csharp/hi-lo.csproj b/47 Hi-Lo/csharp/hi-lo.csproj new file mode 100644 index 00000000..20827042 --- /dev/null +++ b/47 Hi-Lo/csharp/hi-lo.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + +