我创建了一个生成随机单词然后让用户猜出单词的游戏。 在用户输入他的单词后,游戏将比较它们并返回正确的字母以及它们是否具有正确的位置。现在,当我输入相同的单词时,它会返回不同的结果。
这是我到目前为止所做的:
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);
}
答案 0 :(得分:0)
像这样更改代码。如果您有任何问题,请告诉我。
课程游戏 { public string CorrectWord = null;
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()
{
Random ran = new Random();
return Wordarray[(ran.Next(0, Wordarray.Length - 1))];
}
public void Newgame()
{
}
public void CheckWords(string name)
{
char[] charInputWord = name.ToCharArray();
CorrectWord = getRandomWord(); // store the random word in a string
Console.WriteLine("Random Word " + CorrectWord);
Console.WriteLine("User Word " + name);
char[] charCorrectWord = CorrectWord.ToCharArray();
for (int i = 0; i < charInputWord.Length; i++)
{
if (charInputWord[i] == charCorrectWord[i])
Console.WriteLine("Letter #" + (i + 1).ToString() + " was correct!");
else
break;
}
}
}
class Program
{
static void Main(string[] args)
{
Game ab = new Game();
ab.CheckWords("raser");
Console.ReadLine();
}
}
答案 1 :(得分:0)
从不使用names
和name
,似乎不需要。
看起来你想要这样的东西(转换为控制台打印,因为我没有你的UI源)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WordGame
{
class Game
{
static void Main(string[] args)
{
Game g = new Game();
bool gameOver = false;
while (!gameOver)
{
Console.WriteLine("Enter a guess (or type 'exit' to exit):");
string answer = Console.ReadLine();
if (answer.ToLower() == "exit")
break;
if (g.CheckWord(answer))
{
Console.WriteLine("You win!");
while (true)
{
Console.WriteLine("Play Again (y/n)?");
answer = Console.ReadLine().ToLower();
if (answer == "n")
{
gameOver = true;
break;
}
else if (answer == "y")
{
g.ChooseRandomWord();
break;
}
}
}
}
}
public string CorrectWord;
public string[] Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" };
private Random ran = new Random();
public Game()
{
ChooseRandomWord();
}
public void ChooseRandomWord()
{
CorrectWord = Wordarray[(ran.Next(0, Wordarray.Length - 1))];
}
public bool CheckWord(string guess)
{
if (guess.Trim() == CorrectWord)
{
return true;
}
string guessLower = guess.Trim().ToLower();
string correctLower = CorrectWord.ToLower();
for (int i = 0; i < correctLower.Length; i++)
{
if (guessLower[i] == correctLower[i])
Console.Write(guess[i]);
else
Console.Write("#");
}
Console.WriteLine();
return false;
}
}
}
答案 2 :(得分:0)
试试这样:
class Game
{
public string CorrectPlace;
public string WrongPlace;
public string NoneExist;
public string CorrectWord;
public string InputWord;
public int points;
public string[] Wordarray = null;
public string getRandomWord(string names)
{
Random ran = new Random();
return Wordarray[(ran.Next(0,Wordarray.Length-1))];
}
public void Game()
{
Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" }
CorrectWord = getRandomWord(); // store the random word in a string
}
public void CheckWords(string name)
{
for (int i = 0; i < CorrectWord.Length; i++)
{
if (InputWord[i] == CorrectWord[i])
MessageBox.Show("Letter #" + (i + 1).ToString() + " was correct!");
else
break;
}
}
}
场景背后的想法是在游戏开始时获取一个随机单词(实例化类Game
的实例),然后将其与CheckWords
方法中的用户输入进行比较。
我建议不要在循环中调用MessageBox.show
,每次匹配时都会弹出。
发生