1. /*
*Project: SingletonHW6.csproj
*Solution: SingletonRandomSol.sln
*Source: GuessNumber.cs
*
* Solution/Project Name: SingletonRandomSol/GuessNumber
* File Name: **Name you give to this C# source code file**
*
* This file defines the entry point method for the Console application.
*
* Programmer: Christopher Singleton
*
* Revision Date Release Comment
* -------- ---------- ------------------------------------------------------
* 1.0 11/10/2015 Initial Release
*
* ---------------
* **Guessing number game.**
*
*
*********************************************************************//*
Description: This is a random number generated computer guessing program, where you try to match what the computer
has chosen as a stored random number. Each time you guess, the computer lets you know if your higher
or lower than the number stored. If you don't put a number or chose a number that is out of range the
computer will let you know and continue on (Note: This counts against your guessing). Each time you
press enter, whether or not you put a number, the game count continues higher as long as it is not
the number that the computer has stored. When you guess the number correctly, the computer lets you
know and you are prompt to enter Yes to start another game or No to exit the application. When you
exit the application, you will get a friendly prompt letting you know to press any key to exit.
Revision Number: 1.1.0 (Initial Release) Revision Date: 11/09/2015
Back to Resume Page
2. /*____________________________ Welcome to the Back-End of the Guessing Game! ______________________________*/
using System;
namespace SingletonHW6
{
class GuessNumber
{
static void Main(string[] args)
{ //Fixed all the problems except for the heading showing only once.
/*============ Initialize all variables ============*/
int userInput = 2, computerGuess = 0, numAttempts = 0, playAgain = 2;
const int MIN = 1, MAX = 50;
string strInput, strPlayAgain;
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.WindowWidth = 95;
Console.WindowHeight = 40;
/*============= Prompt the user to guess 1 through 50 ===========*/
Random randNumGenerator = new Random();
int numberoflayer = 9, Space = 0, Number = 0;
//Begin the first loop.
do
{
Console.Clear();
// store a random number from 1 to 100, where MIN=1, MAX=100
computerGuess = randNumGenerator.Next(MIN, MAX + 1);
/*================== Display the Pyramid ====================*/
//Console.WriteLine("Print pyramid");
for (int i = 1; i <= numberoflayer; i++)
{
for (Space = 1; Space <= (numberoflayer - i); Space++)
Console.Write(" ");
Console.ForegroundColor = ConsoleColor.White;
for (Number = 1; Number <= i; Number++)
Console.Write(Number);
for (Number = (i - 1); Number >= 1; Number--)
Console.Write(Number);
Console.WriteLine();
3. /*================== Display the Heading =================*/
}
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("****************************************************");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" ***************************************************" +
"n ***************************************************" +
"n ********* ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("THE RANDOM NUMBER GUESSING GAME! ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("********n *** ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Welcome to our random number guessing game! ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("***n *******");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" Where computers and minds think a-like! ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("*** n ***************************************************" +
"n ***************************************************n");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("****************************************************");
/*================ Arguments for User Guidance ===============*/
do
{
//Set the Conditions for each if statement to be displayed.
if (userInput != computerGuess && numAttempts == 0)
{Console.ForegroundColor = ConsoleColor.Green;
Console.Write("nnn Number of guess attempts: {0}n", numAttempts++, computerGuess);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" Please guess a number 1 through 50 (inclusive){0}: ", computerGuess);
}
else if (userInput < MIN || userInput > MAX)
{Console.ForegroundColor = ConsoleColor.Green;
Console.Write("n Number of guess attempts: {0}n", numAttempts++);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" The number you entered was out of range!n" +
" You need to enter a number 1 through 50 (inclusive)" +
", please try again!: ", numAttempts++);
}
4. else if (numAttempts > 0 && userInput > computerGuess)
{Console.ForegroundColor = ConsoleColor.Green;
Console.Write("n Number of failed guess attempts: {0}n", numAttempts++, computerGuess);
Console.ForegroundColor = ConsoleColor.White;
Console.Write("n Nice try! Please guess a number 1 through 50 (inclusive)," +
"n Please Guess again (Lower): " +
"", numAttempts++, computerGuess);
}
else
{Console.ForegroundColor = ConsoleColor.Green;
Console.Write("n Number of attempts: {0}n", numAttempts++, computerGuess);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" Nice try! Please Guess again (Higher) " +
"n Please guess a number 1 through 50 (inclusive): " +
"", numAttempts++, computerGuess);
}
strInput = Console.ReadLine();
/*=============== Exception Handling for Input ===============*/
try
{
userInput = int.Parse(strInput);
}
/*If the user leaves the input blank while pressing the
enter key or enters something that is unexpected,
catch the exeption and let them know while reprompting them.*/
catch (FormatException)
{Console.ForegroundColor = ConsoleColor.Red;
Console.Write("n For some reason you did not enter" +
" a number while pressing the enter key" +
"n or you entered something that was not expected (Error).n");
}
}
/* Continue the loop if these conditions are not met.*/
while (userInput < MIN || userInput > MAX || computerGuess != userInput);
/*================ Display if the User is Right ================*/
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("nnn Total number of attempts:{0}n", numAttempts);
{Console.ForegroundColor = ConsoleColor.White;
Console.Write(" You win! The value you entered was: {0}, which is correct!n " +
"nnnnn Would you like to play again? Type Yes/No: ", computerGuess);
playAgain = userInput;
numAttempts = 0;
strPlayAgain = Console.ReadLine().ToLower();
5. /*==================== Play Again Yes/No ====================*/
//User yes/no decisions.
switch (strPlayAgain)
{
case "y":
{ playAgain = 1; }
break;
case "yes":
{ playAgain = 1; }
break;
case "n":
{Console.ForegroundColor = ConsoleColor.Red;
Console.Write("nnn Please press any key to exit this program => "+
" CSingleton (EOP) => ");
Console.ReadKey();
}
break;
case "no":
{Console.ForegroundColor = ConsoleColor.Red;
Console.Write("nnn Please press any key to exit this program => " +
" CSingleton (EOP) => ");
Console.ReadKey();
}
break;
default:
{Console.ForegroundColor = ConsoleColor.Red;
Console.Write("nnn Please press any key to exit this program => " +
" CSingleton (EOP) => ");
Console.ReadKey();
}
break;
}
}
/*======================= End of Program =======================*/
}
/*Note: Continue the loop only if the two variables do not have a value of 2,
otherwise the program will exit if the variable values are set to 2 by the user input.*/
while (strPlayAgain.Equals("y") || strPlayAgain.Equals("yes"));
}
}
}