创建一个单词猜谜游戏 [英] Create a word guessing game

查看:103
本文介绍了创建一个单词猜谜游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对c#和学生都很陌生,所以请原谅我。我在Windows窗体中创建一个游戏。我必须创建一个包含5个单词的数组,然后单词猜测游戏应该选择5的随机单词。当程序启动时,第一个字母应该显示在lblSecretWord中。当用户在文本框中输入单词并单击按钮以检查单词是否正确然后不正确时,应显示下一个单词。例如,让我们说秘密词是爸爸。当程序启动时,D应显示用户猜测是否不正确,然后Da应显示,依此类推。如果用户最后没有猜到正确的消息框应该显示,但如果用户猜对了,消息框应该说你赢了。消息框我可以做我的随机功能我不能正确,我的循环不起作用所以我把它拿出来..plz帮助



我尝试了什么:



我创建了包含有效单词的数组。我有随机功能,但它没有正常工作或我输入错误的区域,我不确定。我可以在lblSecretWord中显示第一个字母,但不能显示下一个字母,依此类推。



名称空间GuessingGame_Pretest 
{
公共部分类Form1:表格
{
public Form1 ()
{
InitializeComponent();


}

随机随机= new Random();
string [] Word = new string [5] {Nicole,Kyle,Clayton,Jason,Wynand};



private void Form1_Load(object sender,EventArgs e)
{

// string s = Word [random.Next( 4)];

// char FirstLetter = s [1];
//lblSecretWord.Text = FirstLetter.ToString();

}

private void btnStartGame_Click(object sender,EventArgs e)
{
MessageBox.Show(Game have started);
btnCheckWord.Enabled = true;
txtGuessWord.Enabled = true;
lblstart.Text =猜猜这个词!;

string s = Word [random.Next(4)];

char FirstLetter = s [0];
lblSecretWord.Text = FirstLetter.ToString();


}


private void btnCheckWord_Click(object sender,EventArgs e)
{

char FirstLetter = s [0];
lblSecretWord.Text = FirstLetter.ToString();

if(txtGuessWord.Text ==Clayton)
{
MessageBox.Show(Word is Correct);
}
else
{
MessageBox.Show(Word不正确);
char SecondLetter = s [1];
lblSecretWord.Text =(FirstLetter.ToString()+ SecondLetter.ToString());
}

}

private void txtGuessWord_TextChanged(object sender,EventArgs e)
{

}

private void lblSecretWord_Click(object sender,EventArgs e)
{

}
}
}

解决方案

你应该跟踪当前的秘密词(例如,你可以在一个类成员变量中存储随机选择的索引)。

你也可以存储秘密字子串的当前长度。两个存储的值都应该在 btnCheckWord_Click 处理程序中使用:前者用于检查用户是否正确猜测,后者用于扩充提示子字符串(并最终确定用户是否丢失了游戏)。


您需要存储当前的单词和字母位置,以便它们可以在您的点击事件中使用



 //存储当前猜测的单词和当前的
//字母索引
私有字符串currentWord;
private int currentLetter;

private void btnStartGame_Click(object sender,EventArgs e)
{
MessageBox.Show(Game have started);
btnCheckWord.Enabled = true;
txtGuessWord.Enabled = true;
lblstart.Text =猜猜这个词!;

//得到单词使用
this.currentWord = Word [random.Next(Word.Length - 1)];

//从第0位开始
this.currentLetter = 0;

char letter = this.currentWord [currentLetter];

lblSecretWord.Text = letter.ToString();
}

private void btnCheckWord_Click(object sender,EventArgs e)
{
//检查猜测当前单词
if(txtGuessWord.Text == currentWord)
{
MessageBox.Show(Word is Correct);
}
else
{
MessageBox.Show(Word不正确);
//移动到下一个字母位置
this.currentLetter ++;
//从当前单词中获取字母
char letter = this.currentWord [this.currentLetter];
//将下一个字母添加到标签
lblSecretWord.Text + = letter.ToString();

// TODO:此代码需要更新,以便知道何时
//不再有字母并停止游戏
}
}


