Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to select an instance of a class or variable using (i)
    primarykey
    data
    text
    <p>I'm making a Black Jack game and i'm implementing the use of multiple players. I can't seem to figure out how to appropriately name my instances of the playerCards classes, and the various variables needed for each player, so that they are correct selected during the for loop iteration.</p> <pre><code> Hand playerCards1 = new Hand(cards); Hand playerCards2 = new Hand(cards); Hand playerCards3 = new Hand(cards); Hand playerCards4 = new Hand(cards); </code></pre> <p>I have tried using playerCards[i] &amp; playerCards(i), but it says they don't exist in this context.</p> <p>How do I label my playerCards and variables so the (i) is recognised as 1-4?</p> <p>Here is my code.</p> <pre><code>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 &lt;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 &lt; 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" &amp;&amp; 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 (); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload