SlideShare a Scribd company logo
1 of 5
Download to read offline
/*
*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
/*____________________________ 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();
/*================== 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++);
}
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();
/*==================== 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"));
}
}
}

More Related Content

What's hot

step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension
Yuren Ju
 

What's hot (19)

Asynchronous programming and mutlithreading
Asynchronous programming and mutlithreadingAsynchronous programming and mutlithreading
Asynchronous programming and mutlithreading
 
Project hotel on hotel management fo
Project  hotel on hotel management foProject  hotel on hotel management fo
Project hotel on hotel management fo
 
Asynchronous programming with java script and node.js
Asynchronous programming with java script and node.jsAsynchronous programming with java script and node.js
Asynchronous programming with java script and node.js
 
.net progrmming part2
.net progrmming part2.net progrmming part2
.net progrmming part2
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension
 
Node.js in 2020 - part 2
Node.js in 2020 - part 2Node.js in 2020 - part 2
Node.js in 2020 - part 2
 
ES2015 - Stepan Parunashvili
ES2015 - Stepan ParunashviliES2015 - Stepan Parunashvili
ES2015 - Stepan Parunashvili
 
CS3241 Lab A1
CS3241 Lab A1CS3241 Lab A1
CS3241 Lab A1
 
Metarhia KievJS 22-Feb-2018
Metarhia KievJS 22-Feb-2018Metarhia KievJS 22-Feb-2018
Metarhia KievJS 22-Feb-2018
 
How are Race Conditions in single threaded JavaScript possible?
How are Race Conditions in single threaded JavaScript possible?How are Race Conditions in single threaded JavaScript possible?
How are Race Conditions in single threaded JavaScript possible?
 
The Ring programming language version 1.5.3 book - Part 46 of 184
The Ring programming language version 1.5.3 book - Part 46 of 184The Ring programming language version 1.5.3 book - Part 46 of 184
The Ring programming language version 1.5.3 book - Part 46 of 184
 
Patterns and antipatterns
Patterns and antipatternsPatterns and antipatterns
Patterns and antipatterns
 
Programming Languages: comparison, history, future
Programming Languages: comparison, history, futureProgramming Languages: comparison, history, future
Programming Languages: comparison, history, future
 
The Ring programming language version 1.3 book - Part 36 of 88
The Ring programming language version 1.3 book - Part 36 of 88The Ring programming language version 1.3 book - Part 36 of 88
The Ring programming language version 1.3 book - Part 36 of 88
 
ES2015 and React
ES2015 and ReactES2015 and React
ES2015 and React
 
Web Locks API
Web Locks APIWeb Locks API
Web Locks API
 
Serverless Clouds (FaaS) and request context isolation in Node.js
Serverless Clouds (FaaS) and request context isolation in Node.jsServerless Clouds (FaaS) and request context isolation in Node.js
Serverless Clouds (FaaS) and request context isolation in Node.js
 
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
 
Введение в SQL
Введение в SQLВведение в SQL
Введение в SQL
 

Similar to RandomGuessingGame

DiceSimulatorProgram
DiceSimulatorProgramDiceSimulatorProgram
DiceSimulatorProgram
Amy Baxter
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
kavithaarp
 
You will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdf
FashionColZone
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
udit652068
 
Please help with this. program must be written in C# .. All of the g.pdf
Please help with this. program must be written in C# .. All of the g.pdfPlease help with this. program must be written in C# .. All of the g.pdf
Please help with this. program must be written in C# .. All of the g.pdf
manjan6
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
anjandavid
 
Please observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdfPlease observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdf
apexjaipur
 
RussianRouletteProgram
RussianRouletteProgramRussianRouletteProgram
RussianRouletteProgram
Amy Baxter
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
Dillon Lee
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
Abed Bukhari
 
Final requirement in programming niperos
Final requirement in programming   niperosFinal requirement in programming   niperos
Final requirement in programming niperos
markings17
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
arakalamkah11
 
Powerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prgPowerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prg
alyssa-castro2326
 

Similar to RandomGuessingGame (20)

DiceSimulatorProgram
DiceSimulatorProgramDiceSimulatorProgram
DiceSimulatorProgram
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
 
Bookstore-Project
Bookstore-ProjectBookstore-Project
Bookstore-Project
 
You will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdf
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 
Please help with this. program must be written in C# .. All of the g.pdf
Please help with this. program must be written in C# .. All of the g.pdfPlease help with this. program must be written in C# .. All of the g.pdf
Please help with this. program must be written in C# .. All of the g.pdf
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
Parallel Futures of a Game Engine
Parallel Futures of a Game EngineParallel Futures of a Game Engine
Parallel Futures of a Game Engine
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Please observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdfPlease observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdf
 
Game programming-help
Game programming-helpGame programming-help
Game programming-help
 
RussianRouletteProgram
RussianRouletteProgramRussianRouletteProgram
RussianRouletteProgram
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Quiz using C++
Quiz using C++Quiz using C++
Quiz using C++
 
Final requirement in programming niperos
Final requirement in programming   niperosFinal requirement in programming   niperos
Final requirement in programming niperos
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
 
Final requirement in programming vinson
Final requirement in programming  vinsonFinal requirement in programming  vinson
Final requirement in programming vinson
 
Powerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prgPowerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prg
 

More from Christopher Singleton (6)

ASP.Net, move data to and from a SQL Server Database
ASP.Net, move data to and from a SQL Server DatabaseASP.Net, move data to and from a SQL Server Database
ASP.Net, move data to and from a SQL Server Database
 
HTML5 Programming, CSS style sheets
HTML5 Programming, CSS style sheetsHTML5 Programming, CSS style sheets
HTML5 Programming, CSS style sheets
 
Using Inner-Joins (SQL)
Using Inner-Joins (SQL)Using Inner-Joins (SQL)
Using Inner-Joins (SQL)
 
Inner joins
Inner joinsInner joins
Inner joins
 
SSIS_Project
SSIS_ProjectSSIS_Project
SSIS_Project
 
CS_Resume
CS_ResumeCS_Resume
CS_Resume
 

RandomGuessingGame

  • 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")); } } }