Hi I'm very new to c# and a student so please forgive me. I am creating a game in Windows Forms. I have to create an array with 5 words and then the Word Guessing Game should choose a random word of the 5. When the program starts the first letter should be display in the lblSecretWord. When the user enters a word into the textbox and clicks the button to check if the word is correct and then it is incorrect the next letter should be displayed. For instance lets say the secret word is Dad. When the program starts the "D" should show if the user guess incorrect then "Da" should show and so on. If the user did not guess correct at the end a message box should show up saying so, but if the user guesses correct, the message box should say you won. The message boxes I can do its my random function I cant get right and my for loop didn't work so I took it out..plz help

What I have tried:

I have created the array with the words that works. I have the random function but its not working correctly or I typed it into the wrong area I'm not sure. I can show the first letter in the lblSecretWord but cant show the next one and so on.

namespace GuessingGame_Pretest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            

        }

        Random random = new Random();
        string[] Word = new string[5] { "Nicole", "Kyle", "Clayton", "Jason", "Wynand" };
      


        private void Form1_Load(object sender, EventArgs e)
        {

            //string s = Word[random.Next(4)];

            // char FirstLetter = s[1];
            //lblSecretWord.Text = FirstLetter.ToString();

        }

        private void btnStartGame_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Game has started");
            btnCheckWord.Enabled = true;
            txtGuessWord.Enabled = true;
            lblstart.Text = "Guess the word!";

            string s = Word[random.Next(4)];

            char FirstLetter = s[0];
            lblSecretWord.Text = FirstLetter.ToString();

            
        }

        
        private void btnCheckWord_Click(object sender, EventArgs e)
        {
            
            char FirstLetter = s[0];
            lblSecretWord.Text = FirstLetter.ToString();

            if (txtGuessWord.Text == "Clayton")
            {
                MessageBox.Show("Word is Correct");
            }
            else
            {
                MessageBox.Show("Word is incorrect");
                char SecondLetter = s[1];
                lblSecretWord.Text = (FirstLetter.ToString() + SecondLetter.ToString());
            }

        }

        private void txtGuessWord_TextChanged(object sender, EventArgs e)
        {

        }

        private void lblSecretWord_Click(object sender, EventArgs e)
        {
            
        }
    }
}

解决方案

You should keep track of the current secret word (for instance, you could store in a class member variable the randomly selected index).
You could also store the current length of the secret word substring. Both stored values should be used in the btnCheckWord_Click handler: the former to check if the user guessed correctly, the latter for augmenting the hint substring (and eventually establishing if the user lost the game).


You need to store the current word and letter position so they can be used in your click event

// store the current word being guessed and the current
// letter index
private string currentWord;
private int currentLetter;

private void btnStartGame_Click(object sender, EventArgs e)
{
    MessageBox.Show("Game has started");
    btnCheckWord.Enabled = true;
    txtGuessWord.Enabled = true;
    lblstart.Text = "Guess the word!";

    // get the word to use
    this.currentWord = Word[random.Next(Word.Length - 1)];

    // start at position 0
    this.currentLetter = 0;

    char letter = this.currentWord[currentLetter];

    lblSecretWord.Text = letter.ToString();
}

private void btnCheckWord_Click(object sender, EventArgs e)
{
    // check guess against current word
    if (txtGuessWord.Text == currentWord)
    {
        MessageBox.Show("Word is Correct");
    }
    else
    {
        MessageBox.Show("Word is incorrect");
        // move to next letter position
        this.currentLetter++;
        // get the letter from the current word
        char letter = this.currentWord[this.currentLetter];
        // add the next letter to the label
        lblSecretWord.Text += letter.ToString();

        // TODO: This code needs updated so it knows when there
        // are no more letters left and stop the game
    }
}


这篇关于创建一个单词猜谜游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