C# 如何使用(i)选择类或变量的实例

C# 如何使用(i)选择类或变量的实例,c#,C#,我正在制作一个Black Jack游戏,我正在实现使用多个玩家。我似乎不知道如何恰当地命名playerCards类的实例,以及每个播放器所需的各种变量,以便在for循环迭代期间正确选择它们 Hand playerCards1 = new Hand(cards); Hand playerCards2 = new Hand(cards); Hand playerCards3 = new Hand(cards); Hand playerCards4 = new Hand

我正在制作一个Black Jack游戏,我正在实现使用多个玩家。我似乎不知道如何恰当地命名playerCards类的实例,以及每个播放器所需的各种变量,以便在for循环迭代期间正确选择它们

    Hand playerCards1 = new Hand(cards);
    Hand playerCards2 = new Hand(cards);
    Hand playerCards3 = new Hand(cards);
    Hand playerCards4 = new Hand(cards);
我尝试过使用playerCards[I]和playerCards(I),但它说它们在这种情况下并不存在

我如何标记我的播放器卡和变量,以便(I)被识别为1-4

这是我的密码

using System;
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{
    public class MainClass
    {
        public static void Main (string[] args)
        {
            // Creates a new instance of the Deck Class, giving us access to a shuffled deck of cards.
            Deck cards = new Deck();

            // Create 5 new instances of the Hand Class, one for each player, one for the dealer. Will use List to store cards from the Deck,
            //and use Print function to displayer those cards.
            Hand playerCards0 = new Hand(cards);
            Hand playerCards1 = new Hand(cards);
            Hand playerCards2 = new Hand(cards);
            Hand playerCards3 = new Hand(cards);

            Hand dealerCards = new Hand(cards);

            // Creates the player's bank, defaults to 1000.
            int iBank0 = 1000;
            int iBank1 = 1000;
            int iBank2 = 1000;
            int iBank3 = 1000;

            // Stores the player's bet amount.
            int iBet0 = 0;
            int iBet1 = 0;
            int iBet2 = 0;
            int iBet3 = 0;

            int iNumOfPlayers = 0;

            // Creates a bool that will be used to stay in or out of the Hit or Stick loop.
            bool bStick = false;

            // Creates a string to store the user's input.
            string sUserInput;

            // Writes a line to the console welcoming the player.
            Console.WriteLine("Welcome to Black Jack\n\nPress Enter To Start!");

            //Causes a pause in the console untill Enter is hit.
            Console.ReadLine ();

            Console.WriteLine ("Select between 1-4 players?");
            sUserInput = Console.ReadLine();
            iNumOfPlayers = int.Parse (sUserInput);

            // This while loop willly repeated loop the entire BlackJack game untill the player exits.
            while (true) 
            {
                for (int i = 0; i <iNumOfPlayers; i++)
                {
                    bStick = false;

                    // Uses a function from the Deck Class to count how many cards are left in the Deck called "cards".
                    cards.DeckTotal ();

                    // Checks to see if there is less than 21 cards left in the Deck "cards". If there is a new Deck is created.
                    if (cards.iDeckTotal < 21) 
                    {
                        cards = new Deck ();
                        Console.WriteLine ("New Deck!\n");
                        Console.ReadLine ();
                    }

                    // Emptys both the player's and the dealer's Hand Lists of Cards.
                    playerCards[i].PlayerHand.Clear ();
                    dealerCards.DealerHand.Clear ();

                    //Clears the console screen of information, displays the user's current amount in the bank
                    Console.Clear ();
                    Console.WriteLine ("New Round!");
                    Console.WriteLine ("\nYou have " + iBank[i] + " in the Bank.");
                    Console.WriteLine ("\nPlace your Bet, Or Enter Exit to Quit.\n");
                    sUserInput = Console.ReadLine ();

                    if (sUserInput == "Exit" && iNumOfPlayers == 1) 
                    {
                        Environment.Exit (0);
                    }

                    if (sUserInput == "Exit")
                    {
                        iNumOfPlayers = iNumOfPlayers - 1;
                        i--;
                        continue;
                    }

                    iBet[i] = int.Parse (sUserInput);
                    iBank[i] = (iBank[i] - iBet[i]);

                    Console.Clear ();

                    cards.PlayerHit (playerCards[i]);
                    cards.PlayerHit (playerCards[i]);

                    playerCards[i].PlayerTotal ();

                    playerCards[i].PrintPlayerHand ();

                }
使用系统;
使用System.Collections.Generic;
使用系统文本;
命名空间BlackJackGameX
{
公共类主类
{
公共静态void Main(字符串[]args)
{
//创建一个Deck类的新实例,让我们可以访问一副洗牌牌。
卡片组=新卡片组();
//创建5个新的手牌类别实例,每个玩家一个,庄家一个。将使用列表存储牌组中的牌,
//并使用打印功能显示这些卡片。
手牌玩家Cards0=新牌(牌);
手牌玩家Cards1=新牌(牌);
手牌玩家Cards2=新牌(卡片);
手牌玩家Cards3=新牌(卡片);
手牌交易卡=新牌(卡);
//创建玩家的银行,默认为1000。
int-iBank0=1000;
int-iBank1=1000;
int-iBank2=1000;
int-iBank3=1000;
//存储玩家的下注金额。
int iBet0=0;
int iBet1=0;
int iBet2=0;
int iBet3=0;
int=0;
//创建一个bool,该bool将用于保持在命中或棍棒循环中或之外。
boolbstick=false;
//创建一个字符串来存储用户的输入。
弦支;
//向控制台写入欢迎播放机的行。
Console.WriteLine(“欢迎使用Black Jack\n\n按Enter开始!”);
//导致控制台暂停,直到按下Enter键。
Console.ReadLine();
Console.WriteLine(“在1-4名玩家之间选择?”);
sUserInput=Console.ReadLine();
iNumOfPlayers=int.Parse(sUserInput);
//这个while循环会重复循环整个21点游戏,直到玩家退出。
while(true)
{

对于(int i=0;i考虑使用属性手、银行、赌注等创建一个类玩家,然后使用任何数据结构(数组?)存储玩家。

考虑使用属性手、银行、赌注等创建一个类玩家,然后使用任何数据结构(数组?)用于存储玩家。

我想你应该为玩家的手使用数组变量:

Hand[] playerCards = new Hand[4];

for ( int i = 0; i < 4 ; i++ )
 playerCards[i] = new Hand(cards);
Hand[]playerCards=新手[4];
对于(int i=0;i<4;i++)
扑克牌[i]=新牌(牌);

我想你应该在玩家手上使用数组变量:

Hand[] playerCards = new Hand[4];

for ( int i = 0; i < 4 ; i++ )
 playerCards[i] = new Hand(cards);
Hand[]playerCards=新手[4];
对于(int i=0;i<4;i++)
扑克牌[i]=新牌(牌);

您可以制作手部对象数组:

Hand[] playerCards = new Hand[4]; // or however many players you have.
for (int i = 0; i < playerCards.length; i++) {
   playerCards[i] = new Hand(cards);
   // all of your initialization, bank, bet amount, etc.
}
Hand[]playerCards=新手[4];//或者不管你有多少玩家。
for(int i=0;i
然后,您可以引用for循环中的每个玩家作为playerCards[i]。
您可能希望在开始时询问玩家数量,然后使用该数字设置玩家卡阵列的大小。

您可以制作手部对象阵列:

Hand[] playerCards = new Hand[4]; // or however many players you have.
for (int i = 0; i < playerCards.length; i++) {
   playerCards[i] = new Hand(cards);
   // all of your initialization, bank, bet amount, etc.
}
Hand[]playerCards=新手[4];//或者不管你有多少玩家。
for(int i=0;i
然后,您可以引用for循环中的每个玩家作为playerCards[i]。
您可能希望在开始时询问玩家数量,然后使用该数量设置玩家卡阵列的大小。

手的实现在哪里?手的实现在哪里?