C#Hangman with Bugs

时间:2017-10-02 16:28:11

标签: c#

当我做出一个空洞的猜测时,我得到一个超出范围的索引,我也有更新我的分数标签的问题。目前它将进行更新但不在表单上执行。我还想确保用户不能继续输入有效的信件以获得更多积分,请帮助我们回到学习c#和这样的项目帮助我学习

namespace Guess_The_Word
{    
    public partial class Form1 : Form
    {
        private int wrongGuesses = 0;
        private int userGuesses;
        private int score = 0;
        private string secretWord = String.Empty;
        private string[] words;
        private string currentWord = string.Empty;
        private string userGuess = string.Empty;
        private string userInput = string.Empty;
        private string randomInput = string.Empty;


        public Form1()
        {
            InitializeComponent();
        }

        private void guessBtn_Click(object sender, EventArgs e)
        {
            string guess = userInputBox.Text.ToString();
            char[] randomCharArray = currentWord.ToLowerInvariant().ToCharArray();
            char[] userInputArray = guess.ToLowerInvariant().ToCharArray();
            //Assume that userInput would never be superior than randomCharArray
            //And contain only one char
            for (int i = 0; i < randomCharArray.Length; i++)
            {
                if (userInputArray[0].Equals(randomCharArray[i])) // Here is where the error happens
                {
                    UpdateScore();
                }
            }
            // Clean userInput in form
            userInputBox.Text = string.Empty;

        }


        private void resetGamebtn_Click(object sender, EventArgs e)
        {
            SetUpWords();    
        }

        private void SetUpWords()
        {
            string path = (@"C:\commonwords.txt"); // Save the variable path with the path to the txt file
            words = File.ReadAllLines(path);
            int guessIndex = (new Random()).Next(words.Length);
            currentWord = words[guessIndex];
            wordlbl.Text = string.Empty;
            for (int i = 0; i < currentWord.Length; i++)
            {

                wordlbl.Text += "*";

            }
        }

        private void UpdateScore()
        {
            scorelbl.Text = Convert.ToString(score);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

确保在尝试访问特定索引(无论是否已知)之前检查数组的长度。下面的例子对逻辑有点过分,但它彻底地证明了前面的陈述。

if (userInputArray.Length > 0 && userInputArray.Length > i)
    if (randomCharArray.Length > 0 && randomCharArray.Length > i)
        if (userInputArray[0].Equals(randomCharArray[i]))
            UpdateScore();

private void UpdateScore() {
    score++;
    scorelbl.Text = Convert.ToString(score);
}

我去了.NET Fiddle并进行了基本操作,没有使用数组的所有额外复杂性,我返回了一些非常好的结果。下面示例中的逻辑非常基本,目前只处理当前只包含每个字母的单词。你必须从这里扩展它以允许处理诸如“banana”或“apple”之类的单词中的多个字母。 希望这会让你在项目中朝着正确的方向前进,我希望上面的答案能够告诉你在尝试访问它们的索引之前需要了解的关于检查数组长度的知识。

using System;
using System.Collections.Generic;

public class Program
{
    private static int score = 0;
    private static string currentWord = string.Empty;
    private static char userGuess = '`';
    private static List<char> correctLetters = new List<char>();

    public static void Main()
    {
        currentWord = "are";

        do {
            Console.WriteLine("Enter a letter: ");
            userGuess = Console.ReadLine()[0];
        } while (TestInput());

        Console.WriteLine("The word was: " + currentWord);
        Console.ReadLine();
    }

    private static bool TestInput() {
        if (currentWord.ToLowerInvariant().Contains(userGuess.ToString().ToLowerInvariant())) {
            correctLetters.Add(userGuess);
            score++;
            Console.WriteLine("Current Score: " + score);
        }

        if (correctLetters.Count == currentWord.Length)
            return false;

        return true;
    }
}