Home > OS >  Unity C# Script: Deal cards from deck to 4 players 1 card at a time
Unity C# Script: Deal cards from deck to 4 players 1 card at a time

Time:08-22

I have one deck that contains all the cards and want each player to draw 1 card at a time until each player has 7 cards in their hand. Similar to the play style of Uno.

Currently I have all the cards in a Deck list and want to loop through each player's hand (also each a list, p1Hand, p2Hand, p3Hand, p4Hand) and add a card from the deck to each player one at a time.

I have the game looping to draw the 7 cards to one player. But I am trying to expand on that code.

Ps. Still learning coding and unity as a whole.

Current Loop for 7 cards running through a coroutine and places the cards from the Deck list into a Hand list during DrawCard().

 private int initialHand = 7;
 
 for (int i = 0; i < initialHand; i  )
             {   
                 WaitForSeconds wait = new WaitForSeconds(.5f);
                 cardManager.DrawCard();
                 yield return wait;
             }

Here is where I am getting stuck. I create the lists for each player hand instead of just one. Then create a list of lists to cycle through each player a deal them one card each until then all have 7.

 public List<Card> p1Hand = new List<Card>(); //in Player Manager Script
 public List<Card> p2Hand = new List<Card>(); //in Player Manager Script
 public List<Card> p3Hand = new List<Card>(); //in Player Manager Script
 public List<Card> p4Hand = new List<Card>(); //in Player Manager Script
             
 public List<List<Card>> players = new List<List<Card>>(); //in Player Manager Script
 
 public List<Card> tempPlayerHand = new List<Card>(); //in Game Manager Script that handles the initial draw phase
     
 private int initialHand = 7;
         
 for (int i = 0; i < initialHand; i  )
     {
         WaitForSeconds wait = new WaitForSeconds(.5f);
         for (int p = 0; p < playerManager.players.Count; p  )
         {
             if(playerManager.players.Equals(playerManager.p1Hand))
             {
                 tempPlayerHand = playerManager.p1Hand;
                 cardManager.DrawCard(tempPlayerHand);
             }
             yield return wait;
         }
     }

I create a tempPlayerHand list to try and send the information to the Card Manager script that runs the DrawCard function. I am thinking the if statement is wrong to identify which player hand to send the card to. I will also add an if statement for each player hand if that is the way to work the loop.

CodePudding user response:

Welcome to Stack Overflow! This question might get more traction on the Game Developers specialist Stack, but here's my answer.

First a general tip: try to work out the logic independent of the implementation details. I believe you've gotten off-track mostly because you're trying to solve two issues at the same time: how the logic should work, and, how to make Unity do the things you want to show.

The quickest fix is simply lose the p1Hand etc and use 'players':

         for (int p = 0; p < playerManager.players.Count; p  )
         {
             tempPlayerHand = playerManager.players[p];
             cardManager.DrawCard(tempPlayerHand);
             yield return wait;
         }

You want the cards to go to all players, so you don't need to check which one it is.

If you need to access players via p1Hand etc, you can set those up as getter methods which return the relevant list from 'players':

public List<Card> p1Hand
{
   get => players[0];
}
public List<Card> p2Hand
{
   get => players[1];
}
  • Related