I am fairly new to OO programming, I have created a simple "Guess the number" game, it functions correctly. However, when attempting to implement a loop errors occur. More specifically, I want the user to restart the program through a console [not compiling and running the game again].
I need help with the static void ExitGame() method. At this moment in time, I am working on "RepL.it" and the generated errors are as followed:
main.cs (10,10): warning CS0649: Field GuessTheNumber.Game.replay' is never assigned to, and will always have its default valuenull'
Compilation succeeded - 1 warning (s)
Welcome to the guessing game. Please press enter.
using System;
namespace GuessTheNumber
{
class Game
{
static int UserGuess;
static int Answer;
string replay;
static string EndProg = "No";
static void Main (string[] args)
{
Console.Title = "Guess the number.";
EntryMessage();
GenerateRandom();
while (EndProg == "No") {
askData();
}
}
static void EntryMessage()
{
Console.WriteLine("Welcome to the guessing game. Please press enter.");
Console.ReadLine();
}
public static void askData()
{
while (EndProg == "No")
{
Console.WriteLine(Answer);
Console.WriteLine("Guess a number between 1 and 100.");
UserGuess = Convert.ToInt32(Console.ReadLine());
WinLose();
}
askData();
}
public void askData(bool endProg)
{
Console.WriteLine("Does you want to play again");
if (replay == "y")//; Remove this semicolon
{
Console.WriteLine("\nOkay, guess again");
askData(EndProg == "No");
}
else if (replay == "n")//; Remove this semicolon
{
askData(EndProg == "Yes");
}
else
{
Console.ReadLine();
}
}
static void GenerateRandom()
{
Random random = new Random();
Answer = random.Next(0,101);
}
static void WinLose()
{
if (UserGuess == Answer)
{
Console.WriteLine("Correct number!");
EndProg="Yes";
}
else if (UserGuess > Answer)
{
Console.WriteLine("Too high.");
EndProg="No";
}
else if (UserGuess < Answer)
{
Console.WriteLine("Too Low.");
EndProg="No";
}
else
{
Console.WriteLine("Invalid answer.");
EndProg="No";
}
}
}
}
There's a number of syntactical errors in your code:
If statement parentheses shouldn't be suffixed with a semicolon
else if (replay == "n");
should be
else if (replay == "n")
Your askData() method is sometimes being called with a bool argument but doesn't take one
void askData()
should be
void askData(bool endProg)
You are mixing and matching static and non-static methods and need to decide whether to implement this logic in a static or instance context
I believe the culprit here is the semicolons you have after your if/else statements:
static void ExitGame()
{
Console.WriteLine("Does you want to play again");
if (replay == "y")//; Remove this semicolon
{
Console.WriteLine("\nOkay, guess again");
askData(EndProg == "No");
}
else if (replay == "n")//; Remove this semicolon
{
askData(EndProg == "Yes");
}
else
{
Console.ReadLine();
}
}
Those semicolons are terminating the statement, which would then make the Else If/Else blocks not follow an IF block, which is syntactically invalid.
This also explains the empty statement warnings the compiler is generating.
Related
The code works perfectly, but I am not sure if this is good practice or not. I think it could be improved and made easier, but I don't know how. Maybe I could have used more variables to make it simpler. Or is it fine as long as it works? I also need to work on tidying my code up.
using System;
namespace Lesson_number_16_Building_a_guessing_game
{
class Program
{
static void Main(string[] args)
{
string secretWord = "monkey";
string guess = "";
int count = 3;
Console.WriteLine("Please type an animal name that starts with the letter M, you have 3 guesses: ");
guess = Console.ReadLine();
if (guess == secretWord)
{
Console.WriteLine("Nice, you got it right.");
} else if (guess != secretWord)
{
while (guess != secretWord && count > 1)
{
count--;
Console.WriteLine("Nope, try again! you have " + count + " guesses left");
guess = Console.ReadLine();
if (guess == secretWord)
{
Console.WriteLine("Nice, you got it right.");
} else if (count == 1)
{
Console.WriteLine("You have failed to guess it. It was " + secretWord);
break;
}
}
}
}
}
}
1.Instead of initializing the string guess="", you can always prefer string guess=string.Empty.
2.Try to align the curly braces using keys like Ctrl + K.(It depends on Visual Studio version).
Here's a couple of things I would change:
Get rid of the magic number '1' breaking out of the loop. Declare it
as a const with a meaningful name.
since you're breaking out of the while loop within the while loop when validating the retrycount, there is no point to put the
condition in the while loop as well.
Combine the initial question within the loop as well and break out of the loop when correct.
Combine counter substraction and lowerbound check in one call.
Use the equals method for string comparison with the right culture.
using System;
namespace Lesson_number_16_Building_a_guessing_game
{
internal class Program
{
private static void Main(string[] args)
{
const string secretWord = "monkey";
int count = 3;
const int lowerGuessBound = 0;
Console.WriteLine("Please type an animal name that starts with the letter M, you have 3 guesses: ");
while (true)
{
if (Console.ReadLine().Equals(secretWord, StringComparison.CurrentCulture))
{
Console.WriteLine("Nice, you got it right.");
break;
}
else if (--count == lowerGuessBound)
{
Console.WriteLine($"You have failed to guess it. It was {secretWord}");
break;
}
Console.WriteLine($"Nope, try again! you have {count} guesses left");
}
}
}
}
I think that you can shorten and tidy-up your code to something like:
const string secretWord = "monkey";
string guess = "";
const int count = 3;
Console.WriteLine($"Please type an animal name that starts with the letter M, you have {count} guesses: ");
while(count>0)
{
guess = Console.ReadLine();
count--;
if(guess != secretWord )
{
Console.WriteLine($"Nope, try again! you have {count} guesses left");
}
else
{
count=0;
}
}
if(guess != secretWord )
{
Console.WriteLine($"You have failed to guess it. It was {secretWord}");
}
else
{
Console.WriteLine("Nice, you got it right.");}
}
Using const declaration helps to protect against a code error that changes a value that shouldn't. It can also be used on variable input for functions/methods.
I try not to use == where possible as its easy to type = by mistake.
The string interpolation character $ makes for easier understanding of strings that include parameter values when reading code.
Now here is the situation we are in:
getinput:
//things happen here
string typed = Console.ReadLine();
try
if (typed == "com")
{
//things happen here
}
else if (Console.ReadKey(true).Key == (ConsoleKey.F1) + (ConsoleModifiers.Alt))
{
System.Environment.Exit(0);
}
//other else if's happen here
else
{
Console.WriteLine("\n" + typed + " isn't an option.");
goto getInput;
}
}
catch (Exception)
{
}
goto getInput;
what i want to do with this is when i press alt+f1 the program will terminate itself however because the program waits for some input from me to write even with the working version (without the alt part) it wants me to type the things then press enter, which i dont want. how does one handlde this??
static void Main(string[] args)
{
Console.TreatControlCAsInput = true;
var typed = ReadLine();
if (typed == "com")
{
Console.WriteLine("com");
//things happen here
}
//other else if's happen here
else
{
Console.WriteLine("\n" + typed + " isn't an option.");
}
}
public static string ReadLine() {
StringBuilder sb = new StringBuilder();
do
{
ConsoleKeyInfo key = Console.ReadKey();
if ((key.Modifiers & ConsoleModifiers.Alt) != 0)
{
if (key.Key == ConsoleKey.K)
{
Console.WriteLine("killing console");
System.Environment.Exit(0);
}
}
else
{
sb.Append(key.KeyChar);
if (key.KeyChar == '\n'||key.Key==ConsoleKey.Enter)
{
return sb.ToString();
}
}
} while (true);
}
that code will help you with your problem,
just be aware that when you reading a line by char's you will need to handle things like backspace
First of all, please consider using loops instead of goto, as gotos are dangerous. Why? Have a look here: 'Goto' is this bad?
To solve your problem you can use the ConsoleKeyInfo class in combination with the Console.ReadKey() method to get information about single key presses. With this you can check for any key-combination right before adding up the single characters to a string. A working example could look like:
namespace Stackoverflow
{
using System;
class Program
{
public static void Main(string[] args)
{
ConsoleKeyInfo keyInfo = default(ConsoleKeyInfo);
string input = string.Empty;
// loop while condition is true
while (true)
{
// read input character-wise as long as user presses 'Enter' or 'Alt+F1'
while (true)
{
// read a single character and print it to console
keyInfo = Console.ReadKey(false);
// check for close-combination
if (keyInfo.Key == ConsoleKey.F1 && (keyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
{
// program terminates
Environment.Exit(0);
}
// check for enter-press
if (keyInfo.Key == ConsoleKey.Enter)
{
// break out of the loop without adding '\r' to the input string
break;
}
// add up input-string
input += keyInfo.KeyChar;
}
// optional: enter was pressed - add a new line
Console.WriteLine();
// user pressed enter, do something with the input
try
{
if (input == "com")
{
// right option - do something
}
else
{
// wrong option - reset ConsoleKeyInfo + input
Console.WriteLine("\n" + input + " isn't an option.");
keyInfo = default(ConsoleKeyInfo);
input = string.Empty;
continue;
}
}
catch (Exception)
{
// handle exceptions
}
}
}
}
}
I'm pretty new to C# (started learning it a week ago) with a little bit of experience in batch and Expression 2, and I've been working on a text-based game in an attempt to learn more. I used goto statements at first, but according to pretty much everyone I've found, goto statements are some amalgam of death and despair, so I want to learn cleaner, less evil ways to achieve the same effect. Here's a crummy example script I made to demonstrate what I mean:
using System;
namespace TestScript
{
class Program
{
public static void Main(string[] args)
{
string ConsoleReadinator;
string ConsoleReadinator2;
int Go = 0;
mainmenu:
do
{
Go = 0;
Console.Clear();
Console.WriteLine("Main Menu:");
Console.WriteLine("Store or something");
ConsoleReadinator = Console.ReadLine().ToUpper();
if (ConsoleReadinator == "STORE") { Go = 1; }
} while (Go == 0);
// In-game store example
{
Go = 0;
do
{
Console.Clear();
Console.WriteLine("In-game store I guess");
Console.WriteLine("Stuff you can buy, etc");
ConsoleReadinator2 = Console.ReadLine().ToUpper();
if (ConsoleReadinator2 == "GO") { Go = 1; }
} while (Go == 0);
goto mainmenu;
}
}
}
}
This script is functional, but I'd like to avoid using goto as a way to go back to previous statements in order to navigate menus and maybe to repeat algorithms for turn-based games. I read about using methods for this in Alternative to using goto statement in C# (which is basically the same question I have, except a bit more vague), but the example Greg made there didn't work for me at all, to the point that it's probably not worth trying to make that particular example work.
As far as I can see you want an infinite loop:
...
while (true)
{
do
{
...
} while (Go == 0);
Go = 0;
do
{
...
} while (Go == 0);
}
You could use recursion in order to go back and execute code again. For this purpose you could move your code to a separate method and call it in this method:
class Program
{
public static void Main(string[] args)
{
string ConsoleReadinator;
string ConsoleReadinator2;
Method(0);
}
private static void Method(int Go)
{
do
{
..
} while (Go == 0);
// In-game store example
do
{
...
} while (Go == 0);
Method(Go);
}
}
Or you could use loops in a better way. Let's have a look at the example when we want user to enter an integer number :
public static void Main(string[] args)
{
int num;
// This loop ends only when user enters proper integer number
do
{
Console.Clear();
Console.Write("Please enter some integer number: ");
} while(!int.TryParse(Console.ReadLine(), out num));
}
This could be done other way with recursion:
public static int EnterNumber()
{
Console.Clear();
Console.Write("Please enter some integer number: ");
// if the number is successfully parsed return number else run this method again
return int.TryParse(Console.ReadLine(), out num) ? num : EnterNumber();
}
public static void Main(string[] args)
{
int num = EnterNumber();
}
With all the options we have (methods, loops and recursion) there is no practical use case for using GO TOs anymore.
In general you use methods or lambda expressions. So your mainmenu would become a method that you call again at the end of the code.
One suggestion would be to use a switch case construct like this:
static void Main(string[] args)
{
string ConsoleReadinator;
string MENU_TEXT = "Main Menu:";
string ADDITIONAL_INFO = "Store or something";
bool endProg = false;
ConsoleReadinator = printMenu(MENU_TEXT, ADDITIONAL_INFO);
// as long "EXIT" is not typed
while (!endProg)
{
switch (ConsoleReadinator)
{
case "STORE":
// Do your Store Stuff
// maybe change MENU_TEXT and ADDITIONAL_INFO
// and print a new Menu
ConsoleReadinator = printMenu(MENU_TEXT, ADDITIONAL_INFO);
break;
case "GO":
// Do your Go Stuff
break;
case "EXIT":
endProg = true; // set exit condition to true
break;
default:
break;
}
}
Console.ReadKey();
}
// one Method to use for Menu display
public static string printMenu(string menuText, string additionalInfo)
{
Console.Clear();
Console.WriteLine(menuText);
Console.WriteLine(additionalInfo);
return Console.ReadLine().ToUpper();
}
I have created a game that gives a player 5 chances to play after which I would like to ask the player if they would like to play again or quit. I have seen it done in Python, but I do not know python. My code works perfectly fine, but I would like to add these two additional functions
How can I achieve these functionality in C#?
For reference this is what my code main class code looks like.
namespace NumBaseBall
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\t\t\t*************************************");
Console.WriteLine("\t\t\t* Let's Have Some Fun *");
Console.WriteLine("\t\t\t* Welcome To The *");
Console.WriteLine("\t\t\t* Number Baseball Game *");
Console.WriteLine("\t\t\t*************************************\n");
GameResults gameresults = new GameResults();
for (int trysCounter = 1; trysCounter <= 5; trysCounter++)
{
gameresults.Strikes = 0;
Random r = new Random();
var myRange = Enumerable.Range(1, 9);
var computerNumbers = myRange.OrderBy(i => r.Next()).Take(3).ToList();
Console.WriteLine("The Game's Three Random Integers Are: (Hidden from user)");
foreach (int integer in computerNumbers)
{
Console.WriteLine("{0}", integer);
}
List<int> playerNumbers = new List<int>();
Console.WriteLine("Please Enter Three Unique Single Digit Integers and Press ENTER after each:");
for (int i = 0; i < 3; i++)
{
Console.Write("");
int number = Convert.ToInt32(Console.ReadLine());
playerNumbers.Add(number);
}
gameresults.StrikesOrBalls(computerNumbers,playerNumbers);
Console.WriteLine("---> Computer's Numbers = {0}{1}{2}", computerNumbers[0], computerNumbers[1], computerNumbers[2]);
Console.WriteLine("---> Player's Numbers = {0}{1}{2}", playerNumbers[0], playerNumbers[1], playerNumbers[2]);
Console.WriteLine("---> Game Results = {0} STRIKES & {1} BALLS\n", gameresults.Strikes, gameresults.Balls);
Console.WriteLine("You have played this games {0} times\n", trysCounter);
gameresults.TotalStrikes = gameresults.TotalStrikes + gameresults.Strikes;
Console.WriteLine("STRIKES = {0} ", gameresults.TotalStrikes);
if (gameresults.TotalStrikes >= 3)
{
gameresults.Wins++;
Console.WriteLine("YOU ARE A WINNER!!!");
break;
}
}
if (gameresults.TotalStrikes <3)
Console.WriteLine("YOU LOSE :( PLEASE TRY AGAIN!");
}
}
}
Insert your code inside an loop which checks if the user wants to continue:
while(true) // Continue the game untill the user does want to anymore...
{
// Your original code or routine.
while(true) // Continue asking until a correct answer is given.
{
Console.Write("Do you want to play again [Y/N]?");
string answer = Console.ReadLine().ToUpper();
if (answer == "Y")
break; // Exit the inner while-loop and continue in the outer while loop.
if (answer == "N")
return; // Exit the Main-method.
}
}
But perhaps it would be better to split one big routine up into seperate routines.
Lets rename your Main-method to PlayTheGame.
Split up my routines into:
static public bool PlayAgain()
{
while(true) // Continue asking until a correct answer is given.
{
Console.Write("Do you want to play again [Y/N]?");
string answer = Console.ReadLine().ToUpper();
if (answer == "Y")
return true;
if (answer == "N")
return false;
}
}
And now the Main-method can be:
static void Main(string[] args)
{
do
{
PlayTheGame();
}
while(PlayAgain());
}
You'de have to move some local variables to the class as static fields. Or you could make an instance of a Game class, but I think that is one step to far right now.
There are two ways you could accomplish this:
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill%28v=vs.110%29.aspx
System.Diagnostics.Process.GetCurrentProcess().Kill();
Or
https://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.110).aspx
int exitCode =1;
System.Environment.Exit(exitCode);
Environment.Exit is the preferred way to exit your program since the Kill command "causes an abnormal process termination and should be used only when necessary."[msdn]
First, take the suggestion of moving the code for the actual game playing into a separate function. It will clean things up a lot.
Something like
private static bool PlayGame()
{
// Win branch returns true.
// Loss branch returns false.
}
This then lets you greatly simplify the Main function allowing it to only handle the menu functionality.
For the actual menu functionality, I tend to prefer do/while loops. You have a bit of an extra stipulation that you only ask after 5 plays, but that's easy enough to deal with.
static void Main(string[] args)
{
int playCount = 0;
string answer = "Y";
bool winner;
do
{
if(playCount < 5)
{
playCount++;
}
else
{
do
{
Console.Write("Play again? (Y/N): ");
answer = Console.ReadLine().ToUpper();
} while(answer != "Y" && answer != "N");
}
winner = PlayGame();
} while(!winner && answer == "Y");
Console.WriteLine("Thanks for playing!");
}
You could simplify it a bit by moving the test for 5 games into the if conditional with the use of an increment operator. The only issue is if someone plays your game a billion or so times, things might get weird.
static void Main(string[] args)
{
int playCount = 0;
string answer = "Y";
bool winner;
do
{
if(playCount++ > 3)
{
do
{
Console.Write("Play again? (Y/N): ");
answer = Console.ReadLine().ToUpper();
} while(answer != "Y" && answer != "N");
}
winner = PlayGame();
} while(!winner && answer == "Y");
Console.WriteLine("Thanks for playing!");
}
Edit: Changed things a bit as it looks like in your original code the game ends after the person wins the game.
Per your question in your comment below, you could make a static instance of your GameResults class in your Program class. Your code would end up looking something like the following
class Program
{
private static GameResults results = new GameResults();
public static void Main(string[] args)
{
// Code
}
private static bool PlayGame()
{
// Code
}
}
In PlayGame you would just use the static results object instead of creating a new one every time PlayGame is called.
pretty new to programming and I'm a bit stuck. This is the first program I've written with two classes (in two seperate class files). I'll include all the code below. It's a program to play a dice game called Craps. My task is too create a method in the CrapsGame class that allows me to play the game over and over again until I decide to stop. But I'm having a little trouble working out how to correctly call the manyPlay() method. I'm not really sure what I'm doing. The program will play the game once by calling myCraps.play() but then won't go any further. If anyone else notices anything wrong with the code or anything that's bad practice then please point it out as I am very keen to learn. Thanks to anyone that takes the time to answer.
using System;
namespace Task4_7
{
public class CrapsGame
{
string replay;
private Craps myCraps;
private CrapsGame newGame;
public static void Main()
{
CrapsGame newGame = new CrapsGame();
Craps myCraps = new Craps ();
myCraps.play ();
newGame.manyPlay ();
}
public void manyPlay() {
string input; // declare local variable
do {
myCraps.play();
replay:
Console.Write("Would you like to play again? y/n");
input = Console.ReadLine();
if (input == "y") {
replay = input;
}
else if (input == "n") {
replay = "n";
}
else {
Console.WriteLine("\n Erroneous input. Please enter y (yes) or n (no)");
goto replay;
}
}
while(replay != "n");
}
}
}
using System;
namespace Task4_7
{
public class Craps
{
private Random randy; // define randy as a Random class
public Craps() {
this.randy = new Random ();
}
public int oneThrow() {
return randy.Next(6) + 1; // pick a number from 1 to 6 and return this
}
public int throw2Dice() {
int a, b, c;
a = oneThrow ();
b = oneThrow ();
c = a + b;
Console.WriteLine ("You threw a " + a + " and a " + b + " making " + c);
return c;
}
public void play() {
int result = throw2Dice ();
switch (result) {
case 2:
Console.WriteLine ("You lose! End of game!");
break;
case 3:
Console.WriteLine ("You lose! End of game!");
break;
case 12:
Console.WriteLine ("You lose! End of game!");
break;
case 7:
Console.WriteLine ("You win! End of game!");
break;
case 11:
Console.WriteLine ("You win! End of game!");
break;
case 4:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
case 5:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
case 6:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
case 8:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
case 9:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
default:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
}
}
public void throwPoint(int result) {
Throw:
int a = throw2Dice();
if (a == result) {
Console.WriteLine ("You rolled the same score! You win!");
} else if (a == 7) {
Console.WriteLine ("You rolled a 7! You loose!");
} else {
Console.WriteLine ("You rolled a " + a + ". Rolling again!");
goto Throw;
}
}
}
}
This is your problem:
Craps myCraps = new Craps();
You are hiding variable of class CrapsGame with a local variable of Main method. Simply change it to
myCraps = new Craps();
and it should work.
EDIT
and change myCraps declaration to static, of course.
EDIT2
Scope of variables: C# variable scoping not consistent?
My first suggestion is put your Main method in a seperate file.For example you can use three files: Program.cs, CrapsGame.cs, Craps.cs. To do this just add a new class file,and move your main method to new Class file. Second, never use goto.Instead use while loop like this,
public void throwPoint(int result) {
while(true)
{
int a = throw2Dice();
if (a == result) {
Console.WriteLine ("You rolled the same score! You win!");
break;
} else if (a == 7) {
Console.WriteLine ("You rolled a 7! You loose!");
break;
} else {
Console.WriteLine ("You rolled a " + a + ". Rolling again!");
}
}
}
Many play method:
public void manyPlay() {
string input; // declare local variable
myCraps.play();
while(true) {
Console.Write("Would you like to play again? y/n");
input = Console.ReadLine();
if (input == "y") {
myCraps.play();
}
else if (input == "n") {
break;
}
else {
Console.WriteLine("\n Erroneous input. Please enter y (yes) or n (no)");
}
}
}
The problem looks like you where creating a game for use by the game iterator, but it needs to create a game for each iteration. So I delete some code: all the class variables have to go, variables should be as local as possible.
using System;
namespace Task4_7
{
public class CrapsGame
{
public static void Main()
{
new Craps ().play ();
new CrapsGame().manyPlay ();
}
public void manyPlay() {
string replay;
string input; // declare local variable
do {
new Craps().play();
replay:
Console.Write("Would you like to play again? y/n");
input = Console.ReadLine();
if (input == "y") {
replay = input;
}
else if (input == "n") {
replay = "n";
}
else {
Console.WriteLine("\n Erroneous input. Please enter y (yes) or n (no)");
goto replay;
}
}
while(replay != "n");
}
}
}
Now fix that loop: no gotos. You need a loop in a loop, as you have but don't build your own loops.
Then if Craps.play() returned a score. You could add a class variable to accumulate the score.
Change replay to boolean and make set it to true if input is y and false if input is n.
Then change your main method.
public static void Main()
{
CrapsGame newGame = new CrapsGame();
Craps myCraps = new Craps ();
while (replay == true) {
myCraps.play ();
newGame.manyPlay ();
}
}