So i created a deck of card list that holds 20 elements (Different cards)
On layout it randomly places cards from the deck into positions. How do i prevent the same card picked more than one time ? Perhaps i am doing this all wrong but i need the layed down card to be removed from the list so no repeating is possible. If there is a clear example how to do it let me know
As always thank you for your time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gamesetup : MonoBehaviour
{
public List<GameObject> Deck3 = new List<GameObject>();
Void Start()
{
StartCoroutine(Layoutgame());
}
IEnumerator Layoutgame()
{
yield return new WaitForSeconds(1f);
Instantiate(Deck3[Random.Range(0,20)], transform.position = new Vector3(-3.28f, 2.38f, 0), Quaternion.identity);
yield return new WaitForSeconds(1f);
Instantiate(Deck3[Random.Range(0, 20)], transform.position = new Vector3(-1.47f, 2.38f, 0), Quaternion.identity);
yield return new WaitForSeconds(1f);
Instantiate(Deck3[Random.Range(0, 20)], transform.position = new Vector3(0.37f, 2.38f, 0), Quaternion.identity);
yield return new WaitForSeconds(1f);
Instantiate(Deck3[Random.Range(0, 20)], transform.position = new Vector3(2.17f, 2.38f, 0), Quaternion.identity);
}
}
}
As soon as a card get picked, you have to check if the list contains more of that card
private void PickCard()
{
// Get the card on top of the deck
GameObject pickedCard = Deck3[Deck3.Count - 1)
// Check if the deck contains that card
if (Deck3.Countains(pickedCard)) {
Deck3.Remove(pickedCard);
}
}
But that works only if the maximum of the same card is twice, if it can be more times in the deck, you need a while loop.
You said, your deck holds 20 cards, but “Random.Range(0, 20)” picks a random number between 0 and 20, so it will be luck, if the result is 20, is that on purpose ?
I will try this. The elements are numbered from 0 to 19 thats why 20 is the end number. Or does this not work this way ? Size is 20 but it sets it as 0 to 19. It is a game start setup so the cards need to be placed random so no game is the same
Sphinks thank you i needed to fix the script as it had some typo’s in it but it does work. Good to know how this
works. Eses thank you for comfirming i am learning every day so good to know sometimes i get it right.
I noticed just now it takes 1 of the deck but it does not take of the picked element. Say it picks element nr 15 it does not remove 15 it removes just one of the total cards in the deck
Don’t pick a random element each time. Make a list of all the cards, shuffle that list, then just remove the first or last element of the list each time you need a new card. Here’s a good shuffle algorithm: https://www.dotnetperls.com/fisher-yates-shuffle
PraetorBlu thx for the link i found that one earlier but lack the skills to put the part script i see into C# use with unity I will try to figure it all out it sounds usefull.
it’s quite simple. You can just copy the Shuffle method into your script:
static void Shuffle<T>(T[] array)
{
int n = array.Length;
for (int i = 0; i < (n - 1); i++)
{
// Use Next on random instance with an argument.
// ... The argument is an exclusive bound.
// So we will not go past the end of the array.
int r = i + _random.Next(n - i);
T t = array[r];
array[r] = array[i];
array[i] = t;
}
}
Then you can use it with your own data:
// convert list to array
GameObject[] Deck = Deck3.ToArray();
// shuffle the array
Shuffle(Deck);
// convert the shuffled array back into a list
Deck3 = Deck.ToList();
// Tada now Deck3 is shuffled.
So far no luck i tried using the first part but i get the error the name_random does not exist in current content.
Is that seccond part sometyhing i need to insert in the first part ? I prolly missing the logic here.
Is it only shuffeling or indeed making sure once a card is dealed it won’t be dealed no more ?
I know this is old but just in case anyone wants to do this the following code is correct.
var ranNum=Random.Range(0,listName.Count);
var item=listName[ranNum]
listName.RemoveAt(ranNum);
1). Get a random number between 0 and the size of the array (random integers are non exclusive for the max so so you do not need -1).
2). Get item from list using position
3). Remove the item from the list by its position.