From b48634cccf50ed37ab2b4154e64bc92520d20a4a Mon Sep 17 00:00:00 2001 From: Rick van Lieshout Date: Wed, 17 Feb 2021 13:44:02 +0100 Subject: [PATCH 1/2] 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 + + + From 0d2219d1b8decbfda2f67fb8d73b1cf8aa963c5f Mon Sep 17 00:00:00 2001 From: Rick van Lieshout Date: Wed, 17 Feb 2021 16:48:25 +0100 Subject: [PATCH 2/2] Do not use an alias. Added Bye text. Number input function. --- 47 Hi-Lo/csharp/hi-lo.cs | 73 ++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/47 Hi-Lo/csharp/hi-lo.cs b/47 Hi-Lo/csharp/hi-lo.cs index 7ec48ca2..c5cfb3ec 100644 --- a/47 Hi-Lo/csharp/hi-lo.cs +++ b/47 Hi-Lo/csharp/hi-lo.cs @@ -1,18 +1,18 @@ 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(); +Console.WriteLine(Tab(34) + "HI LO"); +Console.WriteLine(Tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); +Console.WriteLine(); +Console.WriteLine(); +Console.WriteLine(); +Console.WriteLine("THIS IS THE GAME OF HI LO."); +Console.WriteLine(); +Console.WriteLine("YOU WILL HAVE 6 TRIES TO GUESS THE AMOUNT OF MONEY IN THE"); +Console.WriteLine("HI LO JACKPOT, WHICH IS BETWEEN 1 AND 100 DOLLARS. IF YOU"); +Console.WriteLine("GUESS THE AMOUNT, YOU WIN ALL THE MONEY IN THE JACKPOT!"); +Console.WriteLine("THEN YOU GET ANOTHER CHANCE TO WIN MORE MONEY. HOWEVER,"); +Console.WriteLine("IF YOU DO NOT GUESS THE AMOUNT, THE GAME ENDS."); +Console.WriteLine(); // rnd is our random number generator Random rnd = new(); @@ -20,49 +20,64 @@ Random rnd = new(); bool playAgain = false; int totalWinnings = 0; -do +do // Our game loop { int jackpot = rnd.Next(100) + 1; // [0..99] + 1 -> [1..100] int guess = 1; - while (true) + while (true) // Our guessing loop { - W("YOUR GUESS "); - int amount = int.Parse(Console.ReadLine().Trim()); + Console.WriteLine(); + int amount = ReadInt("YOUR GUESS "); if (amount == jackpot) { - WL($"GOT IT!!!!!!!!!! YOU WIN {jackpot} DOLLARS."); + Console.WriteLine($"GOT IT!!!!!!!!!! YOU WIN {jackpot} DOLLARS."); totalWinnings += jackpot; - WL($"YOUR TOTAL WINNINGS ARE NOW {totalWinnings} DOLLARS."); + Console.WriteLine($"YOUR TOTAL WINNINGS ARE NOW {totalWinnings} DOLLARS."); break; } else if (amount > jackpot) { - WL("YOUR GUESS IS TOO HIGH."); + Console.WriteLine("YOUR GUESS IS TOO HIGH."); } else { - WL("YOUR GUESS IS TOO LOW."); + Console.WriteLine("YOUR GUESS IS TOO LOW."); } guess++; if (guess > 6) { - WL($"YOU BLEW IT...TOO BAD...THE NUMBER WAS {jackpot}"); + Console.WriteLine($"YOU BLEW IT...TOO BAD...THE NUMBER WAS {jackpot}"); break; } } - WL(); W("PLAY AGAIN (YES OR NO) "); + Console.WriteLine(); + Console.Write("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); +Console.WriteLine(); +Console.WriteLine("SO LONG. HOPE YOU ENJOYED YOURSELF!!!"); + +// Tab(n) returns n spaces +static string Tab(int n) => new String(' ', n); + +// ReadInt asks the user to enter a number +static int ReadInt(string question) +{ + while (true) + { + Console.Write(question); + var input = Console.ReadLine().Trim(); + if (int.TryParse(input, out int value)) + { + return value; + } + Console.WriteLine("!Invalid Number Entered."); + } +}