0

我正在制作一个二十一点游戏,我正在实现多个玩家的使用。我似乎无法弄清楚如何正确命名我的 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),但它说它们在这种情况下不存在。

如何标记我的 playerCards 和变量,以便 (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 ();

                }
4

3 回答 3

2

You can make an array of Hand objects:

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.
}

Then you can reference each player within your for loop as playerCards[i]. You probably want to ask for the number of players at the beginning, and then use that to set the size of the playerCards array.

于 2013-03-24T15:27:27.093 回答
1

考虑创建一个具有 Hand、Bank、Bet 等属性的 Player 类,然后使用任何数据结构(Array?)来存储 Players。

于 2013-03-24T15:26:35.677 回答
1

我猜你应该为玩家手牌使用数组变量:

Hand[] playerCards = new Hand[4];

for ( int i = 0; i < 4 ; i++ )
 playerCards[i] = new Hand(cards);
于 2013-03-24T15:26:38.990 回答