mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Do not use an alias. Added Bye text. Number input function.
This commit is contained in:
@@ -1,18 +1,18 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
WL(Tab(34) + "HI LO");
|
Console.WriteLine(Tab(34) + "HI LO");
|
||||||
WL(Tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
Console.WriteLine(Tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||||
WL();
|
Console.WriteLine();
|
||||||
WL();
|
Console.WriteLine();
|
||||||
WL();
|
Console.WriteLine();
|
||||||
WL("THIS IS THE GAME OF HI LO.");
|
Console.WriteLine("THIS IS THE GAME OF HI LO.");
|
||||||
WL();
|
Console.WriteLine();
|
||||||
WL("YOU WILL HAVE 6 TRIES TO GUESS THE AMOUNT OF MONEY IN THE");
|
Console.WriteLine("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");
|
Console.WriteLine("HI LO JACKPOT, WHICH IS BETWEEN 1 AND 100 DOLLARS. IF YOU");
|
||||||
WL("GUESS THE AMOUNT, YOU WIN ALL THE MONEY IN THE JACKPOT!");
|
Console.WriteLine("GUESS THE AMOUNT, YOU WIN ALL THE MONEY IN THE JACKPOT!");
|
||||||
WL("THEN YOU GET ANOTHER CHANCE TO WIN MORE MONEY. HOWEVER,");
|
Console.WriteLine("THEN YOU GET ANOTHER CHANCE TO WIN MORE MONEY. HOWEVER,");
|
||||||
WL("IF YOU DO NOT GUESS THE AMOUNT, THE GAME ENDS.");
|
Console.WriteLine("IF YOU DO NOT GUESS THE AMOUNT, THE GAME ENDS.");
|
||||||
WL();
|
Console.WriteLine();
|
||||||
|
|
||||||
// rnd is our random number generator
|
// rnd is our random number generator
|
||||||
Random rnd = new();
|
Random rnd = new();
|
||||||
@@ -20,49 +20,64 @@ Random rnd = new();
|
|||||||
bool playAgain = false;
|
bool playAgain = false;
|
||||||
int totalWinnings = 0;
|
int totalWinnings = 0;
|
||||||
|
|
||||||
do
|
do // Our game loop
|
||||||
{
|
{
|
||||||
int jackpot = rnd.Next(100) + 1; // [0..99] + 1 -> [1..100]
|
int jackpot = rnd.Next(100) + 1; // [0..99] + 1 -> [1..100]
|
||||||
int guess = 1;
|
int guess = 1;
|
||||||
|
|
||||||
while (true)
|
while (true) // Our guessing loop
|
||||||
{
|
{
|
||||||
W("YOUR GUESS ");
|
Console.WriteLine();
|
||||||
int amount = int.Parse(Console.ReadLine().Trim());
|
int amount = ReadInt("YOUR GUESS ");
|
||||||
|
|
||||||
if (amount == jackpot)
|
if (amount == jackpot)
|
||||||
{
|
{
|
||||||
WL($"GOT IT!!!!!!!!!! YOU WIN {jackpot} DOLLARS.");
|
Console.WriteLine($"GOT IT!!!!!!!!!! YOU WIN {jackpot} DOLLARS.");
|
||||||
totalWinnings += jackpot;
|
totalWinnings += jackpot;
|
||||||
WL($"YOUR TOTAL WINNINGS ARE NOW {totalWinnings} DOLLARS.");
|
Console.WriteLine($"YOUR TOTAL WINNINGS ARE NOW {totalWinnings} DOLLARS.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (amount > jackpot)
|
else if (amount > jackpot)
|
||||||
{
|
{
|
||||||
WL("YOUR GUESS IS TOO HIGH.");
|
Console.WriteLine("YOUR GUESS IS TOO HIGH.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
WL("YOUR GUESS IS TOO LOW.");
|
Console.WriteLine("YOUR GUESS IS TOO LOW.");
|
||||||
}
|
}
|
||||||
|
|
||||||
guess++;
|
guess++;
|
||||||
if (guess > 6)
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WL(); W("PLAY AGAIN (YES OR NO) ");
|
Console.WriteLine();
|
||||||
|
Console.Write("PLAY AGAIN (YES OR NO) ");
|
||||||
playAgain = Console.ReadLine().ToUpper().StartsWith("Y");
|
playAgain = Console.ReadLine().ToUpper().StartsWith("Y");
|
||||||
|
|
||||||
} while (playAgain);
|
} while (playAgain);
|
||||||
|
|
||||||
// Tab(n) returns n white spaces
|
Console.WriteLine();
|
||||||
static string Tab(int n) => new String(' ', n);
|
Console.WriteLine("SO LONG. HOPE YOU ENJOYED YOURSELF!!!");
|
||||||
// W is an alias for Console.Write
|
|
||||||
static void W(string text) => Console.Write(text);
|
// Tab(n) returns n spaces
|
||||||
// WL is an alias for Console.WriteLine
|
static string Tab(int n) => new String(' ', n);
|
||||||
static void WL(string text = "") => Console.WriteLine(text);
|
|
||||||
|
// 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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user