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
(
as much context as possible. Avoid screenshots where possible, share code
was the important part)http://pastie.org/p/122CUPXHeVQKkMjlI2mKQR
that link has the code that isnt working
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
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
^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
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.
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.
do you think this kind of response is helpful to a beginner?
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
Explanation
Comment
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
It should be clear enough.
Getting input from user
Warning:
Console.ReadLine()
returns
- empty string if user just press enter.
- null
if user press ctrl+z
and enter.
Validating console input
Validates against null or empty string.
For invalid case, throwing an exception is an option among others.
TryParse<Options>()
tries to convert user input from string
to Options
.
Let computer choose a random Options
- 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.
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?
Continuation: (the last part)
Extension methods
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,
You can ask me later for the details.
Comparing computer and your choices
Asking again to play
This part should be clear enough.
Clearing screen
It should be clear enough.dude, please stop
you've written 3 full screens worth of nothing related to actually helping this person with their code
Please don't send DM @jIMMACLE 🙂
responding in public makes it look like you're trying to make it a problem
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.
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
Your code has two independent loops:
- 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
Using one do{}while();
loop
Actually you can use for()
loop too.
Using one for()
loop
The last option with unrecommended goto
.
If you still have any question, don't hesitate to ask for the details. No DM please. 🙂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 thingYour comments cause the mess actually.
This is your code with the most minimal modification. See you tomorrow.