如何使用Vars初始化列表

时间:2016-04-30 18:22:24

标签: c# list

我有一个名为Players的类,它拥有各种玩家属性。我想使用循环来基于某些用户输入初始化播放器列表。然后我将使用该列表与每个玩家进行纸牌游戏。我想List必须是全球性的。

我无法弄清楚如何初始化列表,因为我似乎无法使用var 尝试创建新播放器时在名称字段中。

由于

    class Players
{
    public string name;
    public double bank;
    public Hands  playerHands; //A List of hands played

    public Players ()
    {
        name = "Unknown";
        bank = 0.0;
        playerHands  = null;
    }

    public Players(string name, double bank, Hands playerHands)
    {
        this.name = name;
        this.bank = bank;
        this.playerHands = playerHands;
    }
}

class Program
{
    private static int numOfPlayers;
    private static int numOfDecks;
    private static List<Cards> shoe;

    public static void Main()
    {
        Console.OutputEncoding = Encoding.UTF8; //required to display special characters
        Hands playerHands = new Hands();
        Hands dealerHands = new Hands();
        SetUpTable();

        //Initilize Players
        Players player = new Players("Ken", 100.00, playerHands);
        Players dealer = new Players("Dealer", 0.0, dealerHands);
        int dealerScore = 0, playerScore = 0;

        //Deal initial cards
        //The deal starts with the dealer who gets the first card face down

            Cards dealerUpCard = null;
            Hand dealerHand = new Hand();
            Hand playerHand = new Hand();
            dealer.playerHands.addHand(dealerHand);
            player.playerHands.addHand(playerHand);

            for (int iii = 1; iii < 3; iii++)
            {   
                //Deal dealer's hand                          
                dealerHand.addCard(shoe[0]);
                dealerScore += shoe[0].value;
                if (iii == 2) { dealerUpCard = shoe[0]; }
                shoe.RemoveAt(0);

                //Deal Player's aHand           
                playerHand.addCard(shoe[0]);             
                playerScore += shoe[0].value;
                shoe.RemoveAt(0);
            }

        //Print out the test hands
        displayHand(dealer);            
        displayHand(player);
        Console.Read();
    }

    static void displayHand(Players player)
    {
        //Print out the test hands
        for (int i = 0; i < player.playerHands.hands.Count; i++)
        {
            Console.Write("{0}'s hand: ", player.name);
            for (int j = 0; j < player.playerHands.hands[i].hand.Count; j++)
            {
                Console.Write(" {0}{1} ", player.playerHands.hands[i].hand[j].rank, (char)player.playerHands.hands[i].hand[j].suit);
            }
            Console.WriteLine();
        }
    }

    static void SetUpTable()
    {
        //Create players
        numOfPlayers = validateUserInput("How many players (1-6)?",1,6);
        for (int i = 0; i<numOfPlayers; i++)
            {   //get player elements
                //initiliz a new unique player in the Players list
                //ie: Players player = new Players("Ken", 100.00, playerHands);
            }

        //Create shoe of cards
        numOfDecks = validateUserInput("How many decks of cards (1-8)?", 1, 8);          
        shoe = Deck.createDeck(numOfDecks);
        Deck.shuffleDeck(shoe, numOfDecks);          
    }
}

1 个答案:

答案 0 :(得分:1)

public List<Players> ListOfPlayers=new List<Players>();
static void SetUpTable()
{
    //Create players
    numOfPlayers = 6;

    for (int i = 0; i<numOfPlayers; i++)
        {   
          ListOfPlayers.Add(new Players());
          //or with initial values with ctor:
          ListOfPlayers.Add(new Players("Ken", 100.00, playerHands));
          //or
          ListOfPlayers.Add(new Players(){name="Ken", bank=100.00, playerHands=_playerHands});
            //ie: Players player = new Players("Ken", 100.00, playerHands);
        }

}