2013-03-24 84 views
0

選擇一個類或變量的實例我正在製作黑傑克遊戲,並且正在實施多個玩家的使用。我似乎無法弄清楚如何適當地命名我的playerCards類的實例以及每個玩家所需的各種變量,以便在for循環迭代期間正確選擇它們。如何使用(i)

Hand playerCards1 = new Hand(cards); 
    Hand playerCards2 = new Hand(cards); 
    Hand playerCards3 = new Hand(cards); 
    Hand playerCards4 = new Hand(cards); 

我一直在使用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(); 

       } 
+0

**手**的實現在哪裏? – Blachshma 2013-03-24 15:20:49

回答

2

可以使手部對象的數組:

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

然後你就可以在你的中引用的每個球員環路playerCards [一世]。 您可能想要在開始時詢問玩家人數,然後使用它來設置playerCards數組的大小。

+0

很好的回答謝謝:) – 2013-03-24 16:05:17

1

考慮創建一個類播放器性能的手,銀行,投注等,然後使用任何數據結構(數組?),用於存儲播放器。

+0

好主意謝謝:)] – 2013-03-24 16:05:52

1

我猜你應該使用數組變量玩家手中:

Hand[] playerCards = new Hand[4]; 

for (int i = 0; i < 4 ; i++) 
playerCards[i] = new Hand(cards);