Home > Mobile >  How do I avoid getting a duplicated value from other arrays using C# (Unity)
How do I avoid getting a duplicated value from other arrays using C# (Unity)

Time:11-04

So, I'm almost finish with the team generator. There are 2 teams: red, and blue. I just need to really have them not duplicate from the other. Here is my output. I expect the other team not to duplicate the value from the other team.

I really expect to have my 2 teams get unique random numbers. Each having 2 elements only.failed outputscript

CodePudding user response:

I dont know the exact structure you are using, but I think the best way is to check with each random value against the previous already set members, so you have to backtrack each element with the new value

        for (i = 0; i < mxMembers; i  )
        {                        
            do
            {
                TempVal = Random.RandomRange(0, mxMembers);
                isRepeated = false;
                for (j = 0; j < i; j  )
                {
                    if (RedTeam[j] == TempVal)
                    {
                        isRepeated = true;
                    }
                }
            } while (isRepeated==true);
            RedTeam[i] = TempVal;
            
        }

CodePudding user response:

Assuming you have 4 players and you want 2 players assigned to each team at random, you could try something like this...

using System.Collections.Generic;
using UnityEngine;

public class TeamAssignment : MonoBehaviour
{
    
    //Create 2 teams
    List<int> redTeam = new List<int>();
    List<int> blueTeam = new List<int>();
    
    
    void Start()
    {
        
        //Run this loop 4 times. (For 4 players.)
        for (int player = 1; player <= 4; player  )
        {
            //For each player, pick a random number between 1 and 2
            int randomNumber = Random.Range(1, 3);

            //If random number is a 1, add player to Red team (unless it already has 2 players then we will add to blue team)
            if (randomNumber == 1)
            {
                if (redTeam.Count < 2)
                    redTeam.Add(player);
                else
                    blueTeam.Add(player);
            }

            //If random number is a 2, add player to Blue team (unless it already has 2 players then we will add to red team)
            else if (randomNumber == 2)
            {
                if (blueTeam.Count < 2)
                    blueTeam.Add(player);
                else
                    redTeam.Add(player);
            }
        }


        //Log our team assignments to the console
        for (int playerNumber = 1; playerNumber <= 4; playerNumber  )
        {
            Debug.Log($"i am player {playerNumber} of the {TeamNameOf(playerNumber)} team");
        }

    }


    /// <summary> Returns the team name of a player. </summary>
    public string TeamNameOf(int player)
    {
        if ( redTeam.Contains(player) )
            return "red";
        else
            return "blue";
    }
}

Each team gets exactly two random players between 1-4, with no duplicates.

Here's an example of the code's unity console output

Thank you.

  • Related