Hello, I’m trying to make a card game of sorts and I have a deck of 32 cards and I want to deal 8 cards to four players. I wrote a function that shuffles the deck and now I want to deal the cards. I know how to deal all 32 cards to one player but I can’t figure out how to deal 8 cards to every player (4). I have a GameObject array that consists of all the cards and I cannot figure out how to select only 8 elements (cards) in that array and deal them to a specific player. If anyone knows the answer I would greatly appreciate it.
Here is the code (The problem is in the DealCardsPhase() method)
{
// Game Objects
public GameObject[] cardsArray;
public GameObject playerHand;
public GameObject opponentHand;
public GameObject dropZone;
private GameObject temporaryGameObject; // For the ShuffleCards() method.
// Game Phases (States)
[SerializeField] bool gameStartPhase;
[SerializeField] bool shuffleCardsPhase;
[SerializeField] bool dealCardsPhase;
[SerializeField] bool playPhase;
[SerializeField] bool endPhase;
[SerializeField] bool gameEndPhase;
private void Update()
{
GameStartPhase();
ShuffleCardsPhase();
DealCardsPhase();
}
private void GameStartPhase()
{
if (timeCounter <= 1f)
{
Debug.Log("---- Game Start Phase ----");
gameStartPhase = true;
foreach (GameObject card in cardsArray) // Places cards on the drop zone (for shuffling in the shuffling phase).
{
card.transform.SetParent(dropZone.transform);
}
}
else
{
gameStartPhase = false;
}
}
private void ShuffleCardsPhase()
{
if ((timeCounter > 1f) && (timeCounter <= 4f))
{
Debug.Log("---- Shuffle Cards Phase ----");
ShuffleCards();
shuffleCardsPhase = true;
}
else
{
shuffleCardsPhase = false;
}
}
private void DealCardsPhase()
{
if ((timeCounter > 4f) && (timeCounter <= 7f))
{
Debug.Log("---- Deal Cards Phase ----");
dealCardsPhase = true;
foreach (GameObject card in cardsArray) // Deal (shuffled) cards to players.
{
card.transform.SetParent(playerHand.transform);
}
}
else
{
dealCardsPhase = false;
}
}
private void ShuffleCards() // Constantly shuffle cards (objects in array).
{
for (int cards = 0; cards < cardsArray.Length; cards++)
{
// Debug.Log(cards);
int random = Random.Range(0, cardsArray.Length);
temporaryGameObject = cardsArray[random];
cardsArray[random] = cardsArray[cards];
cardsArray[cards] = temporaryGameObject;
}
}
}