我正在创建一个生成随机单词然后让用户猜测单词的游戏。在用户输入他的单词后,游戏将比较它们并返回正确的字母以及它们的位置是否正确。现在,当我输入相同的单词时,它会返回不同的结果。
这是我到目前为止所拥有的:
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;
}
}
}
我在我的表单中调用该方法
private void button1_Click(object sender, EventArgs e)
{
Game nc = new Game();
nc.InputWord = textBox1.Text;
nc.CheckWords(nc.InputWord);
}