找到刽子手所有遗失的字母? [英] Find all the missing letters of a word for hangman?

查看:90
本文介绍了找到刽子手所有遗失的字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直试图制作一个刽子手游戏,我几乎让游戏的主要部分发挥作用。但是玩家猜测这封信的部分工作不正常,我会尝试让这个更清楚:



当我按下一个正确的字母时,它会显示,但是当我按另一个*不同的,正确的字母时,那个显示但前一个字母消失了。我还使用两个或多个相同的字母进行了测试,当我输入正确的字母时,它只显示其中一个字母的位置。我不明白发生了什么,这是我到目前为止所得到的:



**更新2014年6月15日**

*我几乎让字母正常工作*,但代码仍然只添加一个正确字母的位置,即使在一个单词的不同位置有两个相同的字母。这是更新的代码:



I've been trying to make a hangman game lately, I've almost gotten the main part of the game to work. But the part where the player guesses the letter is not working properly, I'll try and make this clearer:

When I press one correct letter it shows, but then when I press another *different, correct letter, that one shows but the previous one disappears. I also tested out using two or more of the same letters and when I typed a correct letter, it only shows the position of one of them. I don't understand what's going on, here's what I got so far:

**UPDATE June 15, 2014**
*I've almost got the letters working properly*, but the code still only adds one position of a correct letter even if there are two of the same letters in different positions of a word. Here's the updated code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Randell_Hangman
{
    public partial class Form1 : Form
    {
        string[] brandnames = new string[1] { "abc" };
        Random word_shuffle = new Random();
        string word_select;
        int life_count;               
        int i = 0;

        int word_length;

        string w, x, y, z;

        int keypress_stage;

        int a_key;
        //copyright 2014 by Randell Angelo Acuram
        public Form1()
        {
            InitializeComponent();
            word_select = brandnames[word_shuffle.Next(0, brandnames.Length)];

            word_length = word_select.Length;

            life_count = 5;

            keypress_stage = 0;

            a_key = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            space_adder_timer.Start();

            life_line.Text = "Lives = " + life_count.ToString();

            w = word_select.Remove(0);

            word_display.Text = x.ToString();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //The keypress_stage variable prevents other letters from disappearing
            //when a different correct letter is pressed.
            life_line.Text = "Lives = " + life_count.ToString();
            #region Pressing A
            if (e.KeyData == Keys.A)
            {
                string letter_a = "a";
                if (word_select.Contains(letter_a) == true)
                {
                    int y1 = word_select.IndexOf("a");
                    
                    if (keypress_stage == 0)
                    {
                        y = x.Insert(y1, "a");
                        keypress_stage = 1;
                        a_key = 1;
                    }

                    else if (keypress_stage == 1)
                    {
                        y = y.Insert(y1, "a");
                        keypress_stage = 1;
                        a_key = 1;
                    }
                }

                if (word_select.Contains(letter_a) == false)
                {
                    life_count--;                   
                }

                if (life_count <= 0)
                {
                    MessageBox.Show("Game Over");
                    life_count = 0;
                    life_line.Text = "Lives = 0";
                }
            }
            if (e.KeyCode >= Keys.A)
            {
                if (a_key == 1)
                {
                    e.SuppressKeyPress = true;
                }
            }
            #endregion
            #region Pressing B
            if (e.KeyData == Keys.B)
            {
                string letter_b = "b";
                if (word_select.Contains(letter_b) == true)
                {
                    int y1 = word_select.IndexOf("b");

                    if (keypress_stage == 0)
                    {
                        y = x.Insert(y1, "b");
                        keypress_stage = 1;
                    }

                    if (keypress_stage == 1)
                    {
                        y = y.Insert(y1, "b");
                        keypress_stage = 1;
                    }
                }

                if (word_select.Contains(letter_b) == false)
                {
                    life_count--;
                }

                if (life_count <= 0)
                {
                    MessageBox.Show("Game Over");
                    life_count = 0;
                    life_line.Text = "Lives = 0";
                }
            }
            #endregion
            word_display.Text = y.ToString();
        }

        private void space_adder_timer_Tick(object sender, EventArgs e)
        {
            x += w.Insert(0, "_");

            if (x.Length == word_length)
            {
                space_adder_timer.Stop();
                word_display.Text = x.ToString();
            }
        }
    }
}

推荐答案

我会保留这个词包含对象的数组。该对象包含一个字母和一个布尔值。每次输入一个字母时,如果遇到正确的字母,则会遍历数组并将布尔值设置为true。如果最后没有设置为true,则添加一块图形。

只显示布尔值设置为true的字母。



例如。 FISH最初将成为一个数组:

{F,I,S,H}

{f,f,f,f}



然后用户输入字母'i'就变成:

{F,I,S,H}

{f,t ,f,f}



等...



希望这会有所帮助。
I would keep the word in an array which contains an object. That object contains one letter and one boolean. Each time you enter a letter you run through the array and set the booleans to true if it encounters the correct letter. If at the end nothing was set to true you add a piece of the drawing.
You only show the letters where the boolean was set to true.

eg. "FISH" would become an array initially:
{ F,I,S,H }
{ f,f,f,f }

then the user enters the letter 'i' it becomes:
{ F,I,S,H }
{ f,t,f,f }

etc...

hope this helps.


这篇关于找到刽子手所有遗失的字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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