如何使用C#对句子中的单词进行置乱并放入数组

如何使用C#对句子中的单词进行置乱并放入数组,c#,arrays,unity3d,scramble,C#,Arrays,Unity3d,Scramble,我需要帮助修复我的代码,从我得到的教训,我试图创建简单的脚本,在句子中,如“房子坏了”变成了“房子坏了”。我的代码工作,但它只有一个字,如“的”变成了“H.T.E”, 我尝试使用string.split方法,但我不明白我必须在哪里将代码更改为数组。 这是我的代码,结果是 using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; [System.Seri

我需要帮助修复我的代码,从我得到的教训,我试图创建简单的脚本,在句子中,如“房子坏了”变成了“房子坏了”。我的代码工作,但它只有一个字,如“的”变成了“H.T.E”, 我尝试使用string.split方法,但我不明白我必须在哪里将代码更改为数组。 这是我的代码,结果是

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

[System.Serializable]
public class Word
{
    public string word;

    [Header("biarkan kosong untuk acak otomatis")]
    public string desiredRandom;

    public string GetString()
    {
        if (!string.IsNullOrEmpty(desiredRandom))
        {
            return desiredRandom;
        }

        string result = word;

        // **I'm try to split string here where i try to input that into array**
        string[] array = result.Split(' ');
        foreach (string token in array)
        {
            Debug.Log((token).ToString());
            word = token;
        }

        result = "";

        List<char> characters = new List<char>(word.ToCharArray());
        while (characters.Count > 0)
        {
            int indexChar = Random.Range(0, characters.Count - 1);
            result += characters[indexChar];
            characters.RemoveAt(indexChar);
        }
        return result;
    }
}

public class WordScramble : MonoBehaviour
{
    public Word[] words;

    [Header("UI Reference")]
    public CharObject prefab;
    public Transform container;
    public float space;
    public float lerpSpeed = 5;

    List<CharObject> charObjects = new List<CharObject>();
    CharObject firstSelected;

    public int currentWord;
    public static WordScramble main;

    void Awake()
    {
        main = this;
    }

    // Use this for initialization
    void Start()
    {
        ShowScramble(currentWord);
    }

    // Update is called once per frame
    void Update()
    {
        RepositionObject();
    }

    void RepositionObject()
    {
        if (charObjects.Count == 0)
        {
            return;
        }

        float center = (charObjects.Count - 1) / 2;
        for (int i = 0; i < charObjects.Count; i++)
        {
            charObjects[i].rectTransform.anchoredPosition
                = Vector2.Lerp(charObjects[i].rectTransform.anchoredPosition,
                new Vector2((i - center) * space, 0), lerpSpeed * Time.deltaTime);
            charObjects[i].index = i;
        }
    }

    public void ShowScramble()
    {
        ShowScramble(Random.Range(0, words.Length - 1));
    }

    public void ShowScramble(int index)
    {
        charObjects.Clear();
        foreach (Transform child in container)
        {
            Destroy(child.gameObject);
        }

        if (index > words.Length - 1)
        {
            Debug.LogError("index out of range between 0-" + (words.Length - 1).ToString());
            return;
        }

        // string result = huruf ;

        //  foreach (string words is word());

        char[] chars = words[index].GetString().ToCharArray();

        foreach (char c in chars)
        {
            CharObject clone = Instantiate(prefab.gameObject).GetComponent<CharObject>();
            clone.transform.SetParent(container);
            charObjects.Add(clone.Init(c));
        }
        currentWord = index;
    }

    public void Swap(int indexA, int indexB)
    {
        CharObject tmpA = charObjects[indexA];
        charObjects[indexA] = charObjects[indexB];

        charObjects[indexB] = tmpA;

        charObjects[indexA].transform.SetAsLastSibling();
        charObjects[indexB].transform.SetAsLastSibling();

        CheckWord();
    }

    public void Select(CharObject charObject)
    {
        if (firstSelected)
        {
            Swap(firstSelected.index, charObject.index);

            // unselect
            //firstSelected = null;
            firstSelected.Select();
            charObject.Select();
        }
        else
        {
            firstSelected = charObject;
        }
    }

