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

时间:2018-10-13 10:31:54

标签: c# arrays unity3d unityscript scramble

我需要从我上的课程中获取有关代码修复的帮助,我尝试创建一个简单的脚本来扰乱句子中的单词,例如“房子破了”变成“房子破了”。我的代码也可以工作只能用一个字打乱,例如“ THE”变成“ HTE”, 我尝试使用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);
        }
    }
}

此为结果

no. 1 is a result for this code and no.2 is log from split string i try to add into array

也许我可以为这个问题寻求帮助,我仍然在学习C#,如果我的代码搞砸了,我感到抱歉

2 个答案:

答案 0 :(得分:2)

您的代码将循环中的拆分重复分配给word变量,每次都覆盖word。然后,您将word分成了几个字符,并尝试对其进行随机排序。您应该改组拆分数组。

使用LINQ的示例:

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

答案 1 :(得分:0)

这是随机化文本中单词位置的简单方法

Object.create