Yes, this is pretty sloppy. I probably should have used flags for some (or all?) of the error checking.
And some of the while loops probably could have been for loops, when I used a counter...except that you'd need to break out of the for loop when the user gets the correct answer and I don't know how to do that. How do you do that?
Also, is there a way to keep from having to create different variable names (example: for the user's answer or for their guess)?
So here's my sloppy code:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
private static void greeting()
{
//Depending on the current time, print either "Good morning," "Good afternoon," or "Good evening"
DateTime time = new DateTime();
//get the current time
time = DateTime.Now;
//if it is past midnight but before noon, print "Good morning."
if (time.Hour >=0 && time.Hour < 12 )
{
Console.WriteLine("Good morning.");
}
//if it is past noon but before 6PM, print "Good afternoon."
else if (time.Hour >= 12 && time.Hour < 18)
{
Console.WriteLine("Good afternoon.");
}
//if it is past 6PM, print "Good evening."
else if (time.Hour >= 18)
{
Console.WriteLine("Good evening.");
}
}
//method to get answer from user, when asked if they want to know my name
public static void GetAns()
{
string ans = Console.ReadLine();
ans = ans.ToUpper();
if (ans == "Y")
{
Console.WriteLine("Okay. You asked. My name is Tammy.");
greeting();
}
else //if they enter anything other than Y or y, allow them to exit
{
Console.WriteLine("Okay. I will not tell you my name.");
greeting();
}
}
//method called when the user doesn't enter a valid number
public static void GuessError()
{
Console.WriteLine("You must enter a whole number greater than zero.");
}
static void Main(string[] args)
{ //initialize a new instance of the Random class
Random RandomClass = new Random();
//print greeting based on user's current time
greeting();
Console.WriteLine("Welcome to the game.\n");
Console.WriteLine("What shall I call you?");
//assign whatever they type to a property called name, which we will use throughout the game
string name = Console.ReadLine();
Console.WriteLine("Hello {0}!", name);
Console.WriteLine("Would you like to play a game? Type Y for yes or N for no.");
string answer = Console.ReadLine();
answer = answer.ToUpper();
if (answer == "Y")
{
Console.WriteLine("What do you want to play, {0}? Pick one: 1. Harder guessing game 2. Math game 3. Easy guessing game", name);
int game;
//if they didn't enter a number, return 0
int.TryParse(Console.ReadLine(), out game);
//show error if they did not enter a number between 1 and 3
while (game <= 0 || game > 3)
{
Console.WriteLine("You must enter either 1, 2, or 3.");
int.TryParse(Console.ReadLine(), out game);
}
switch (game)
{
case 1: Console.WriteLine("Okay, let's start the harder guessing game!");
Console.WriteLine("I am thinking of a number between one and one hundred.");
Console.WriteLine("Try to guess my number, {0}! Type your first guess now.", name);
//get a random number between one and 100
int myNum = RandomClass.Next(1, 100);
//create property for guesses
int guess;
int.TryParse(Console.ReadLine(), out guess);
//make sure the user entered a valid number
while (guess <=0 || guess >= 101)
{
GuessError();
int.TryParse(Console.ReadLine(), out guess);
}
int countr = 0;
//let them keep guessing until they guess the right number or for 10? tries
while (guess != myNum && countr<9)
{
//give a hint if they are within one number
if (guess == (myNum + 1)|| guess == myNum -1)
{
//put a couple methods in here for repeating code
Console.WriteLine("That's pretty close, {0}, but not right. Guess again!", name);
int.TryParse(Console.ReadLine(), out guess);
}
//if they guess too high, tell them to guess lower
else if (guess > myNum)
{
Console.WriteLine("Lower!");
int.TryParse(Console.ReadLine(), out guess);
}
//if they guess too low, tell them to guess higher
else if (guess < myNum)
{
Console.WriteLine("Higher!");
int.TryParse(Console.ReadLine(), out guess);
}
countr++;
//make sure the user entered a valid number
while (guess <= 0 || guess >= 101)
{
GuessError();
int.TryParse(Console.ReadLine(), out guess);
}
}
if (countr >= 9)
{
Console.WriteLine("Sorry, {0}, the correct answer was " + myNum.ToString(), name);
}
if (guess == myNum)
{
Console.WriteLine("Good guess, {0}! That's my number!", name);
}
Console.WriteLine("Would you like to play again?");
string again = Console.ReadLine();
again = again.ToUpper();
//if they want to play again, repeat the game
if (again == "Y")
{
goto case 1;
}
else//if they enter anything other than Y or y, end the game
Console.WriteLine("Okay. No more games. Shall I tell you my name, {0}?", name);
//call method to get the user's answer
GetAns();
break;
case 2: Console.WriteLine("Let's play a math game. Pick one: 1. addition or 2. subtraction.");
int pick;
//if they didn't enter a number, return 0
int.TryParse(Console.ReadLine(), out pick);
//if they didn't enter either 1 or 2, show error
while (pick <= 0 || pick > 2)
{
Console.WriteLine("You must enter either 1 or 2.");
int.TryParse(Console.ReadLine(), out pick);
}
if (pick == 1)
{
Console.WriteLine("Let's start adding!");
int add1 = RandomClass.Next(1, 100);
int add2 = RandomClass.Next(1, 100);
Console.WriteLine("Add " + add1.ToString() + " and " + add2.ToString() + ".");
Console.WriteLine("Type your answer now.");
int sumGuess;
int.TryParse(Console.ReadLine(), out sumGuess);
//make sure they entered a number
while (sumGuess == 0)
{
GuessError();
int.TryParse(Console.ReadLine(), out sumGuess);
}
int sum = add1 + add2;
int tries = 0;
//let them keep trying until they get it or for 10 tries
while (sumGuess != sum && tries < 9)
{
//if their answer is within one number, give a hint
if (sumGuess == sum + 1 || sumGuess == (sum - 1))
{
Console.WriteLine("That's pretty close, {0}, but not right. Keep guessing!", name);
}
else
{
Console.WriteLine("Keep trying!");
}
//update counter
tries++;
int.TryParse(Console.ReadLine(), out sumGuess);
//show error if they don't enter number or leave blank
while (sumGuess == 0)
{
GuessError();
int.TryParse(Console.ReadLine(), out sumGuess);
}
}
if (tries >= 9)
{ //if they don't get it within 10 tries, tell them the answer
Console.WriteLine("Sorry, {0}, the correct answer is " + sum + ".", name);
}
if (sumGuess == sum)
{
Console.WriteLine("Congratulations! That is the correct answer.");
}
Console.WriteLine("Would you like to play again? Type Y or N.");
string addAgain = Console.ReadLine();
if (addAgain == "Y" || addAgain == "y")
{
goto case 2;
}
else
{
Console.WriteLine("Ok, so you don't want to add any more. Do you want to know my name?");
//call method to get the user's answer
GetAns();
}
}
else if (pick == 2)
{
Console.WriteLine("Let's start subtracting!");
int sub1 = RandomClass.Next(1, 100);
int sub2 = RandomClass.Next(1, 100);
int trys = 0;
//make sure the first number is more than the second one, to avoid negative numbers
//and make sure the two numbers are not equal, so the answer will never be zero
while (sub1 <= sub2)
{ sub1 = RandomClass.Next(1, 100);
sub2 = RandomClass.Next(1, 100);
}
int diff = sub1 - sub2;
Console.WriteLine("Subtract " + sub1 + " minus " + sub2 + ".");
Console.WriteLine("Type your answer now.");
int diffGuess;
int.TryParse(Console.ReadLine(), out diffGuess);
//show error if they didn't enter a number
while (diffGuess == 0)
{
GuessError();
int.TryParse(Console.ReadLine(), out diffGuess);
}
//let them keep trying until they get it or for 10 tries
while (diffGuess != diff && trys < 9)
{
//if their answer is within one number, give a hint
if (diffGuess == diff + 1 || diffGuess == diff - 1)
{
Console.WriteLine("That's pretty close, {0}, but not right. Try again.",name);
}
else
{
Console.WriteLine("Keep trying!");
}
//update counter
trys++;
int.TryParse(Console.ReadLine(), out diffGuess);
//show error if they don't enter number or leave blank
//this code is repeated, can I avoid this?
while (diffGuess == 0)
{
GuessError();
int.TryParse(Console.ReadLine(), out diffGuess);
}
}
if (trys >= 9)
{
Console.WriteLine("Sorry, {0}, the correct answer is " + diff + ".", name);
}
if (diffGuess == diff)
{
Console.WriteLine("Congratulations! That is the correct answer.");
}
Console.WriteLine("Would you like to play again? Type Y or N.");
string subAgain = Console.ReadLine();
if (subAgain == "Y" || subAgain == "y")
{
goto case 2;
}
else
{
Console.WriteLine("Ok, so you don't want to subtract any more. Do you want to know my name?");
//call method to get the user's answer
GetAns();
}
}
break;
case 3: Console.WriteLine("Let's start the easy guessing game!");
Console.WriteLine("I am thinking of a number between one and ten.");
Console.WriteLine("Try to guess my number, {0}! Type your first guess now.", name);
//get a random number between one and ten
myNum = RandomClass.Next(1, 10);
int.TryParse(Console.ReadLine(), out guess);
//make sure the user enters a valid number (1-10)
while(guess <=0||guess>=11)
{
GuessError();
int.TryParse(Console.ReadLine(), out guess);
}
//let them keep guessing until they guess the right number or for 5 tries
int count = 0;
while (guess != myNum && count<4)//count starts at 0, so when it gets to 4, they will have used 5 tries
{
//give a hint if they guess within one number
if (guess == (myNum + 1) || guess == myNum - 1)
{
Console.WriteLine("That's pretty close, {0}, but not right. Guess again!", name);
count++;
int.TryParse(Console.ReadLine(), out guess);
}
else
{
Console.WriteLine("Keep guessing!");
count++;
int.TryParse(Console.ReadLine(), out guess);
}
//make sure the user enters a valid number (1-10)
while (guess <= 0 || guess >= 11)
{
GuessError();
int.TryParse(Console.ReadLine(), out guess);
}
}
//if they use up all their tries, tell them the answer
if (count >= 4)
{
Console.WriteLine("Sorry, {0}, my number was " + myNum + ".", name);
}
//if they get the right answer, let them know
if (guess == myNum)
{
Console.WriteLine("Good guess, {0}! That's my number!", name);
}
//give the user a choice whether to play again
Console.WriteLine("Would you like to play again?");
again = Console.ReadLine();
again = again.ToUpper();
//if they want to play again, repeat the game
if (again == "Y")
{
goto case 3;
}
else//if they enter anything other than Y or y, end the game
Console.WriteLine("Okay. No more games, {0}. Shall I tell you my name?", name);
//call method to get the user's answer
GetAns();
break;
}
}
else //if they enter anything other than Y or y when asked if they want to play a game
{
Console.WriteLine("Okay. No games. Shall I tell you my name, {0}?", name);
//call method to get the user's answer
GetAns();
}
}
}
}

New Topic/Question
Reply




MultiQuote






|