mardi 9 avril 2019

How to remove from list the string that is selected randomly (after the shuffle)

I have two lists with 4 strings each. I shuffle them and then I select a string on each list to create a combination of strings from both lists. I want four combinations in total and I want always to print randomly the combinations below:

Dog Lion Dog and car Lion and car

I have used the RemoveAt index 0 to remove the selected strings to not have any duplicates but sometimes one of above combinations get printed twice. How can I fix this? Any advice would be very helpful.

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

public class randomLetters : MonoBehaviour
{

    List<string> mylist = new List<string>();
    List<string> mylist2 = new List<string>();

    void Start()
    {
        mylist.Add("dog");
        mylist.Add("lion");
        mylist.Add("dog");
        mylist.Add("lion");

        mylist2.Add("carfordog");
        mylist2.Add("carforlion");
        mylist2.Add("carfordog");
        mylist2.Add("carforlion");


        Shuffle(mylist);
        Shuffle(mylist2);

        StartCoroutine(Wait());
    }


    IEnumerator Wait()
    {
        for (int i = 0; i < 4; i++)
        {


            string selected = mylist[i];
            string selected2 = mylist2[i];

            if(selected == "dog" && selected2 == "carfordog")
            {

                Debug.Log("Dog and Car");


            }else if (selected == "dog" && selected2 == "carforlion")
            {
                Debug.Log("Dog");
            }
            else if (selected == "lion" && selected2 == "carforlion")
            {
                Debug.Log("Lion and Car");
            }
            else if (selected == "lion" && selected2 == "carfordog")
            {
                Debug.Log("Lion");
            }

            yield return new WaitForSeconds(1);
        }

        mylist.RemoveAt(0);
        mylist2.RemoveAt(0);
    }


    void Shuffle(List<string> lists)
    {
        for (int j = lists.Count - 1; j > 0; j--)
        {
            int rnd = UnityEngine.Random.Range(0, j);
            string temp = lists[j];
            lists[j] = lists[rnd];
            lists[rnd] = temp;


        }
    }


}






Aucun commentaire:

Enregistrer un commentaire