C
C#•10mo ago
skib

Practicing basics of c#

im tryna practice my coding basics with c# and made a rock paper scissors game. It works perfectly fine but I can't seem to get the part of the code which allows the user to play again to work. Please let me know if you can help.
22 Replies
Jimmacle
Jimmacle•10mo ago
(as much context as possible. Avoid screenshots where possible, share code was the important part)
skib
skibOP•10mo ago
http://pastie.org/p/122CUPXHeVQKkMjlI2mKQR that link has the code that isnt working
Jimmacle
Jimmacle•10mo ago
so if that code isn't running, the problem is in other code share the full program for as much context as possible and say exactly what "not working" means
skib
skibOP•10mo ago
my goal is to make it so that the player can choose whether or not they want to continue playing or stop playing, and I coded that section so they could do just that
skib
skibOP•10mo ago
skib
skibOP•10mo ago
^that is all the code it also has some issue sometimes when I select rock, paper, or scissors it runs three instances of me choosing whatever i chose
i like chatgpt
i like chatgpt•10mo ago
To figure out your "mistake", navigate to https://discord.com/channels/143867839282020352/1203165475337535559/1203251951660175451. I remade your code to be much simpler as follows. Use it as comparison and future learning. 🙂 You just need to adjust the print messages. Explanations are given below this because Discord does not allow me to post too long contents.
do
{
// Play against computer section.
Console.WriteLine("Select one of a set { paper, scissors, rock }.");
string? input = Console.ReadLine();

if (string.IsNullOrEmpty(input))
throw new Exception("Input is null or empty");
if (!Enum.TryParse<Options>(input, true, out var option))
throw new Exception("Input is not a valid option");

Options other = (Options)Random.Shared.Next(0, Enum.GetNames<Options>().Count());

bool? result = option.DoesWinAgainst(other);
string status = result is null ? "draws" : (result.Value ? "wins" : "loses");
Console.WriteLine($"Your {option} {status} against computer's {other}");

// Asking to play again?
Console.WriteLine("\nType again to play again.\nAny other input to exit.");
input = Console.ReadLine();

if (string.IsNullOrEmpty(input)) return;
if (input.Trim().ToLower() != "again") return;

Console.Clear();

} while (true);

static class OptionsExtensions
{
public static bool? DoesWinAgainst(this Options option, Options other)
{
switch (other - option)
{
case 0: return null;
case -1: case 2: return true;
default: return false;
}
}
}
enum Options { Paper, Scissors, Rock } // Caution: Don't randomize the element order again because the above code uses this convention!
do
{
// Play against computer section.
Console.WriteLine("Select one of a set { paper, scissors, rock }.");
string? input = Console.ReadLine();

if (string.IsNullOrEmpty(input))
throw new Exception("Input is null or empty");
if (!Enum.TryParse<Options>(input, true, out var option))
throw new Exception("Input is not a valid option");

Options other = (Options)Random.Shared.Next(0, Enum.GetNames<Options>().Count());

bool? result = option.DoesWinAgainst(other);
string status = result is null ? "draws" : (result.Value ? "wins" : "loses");
Console.WriteLine($"Your {option} {status} against computer's {other}");

// Asking to play again?
Console.WriteLine("\nType again to play again.\nAny other input to exit.");
input = Console.ReadLine();

if (string.IsNullOrEmpty(input)) return;
if (input.Trim().ToLower() != "again") return;

Console.Clear();

} while (true);

