-1

我正在创建一个生成随机单词然后让用户猜测单词的游戏。在用户输入他的单词后,游戏将比较它们并返回正确的字母以及它们的位置是否正确。现在,当我输入相同的单词时,它会返回不同的结果。

这是我到目前为止所拥有的:

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);



        }
4

3 回答 3

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();
        }
    }
于 2012-11-29T06:28:34.707 回答
0

两者namesname从未使用过,并且似乎不需要。

看起来你想要这样的东西(转换为控制台打印,因为我没有你的 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;
        }

    }
}
于 2012-11-29T06:34:36.553 回答
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循环调用,每次匹配时都会弹出。发生

于 2012-11-29T06:43:45.543 回答