We are making our first 2D mobile game in Unity and we need some help. We coded on Java but is also our first time coding C#.
We are making a card game and we want to give to every player a random number, but (here's the important and difficult part for us) we want each random number not to repeat. For example if we have 5 players and we want to assign a random number from 5 to 10, if number 6 is assigned to player 1 then number 6 cant be assigned to other player.
Here's where we are:
//Player class only have string name and int trabajo
public static void assignRandom(Player jug)
{
int randomnum;
bool aleatorio;
do
{
randomnum= Random.Range(1, 5);
aleatorio = comprobarAleatorio(randomnum);
}
while (aleatorio);
jug.setTrabajo(randomnum);
}
public static bool comprobarAleatorio(int numaleatorio)
{
bool exists = false;
int longitud = jugadoresList.Count;
for (int i = 0; i < 5; i++)
{
if (jugadoresList[i].getTrabajo().Equals(numaleatorio))
{
exists = true;
}
}
}
Then with the int we make a switch and show on screen what type of player you are. But clearly we are doing something wrong because the numbers repeat itselfs, even with 0 (we dont know how).
We appreciate every help. Thank you very much! (and sorry for our english)