C
C#ā€¢4w ago
Blippet

āœ… 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!"); }
21 Replies
Blippet
BlippetOPā€¢4w ago
when i try to input it requires me to do it multiple times before it accepts it
Pobiega
Pobiegaā€¢4w ago
$code
MODiX
MODiXā€¢4w ago
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/
Pobiega
Pobiegaā€¢4w ago
post it properly so its easier to read
Blippet
BlippetOPā€¢4w ago
oh okay thank you
Blippet
BlippetOPā€¢4w ago
BlazeBin - umldzrrcrnua
A tool for sharing your source code with the world!
Pobiega
Pobiegaā€¢4w ago
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!");
}
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!");
}
mg
mgā€¢4w ago
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?
Blippet
BlippetOPā€¢4w ago
im not sure at all i put the readline in a function because its a requirement for my code and its now not working
Pobiega
Pobiegaā€¢4w ago
specifically, look at
if (letters.Contains(PlayerInput()) && !guessedLetters.Contains(PlayerInput()))
Console.WriteLine("Correct guess!");
else
{
Console.WriteLine("Incorrect guess!");
currentLives--;
}
guessedLetters.Add(PlayerInput());
if (letters.Contains(PlayerInput()) && !guessedLetters.Contains(PlayerInput()))
Console.WriteLine("Correct guess!");
else
{
Console.WriteLine("Incorrect guess!");
currentLives--;
}
guessedLetters.Add(PlayerInput());
mg
mgā€¢4w ago
well let's get sure
Pobiega
Pobiegaā€¢4w ago
how many times do you use PlayerInput()? and what happens each time you use it?
Blippet
BlippetOPā€¢4w ago
ohh so its carrying the readline over that makes a lot of sense now
Pobiega
Pobiegaā€¢4w ago
? its not carrying it over thats whats causing the issue instead of asking for input 3 times, do it once and save the result
Blippet
BlippetOPā€¢4w ago
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
Pobiega
Pobiegaā€¢4w ago
char guess = PlayerInput(); the function is its own separate thing. It probably gets really confusing for you since you declared it locally
Blippet
BlippetOPā€¢4w ago
i feel stupid for not thinking of that šŸ˜­
Pobiega
Pobiegaā€¢4w ago
when you call a function, it runs the entire function not just a part of it šŸ™‚
Blippet
BlippetOPā€¢4w ago
thank you so much you are a life saver
Pobiega
Pobiegaā€¢4w ago
np if the thread is "done", feel free to /close it šŸ™‚
Blippet
BlippetOPā€¢4w ago
is there anyway to add a parameter to the function? i forgot that i needed one

Did you find this page helpful?