C
C#17mo ago
Masmoudi

Im a beginner

9 Replies
Masmoudi
MasmoudiOP17mo ago
so i am coding a hangman game but i cant figure out how to make it so when theres more than one of the same letter it puts the letter in all the places and not the first index it finds
Denis
Denis17mo ago
Share the code directly $code
MODiX
MODiX17mo 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/
Denis
Denis17mo ago
$codegif
Denis
Denis17mo ago
But from what you say it seems that you need to modify one of your loops to traverse all letters of the word and check if the letter provided by the user is present on the current loop index And.your loop should not stop until it reaches the end
Masmoudi
MasmoudiOP17mo ago
yeah i figured it would be something close to that but i have close to no idea how to
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hangman
{
class Program
{
static void Main(string[] args)
{
string CorrectWord = "mamamia";
String HiddenWord = "";
int lose = 0;


for (int i = 0; i<CorrectWord.Length; i++)
{
HiddenWord += "_";
}

for (int i = 0; i!=CorrectWord.Length;)
{
Console.WriteLine(HiddenWord);
Console.WriteLine(" Guess a letter");

char guess = Convert.ToChar(Console.ReadLine());
int index = CorrectWord.IndexOf(guess);

if (index != -1)
{
HiddenWord = HiddenWord.Remove(index, 1).Insert(index, guess.ToString());
i++;

}
else if(index == -1)
{
lose++;
}

}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hangman
{
class Program
{
static void Main(string[] args)
{
string CorrectWord = "mamamia";
String HiddenWord = "";
int lose = 0;


for (int i = 0; i<CorrectWord.Length; i++)
{
HiddenWord += "_";
}

for (int i = 0; i!=CorrectWord.Length;)
{
Console.WriteLine(HiddenWord);
Console.WriteLine(" Guess a letter");

char guess = Convert.ToChar(Console.ReadLine());
int index = CorrectWord.IndexOf(guess);

if (index != -1)
{
HiddenWord = HiddenWord.Remove(index, 1).Insert(index, guess.ToString());
i++;

}
else if(index == -1)
{
lose++;
}

}
}
}
}
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Masmoudi
MasmoudiOP17mo ago
oh ill work on that thanks

Did you find this page helpful?