ā Need help with a function
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string[] word = File.ReadAllLines("words.txt");
Random rnd = new Random();
int random = rnd.Next(1, 4);
char[] letters = word[random].ToCharArray();
int maxLives = 7;
int currentLives = maxLives;
bool win = false;
List<char> guessedLetters = new List<char>();
while (currentLives > 0 && !win)
{
foreach (var c in letters) { if (guessedLetters.Contains(c)) Console.Write(c); else Console.Write("_"); } //the prompt which asks you to guess and shows current lives Console.WriteLine("\nGuess a letter:"); Console.WriteLine(currentLives + "/" + maxLives + " lives left.");
//takes the input from the console and uses the ToChar to convert it to a single character static char PlayerInput() { char guess = Convert.ToChar(Console.ReadLine()); return guess; }
if (letters.Contains(PlayerInput()) && !guessedLetters.Contains(PlayerInput())) Console.WriteLine("Correct guess!"); else { Console.WriteLine("Incorrect guess!"); currentLives--; } guessedLetters.Add(PlayerInput()); bool wordComplete = true; foreach (var c in letters) if(!guessedLetters.Contains(c)) wordComplete = false;
win = wordComplete; } if(win) Console.WriteLine("You win!"); else { Console.WriteLine("You lost!"); }
foreach (var c in letters) { if (guessedLetters.Contains(c)) Console.Write(c); else Console.Write("_"); } //the prompt which asks you to guess and shows current lives Console.WriteLine("\nGuess a letter:"); Console.WriteLine(currentLives + "/" + maxLives + " lives left.");
//takes the input from the console and uses the ToChar to convert it to a single character static char PlayerInput() { char guess = Convert.ToChar(Console.ReadLine()); return guess; }
if (letters.Contains(PlayerInput()) && !guessedLetters.Contains(PlayerInput())) Console.WriteLine("Correct guess!"); else { Console.WriteLine("Incorrect guess!"); currentLives--; } guessedLetters.Add(PlayerInput()); bool wordComplete = true; foreach (var c in letters) if(!guessedLetters.Contains(c)) wordComplete = false;
win = wordComplete; } if(win) Console.WriteLine("You win!"); else { Console.WriteLine("You lost!"); }
21 Replies
when i try to input it requires me to do it multiple times before it accepts it
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/post it properly so its easier to read
oh okay thank you
BlazeBin - umldzrrcrnua
A tool for sharing your source code with the world!
think about what causes your program to ask the user for input
the problem is that you're getting asked for input too many times
why might that be happening?
im not sure at all i put the readline in a function because its a requirement for my code and its now not working
specifically, look at
well let's get sure
how many times do you use
PlayerInput()
?
and what happens each time you use it?ohh so its carrying the readline over
that makes a lot of sense now
?
its not carrying it over
thats whats causing the issue
instead of asking for input 3 times, do it once and save the result
what do i replace the other ones with though, originally i just had the guess variable on its own which worked fine but isnt it now only in the function?
sorry im a complete beginner at this and it's all so confusing to me
char guess = PlayerInput();
the function is its own separate thing. It probably gets really confusing for you since you declared it locallyi feel stupid for not thinking of that š
when you call a function, it runs the entire function
not just a part of it
š
thank you so much
you are a life saver
np
if the thread is "done", feel free to
/close
it šis there anyway to add a parameter to the function? i forgot that i needed one