C
C#15mo ago
Nomnomfighter

❔ ✅ While loop takes input but gives out output + two phantom outputs

working on a simple rock, paper, scissor game. After a few tries with if statements, I changed to switch statements, but both have the same problem. Code looks like this: https://paste.ofcode.org/TWijGyx4rzUSdgdVi4mWTS running it looks like this: https://gyazo.com/42a2a8891da011b6ca32ec5e2eaf49ff The two extra prompts + "invalid input" happen all the time aside from choosing 'e'. I assume that's because I've specifically put stopPlaying = true there. However, it shouldn't touch the default case when the input is valid and it definitely shouldn't repeat the prompt + default twice at all after processing the input. What am I missing here?
Gyazo
Gyazo
9 Replies
Thinker
Thinker15mo ago
(this actually took me a while) Okay so you're using Console.Read(), which reads one character at a time from the console's input stream Issue with that is that when it asks you to enter a character, you type a character + enter, which enters three characters: the character you typed, \r, and \n (which are the two newline characrers on Windows). You should probably use playerChoice = char.ToLower(Console.ReadLine()[0]) instead Which reads an entire line from the user and takes the first character from it
Nomnomfighter
NomnomfighterOP15mo ago
Ah, okay. Would .ReadKey work instead or what would that do?
Thinker
Thinker15mo ago
Yeah that too Console.ReadKey().KeyChar
Nomnomfighter
NomnomfighterOP15mo ago
I see. Also, since I'm new to switch cases, is there a way to branch them? I'm trying to see if I can be a bit more elegant than big if statements
Thinker
Thinker15mo ago
wdym branch them?
Nomnomfighter
NomnomfighterOP15mo ago
I know I can check multiple ifs at the same time with stuff like https://paste.ofcode.org/P6baM3DpBydj6gk9hu9aXD But is there a way to include that check against the computer in a switch statement?
Thinker
Thinker15mo ago
you could do this:
if ((playerChoice, cpuChoice) is ('r', 'p') or ('p', 's') or ('s', 'r'))
{
Console.WriteLine("You lost.");
loss++;
}
else
{
Console.WriteLine("You win!");
wins++;
}
if ((playerChoice, cpuChoice) is ('r', 'p') or ('p', 's') or ('s', 'r'))
{
Console.WriteLine("You lost.");
loss++;
}
else
{
Console.WriteLine("You win!");
wins++;
}
or if you want something a bit longer
switch (playerChoice, cpuChoice)
{
case ('r', 'p') or ('p', 's') or ('s', 'r'):
// or
case ('r', 'p'):
case ('p', 's'):
case ('s', 'r'):
Console.WriteLine("You lost.");
loss++;

default:
Console.WriteLine("You win!");
wins++;
}
switch (playerChoice, cpuChoice)
{
case ('r', 'p') or ('p', 's') or ('s', 'r'):
// or
case ('r', 'p'):
case ('p', 's'):
case ('s', 'r'):
Console.WriteLine("You lost.");
loss++;

default:
Console.WriteLine("You win!");
wins++;
}
Nomnomfighter
NomnomfighterOP15mo ago
Oh nice! Thank you! !close
Accord
Accord15mo ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server