C#如何将一个或多个字母与另一个字符串进行比较

时间:2017-09-30 21:06:13

标签: c# string search compare substring

所以我正在制作一个刽子手游戏来练习我的C#技能,但是我遇到了一个问题,我无法弄清楚如何循环随机选择的单词并比较文本框中的用户选择。我知道如何获取用户输入并将其转换为变量字符串。但不是如何比较它并删除*并将其替换为当前的字母。

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

namespace Guess_The_Word
{

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


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            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];

            char[] currentWordCharArray = currentWord.ToCharArray();

            foreach (char c in currentWordCharArray)
            {
                currentWord.Contains(userInputBox.Text);

                wordlbl.Text += "*";
            }



        }

        private void guessBtn_Click(object sender, EventArgs e)
        {

        }


        private void resetGamebtn_Click(object sender, EventArgs e)
        {


        }
    }
}

2 个答案:

答案 0 :(得分:1)

如果是这样的话:

char a = 'x';
char b = 'y';

然后你可以使用像if (a == b)这样的if语句,你可以使用这个结果作为输出,就像布尔的列表一样,正确的答案放置。

你需要循环currentWordCharArray并检查userInputCharArray(转换为ToCharArray)并且需要确保你没有检查出userInputCharArray。我的意思是currentWordCharArray可以有7个字母,你可以显示正确单词的字数,无论如何用户可以使用没有7个字母的输入字。毕竟收集了真实和愚蠢,以显示哪个字母是正确的。

编辑:

private void guessBtn_Click(object sender, EventArgs e)
    {
        char[] currentWordCharArray = currentWord.ToCharArray();
        char[] userGuessCharArray = userInputBox.Text.ToCharArray();
        int lengthOfAnswer = currentWordCharArray.Length;
        List<char> progressInformer = new List<char>();

        for (int i = 0; i < lengthOfAnswer; i++)
        {
            if (i < userGuessCharArray.Length)
            {
                if (currentWordCharArray[i] == userGuessCharArray[i])
                {
                    progressInformer.Add(currentWordCharArray[i]);
                }
                else
                {
                    progressInformer.Add('*');
                }
            }
            else
            {
                progressInformer.Add('*');
            }
        }
    }

你无法想象的循环就在上面。如果在正确的位置猜对了,progressInformer将包含真正的字母,否则为错误的字母,则为wild char(表示'*')。例如;如果您的系统选择'italic'作为单词并且用户猜到'italy',那么progressInformer将是一个char列表,​​类似于

List<char> progressInformer = new List<char> { 'i', 't', 'a', 'l', '*', '*' };

我解决方案的任何未解决的部分?

干杯!

答案 1 :(得分:1)

这是一个带有示例的控制台应用程序,您可以从这里构建。这不会为您替换*或者您打算做什么,但它会帮助您作为起点。

using System;

    public class Program
    {
        public static void Main()
        {
            string randomInput = "Apple";
            string userInput = "Apple";


            //Word lengths are not equal therefore user has lost already
            if(randomInput.Length != userInput.Length){
                Console.WriteLine("User has not guessed the word, your word is not the same size as the random one.");
                return;
            }


            char[] randomCharArray = randomInput.ToLowerInvariant().ToCharArray();
            char[] userInputArray = userInput.ToLowerInvariant().ToCharArray();

            for(int i = 0; i < randomCharArray.Length; i++)
            {
                if(randomCharArray[i] == userInputArray[i])
                    continue;
                else{
                    Console.WriteLine("You have not guessed the word, keep trying!");
                    return;
                }
            }

            Console.WriteLine(string.Format("You have won!, you have guessed the word {0}",userInput));

        }

    }