Im creating a game that generates a random word and then lets the user guess the word.
After the user enters his word the game will compare them and return what letter were correct and if they had the right position. Right now it return differents results when i input the same word.
This is what i have so far:
class Game
{
public string CorrectPlace;
public string WrongPlace;
public string NoneExist;
public string CorrectWord;
public string InputWord; // the word the uis
public int points;
public string[] Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" };
public string getRandomWord(string names)
{
Random ran = new Random();
return Wordarray[(ran.Next(0,Wordarray.Length-1))];
}
public void CheckWords(string name)
{
InputWord.ToCharArray();
CorrectWord = getRandomWord(CorrectWord); // store the random word in a string
for (int i = 0; i < CorrectWord.Length; i++)
{
if (InputWord[i] == CorrectWord[i])
MessageBox.Show("Letter #" + (i + 1).ToString() + " was correct!");
else
break;
}
}
}
Im calling the method in my form
private void button1_Click(object sender, EventArgs e)
{
Game nc = new Game();
nc.InputWord = textBox1.Text;
nc.CheckWords(nc.InputWord);
}
Try like this:
The idea behind the scene is to get a random word when game starts( an instance of class
Gameinstantiated) and then compare it with the user input inCheckWordsmethod.I would suggest to do not call
MessageBox.showin a loop, it will popup every time a match exists.occur