I have this HangMan game and i got problems. When user clicks on alphabet button, it wont jump into that if-sentence which i got in my code. "Current" variable is a string and the button content is a letter and i want to know if that guessing word contains that letter, which user clicks on. Even if i debug that code, i don't get the "current" variable(guessing word)in that Guessing method. Thanks for all!
public sealed partial class Hangman : Page
{
private string[] words;
private int WrongGuesses = 0;
private string copyCurrent;
private string current;
public Hangman()
{
InitializeComponent();
loadWords();
DisplayTheWord();
Hangman_OnLoaded();
}
private void loadWords()
{
string[] ReadWords = File.ReadAllLines("EnglishWords.txt");
words = new string[ReadWords.Length];
}
private string[] images =
{
"/Assets/hang1.png", "/Assets/hang2.png", "/Assets/hang3.png",
"/Assets/hang4.png", "/Assets/hang5.png"
};
public void PlayAgain_OnClick(object sender, RoutedEventArgs e)
{
WrongGuesses = 0;
BitmapImage Hangman2 = new BitmapImage();
Uri URL = new Uri(BaseUri, images[WrongGuesses]);
Hangman2.UriSource = URL;
picture.Source = Hangman2;
string[] ReadWords = File.ReadAllLines("EnglishWords.txt");
int NextNumber = new Random().Next(words.Length);
copyCurrent = "";
for (int i = 0; i < ReadWords[NextNumber].Length; i++)
{
copyCurrent += "_" + " ";
}
CopiedWord.Text = copyCurrent;
}
public void DisplayTheWord()
{
WrongGuesses = 0;
BitmapImage Hangman2 = new BitmapImage();
Uri URL = new Uri(BaseUri, images[WrongGuesses]);
Hangman2.UriSource = URL;
picture.Source = Hangman2;
string[] ReadWords = File.ReadAllLines("EnglishWords.txt");
int NextNumber = new Random().Next(words.Length);
copyCurrent = "";
current = ReadWords[NextNumber];
for (int i = 0; i < ReadWords[NextNumber].Length; i++)
{
copyCurrent += "_" + " ";
}
CopiedWord.Text = copyCurrent;
}
public void Hangman_OnLoaded()
{
const int btnSize = 35;
var c = 0;
for (var i = 65; i <= 90; i++)
{
var btn = new Button {
Content = (char) i,
};
btn.Width = btn.Height = btnSize;
var margin = btn.Margin;
margin.Left = c += 37;
btn.Margin = margin;
GridMain.Children.Add(btn);
btn.Click += Guessing;
}
}
private void Guessing(object sender, RoutedEventArgs e)
{
Button choice = sender as Button;
var ltr = choice.Content.ToString();
if (current.Contains(ltr)) // it wont jump into this if sentence
{
char[] temp = copyCurrent.ToCharArray();
char[] find = current.ToCharArray();
char guessChar = ltr.ElementAt(0);
for (int index = 0; index < find.Length; index++)
{
if (find[index]== guessChar)
{
temp[index] = guessChar;
}
}
copyCurrent = new string(temp);
}
else
{
WrongGuesses++;
}
if (WrongGuesses < 6)
{
//picture.Source = muudab pilti
}
No jump is because your
currentvariable is null or it doesn't contain theItryou give.I have tested your code about getting
currentvalue and there is nothing wrong with the code itself. Butcurrentis a string that got from a random line fromEnglishWords.txt, if the random line has no content, it will contain nothing. So please check the text file if it has blank lines and the result ofcurrent = ReadWords[NextNumber];.