static class OptionsExtensions
{
public static bool? DoesWinAgainst(this Options option, Options other)
{
switch (other - option)
{
case 0: return null;
case -1: case 2: return true;
default: return false;
}
}
}
enum Options { Paper, Scissors, Rock } // Caution: Don't randomize the element order again because the above code uses this convention!
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
Jimmacle
Jimmacle•10mo ago
do you think this kind of response is helpful to a beginner?
Jimmacle
Jimmacle•10mo ago
it doesn't, really the point of this channel is to guide people to solving their problems, not to throw an entirely different solution at them with no explanation
i like chatgpt
i like chatgpt•10mo ago
Explanation Comment
// Play against computer section.
// Play against computer section.
Even though adding comments does not affect the binary output, we should use them sparingly. Unnecessary comments just add distraction and cognitive overload. Printing on screen
Console.WriteLine("Select one of a set { paper, scissors, rock }.");
Console.WriteLine("Select one of a set { paper, scissors, rock }.");
It should be clear enough. Getting input from user
string? input = Console.ReadLine();
string? input = Console.ReadLine();
Warning: Console.ReadLine() returns - empty string if user just press enter. - null if user press ctrl+z and enter. Validating console input
if (string.IsNullOrEmpty(input))
throw new Exception("Input is null or empty");
if (string.IsNullOrEmpty(input))
throw new Exception("Input is null or empty");
Validates against null or empty string. For invalid case, throwing an exception is an option among others.
if (!Enum.TryParse<Options>(input, true, out var option))
throw new Exception("Input is not a valid option");
if (!Enum.TryParse<Options>(input, true, out var option))
throw new Exception("Input is not a valid option");
TryParse<Options>() tries to convert user input from string to Options. Let computer choose a random Options
Options other = (Options)Random.Shared.Next(0, Enum.GetNames<Options>().Count());
Options other = (Options)Random.Shared.Next(0, Enum.GetNames<Options>().Count());
- Enum.GetNames<Options>().Count() returns the number of elements in Options. - Random.Shared.Next(0, N) returns a random integer between 0 inclusive and N exclusive. - (Options) x casts an integer x to Options. Define enum We can use enum for a list of options that never change.
enum Options { Paper, Scissors, Rock }
enum Options { Paper, Scissors, Rock }
Jimmacle
Jimmacle•10mo ago
i didn't ask you to explain your code help with OP's code instead of writing your own version of a solution for them can you explain exactly what isn't working? for example, what output are you expecting to see and not seeing? what steps do you take to make the issue happen? the issue comes down to how you're using loops - consider your first loop, are there any paths that actually lead to it looping? how would the second loop result in the first loop running again?
i like chatgpt
i like chatgpt•10mo ago
Continuation: (the last part) Extension methods
static class OptionsExtensions
{
public static bool? DoesWinAgainst(this Options option, Options other)
{
switch (other - option)
{
case 0: return null;
case -1: case 2: return true;
default: return false;
}
}
}
static class OptionsExtensions
{
public static bool? DoesWinAgainst(this Options option, Options other)
{
switch (other - option)
{
case 0: return null;
case -1: case 2: return true;
default: return false;
}
}
}
Extension methods can be used to "insert" or "add" extra methods to the existing types. In this example, I want to add DoesWinAgainst to Options type. The syntax is quite simple,
static class TypeToBeExtendedExtensions // this class must be static
{
public static WhateverTypeYouWantToReturn WhatEverMethodName(this WhateverTypeYouWantToExtend t, ....)
{
// do whatever you want to do
}
}
static class TypeToBeExtendedExtensions // this class must be static
{
public static WhateverTypeYouWantToReturn WhatEverMethodName(this WhateverTypeYouWantToExtend t, ....)
{
// do whatever you want to do
}
}
You can ask me later for the details. Comparing computer and your choices
bool? result = option.DoesWinAgainst(other);
string status = result is null ? "draws" : (result.Value ? "wins" : "loses");
Console.WriteLine($"Your {option} {status} against computer's {other}");
bool? result = option.DoesWinAgainst(other);
string status = result is null ? "draws" : (result.Value ? "wins" : "loses");
Console.WriteLine($"Your {option} {status} against computer's {other}");
Asking again to play This part should be clear enough. Clearing screen
Console.Clear();
Console.Clear();
It should be clear enough.
Jimmacle
Jimmacle•10mo ago
dude, please stop you've written 3 full screens worth of nothing related to actually helping this person with their code
i like chatgpt
i like chatgpt•10mo ago
Please don't send DM @jIMMACLE 🙂
Jimmacle
Jimmacle•10mo ago
responding in public makes it look like you're trying to make it a problem
i like chatgpt
i like chatgpt•10mo ago
I haven't read it yet. I just don't want to read DM. OK. Lets focus on OP question. I am trying to figure out his/her error now.
Jimmacle
Jimmacle•10mo ago
there is nothing else to say until OP responds to my question the goal is not to fix it for them, it's to teach them how to fix it themselves
i like chatgpt
i like chatgpt•10mo ago
Your code has two independent loops:
while(playAgain)
{
//...
playAgain = false;
}
while(!playAgain)
{
//...
}
while(playAgain)
{
//...
playAgain = false;
}
while(!playAgain)
{
//...
}
- The first while loop will never be executed after playAgain is set to false. - No matter the value of playAgain you set in the second while, the first while will never be executed again. goto can be used to link but it is not recommended. To fix it, use only one while() or one do{}while(). Using one while() loop
// YOUR INITIALIZATION GOES HERE
while(playAgain)
{
// YOUR LENGTHY CODE GOES HERE
Console.WriteLine("Would you like to continue? Type Y or N:");
response = Console.ReadLine();
if (response == "Y" || response == "y")
{
playAgain = true;
}
else if (response == "N" || response == "n")
{
// ...
playAgain = false; // MY MODIFICATION
}
}
// YOUR INITIALIZATION GOES HERE
while(playAgain)
{
// YOUR LENGTHY CODE GOES HERE
Console.WriteLine("Would you like to continue? Type Y or N:");
response = Console.ReadLine();
if (response == "Y" || response == "y")
{
playAgain = true;
}
else if (response == "N" || response == "n")
{
// ...
playAgain = false; // MY MODIFICATION
}
}
Using one do{}while(); loop
// YOUR INITIALIZATION GOES HERE
do // MY MODIFICATION
{
// YOUR LENGTHY CODE GOES HERE
Console.WriteLine("Would you like to continue? Type Y or N:");
response = Console.ReadLine();
if (response == "Y" || response == "y")
{
playAgain = true;
}
else if (response == "N" || response == "n")
{
// ...
playAgain = false; // MY MODIFICATION
}

} while (playAgain); // MY MODIFICATION
// YOUR INITIALIZATION GOES HERE
do // MY MODIFICATION
{
// YOUR LENGTHY CODE GOES HERE
Console.WriteLine("Would you like to continue? Type Y or N:");
response = Console.ReadLine();
if (response == "Y" || response == "y")
{
playAgain = true;
}
else if (response == "N" || response == "n")
{
// ...
playAgain = false; // MY MODIFICATION
}

} while (playAgain); // MY MODIFICATION
Actually you can use for() loop too. Using one for() loop
// move bool playAgain = true; to for(...)
// YOUR INITIALIZATION GOES HERE
for (bool playAgain = true; playAgain;) // MY MODIFICATION
{
// YOUR LENGTHY CODE GOES HERE
Console.WriteLine("Would you like to continue? Type Y or N:");
response = Console.ReadLine();
if (response == "Y" || response == "y")
{
playAgain = true;
}
else if (response == "N" || response == "n")
{
// ...
playAgain = false; // MY MODIFICATION
}
}
// move bool playAgain = true; to for(...)
// YOUR INITIALIZATION GOES HERE
for (bool playAgain = true; playAgain;) // MY MODIFICATION
{
// YOUR LENGTHY CODE GOES HERE
Console.WriteLine("Would you like to continue? Type Y or N:");
response = Console.ReadLine();
if (response == "Y" || response == "y")
{
playAgain = true;
}
else if (response == "N" || response == "n")
{
// ...
playAgain = false; // MY MODIFICATION
}
}
The last option with unrecommended goto. If you still have any question, don't hesitate to ask for the details. No DM please. 🙂
Jimmacle
Jimmacle•10mo ago
The first while loop continue looping because playAgain is kept true. this is wrong please stop being confusing and misleading just... stop they need to respond in order for there to be anything else to talk about i asked OP appropriate questions to get them to look their code a specific way, you are not adding additional useful information and are in fact misleading them saying the issue is one that doens't exist this whole thread has become a mess because you're just doing your own thing
i like chatgpt
i like chatgpt•10mo ago
Your comments cause the mess actually.
i like chatgpt
i like chatgpt•10mo ago
This is your code with the most minimal modification. See you tomorrow.
Want results from more Discord servers?
Add your server