    public void UnSelect()
    {
        firstSelected = null;
    }

    public void CheckWord()
    {
        StartCoroutine(CoCheckWord());
    }

    IEnumerator CoCheckWord()
    {
        yield return new WaitForSeconds(0.5f);

        string word = "";
        foreach (CharObject charObject in charObjects)
        {
            word += charObject.character;
        }

        if (word == words[currentWord].word)
        {
            currentWord++;
            ShowScramble(currentWord);
        }
    }
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine.UI;
[系统可序列化]
公共类词
{
公共字符串;
[页眉(“将科松的头发剪成两半”)]
公共字符串所需的随机数;
公共字符串GetString()
{
如果(!string.IsNullOrEmpty(desiredRandom))
{
返回所需的随机数;
}
字符串结果=字;
//**我尝试在此处拆分字符串,并尝试将其输入到数组中**
字符串[]数组=result.Split(“”);
foreach(数组中的字符串标记)
{
Log((token.ToString());
字=记号;
}
结果=”;
列表字符=新列表(word.ToCharArray());
而(字符数>0)
{
int indexChar=Random.Range(0,字符数-1);
结果+=字符[indexChar];
字符.RemoveAt(indexChar);
}
返回结果;
}
}
公共类文字混乱:单一行为
{
公共词[]词;
[标题(“UI引用”)]
公共物品预制;
公共转换容器;
公共浮动空间;
公共浮点数LERPSEED=5;
List charObjects=new List();
首先选择的对象为CharObject;
公共词;
公共静态字扰频主;
无效唤醒()
{
main=这个;
}
//用于初始化
void Start()
{
ShowScramble(currentWord);
}
//每帧调用一次更新
无效更新()
{
重新定位对象();
}
void对象()
{
if(charObjects.Count==0)
{
返回;
}
浮动中心=(charObjects.Count-1)/2;
for(int i=0;i单词长度-1)
{
LogError(“索引超出了0-”+(words.Length-1.ToString()之间的范围);
返回;
}
//字符串结果=huruf;
//foreach(字符串是word());
char[]chars=words[index].GetString().ToCharray();
foreach(字符中的字符c)
{
CharObject clone=实例化(prefab.gameObject.GetComponent();
clone.transform.SetParent(容器);
添加(clone.Init(c));
}
currentWord=索引;
}
公共无效掉期(int indexA、int indexB)
{
CharObject tmpA=charObjects[indexA];
charObjects[indexA]=charObjects[indexB];
charObjects[indexB]=tmpA;
charObjects[indexA].transform.SetAsLastSibling();
charObjects[indexB].transform.SetAsLastSibling();
校验字();
}
公共无效选择(CharObject CharObject)
{
如果(第一次选择)
{
交换(firstSelected.index、charObject.index);
//取消选择
//firstSelected=null;
firstSelected.Select();
charObject.Select();
}
其他的
{
firstSelected=charObject;
}
}
公共无效取消选择()
{
firstSelected=null;
}
公共无效校验字()
{
启动例程(CoCheckWord());
}
IEnumerator CoCheckWord()
{
收益率返回新的WaitForSeconds(0.5f);
字串=”;
foreach(CharObject中的CharObject CharObject)
{
word+=charObject.character;
}
if(word==words[currentWord].word)
{
currentWord++;
ShowScramble(currentWord);
}
}
}
这是一个结果


也许我可以得到这个问题的帮助,我仍然在学习C#,如果我的代码乱七八糟,我很抱歉,你的代码将循环中的拆分一遍又一遍地分配给
word
变量,每次都覆盖
word
。然后您将
word
拆分为其字符,并尝试将其洗牌。相反,您应该洗牌分割数组

使用LINQ的示例:

public static string[] ScrambleSentence(string sentence)
{
    var random = new Random();
    return sentence.Split(' ').OrderBy(x => random.Next()).ToArray();
}

下面是一个简单的方法来随机单词在文本中的位置

var rd = new Random();
string[] words = text.Split(' ').OrderBy(w => rd.Next()).ToArray();

// If you want a simple string instead of an array of words
string rdText = string.Join(" ", words);

这基本上是一个双重的。此外,OP明确要求输出为数组而不是新字符串。