The card game program I am creating should prompt the user for the number of players, and how many cards in each hand, then shuffle the Deck, then deal cards, then display the cards in each players hand. It will display the Hands for all players unless there are not enough Cards in the Deck (we cannot deal 10 Hands of 7 Cards from a Deck of 52 Cards).
I have created a Card class, a Deck class, a Hand class, and driver to run the program. Since this is an assignment I must stick to the rules given, and so I am only allowed to use arrays, not array lists. My problem is that I cannot figure out how to print the Card objects inside the Hand object, inside the array of Hands. There is also probably a much better way to do this, but I am limited to what I am allowed to import / use. Can I get some help on where to look for printing objects inside nested arrays? I cannot use my method written in the Hand class to add cards to the Hands in an array, due to not having an object name.
EDIT: I have the constructor in the Hand class create a hand sized on the int passed to it. The confusion is that when I get how many players are indeed playing the game from the user, I create an array of type Hand in the driver, which is filled with new Hand objects using a loop. At that point I have no idea how to reference each individual hand object so I can print the contents using toString(). They do not appear to have a name.
Code to follow:
import java.util.Arrays;
import java.util.Scanner;
public class CardsDriver {
public static void main(String[] args)
{
Card c1 = new Card(); //create new card object
Scanner kb = new Scanner(System.in);
int cards;
int players;
System.out.print("How many players are in the game?");
players = Integer.parseInt(kb.nextLine());
System.out.print("\nHow many cards will be dealt to each player?");
cards = Integer.parseInt(kb.nextLine());
while ((cards * players) > 52)
{
System.out.print("There are not enough cards in the deck to deal " +
players + " hands of " + cards + " cards. try again.");
System.out.print("How many players are in the game?");
players = Integer.parseInt(kb.nextLine());
System.out.print("\nHow many cards will be dealt to each player?");
cards = Integer.parseInt(kb.nextLine());
}
Deck readyDeck = new Deck(); //create new deck
readyDeck.shuffleDeck(); //shuffle the newly built deck using Java.Util.Random
Hand[] playerHands= new Hand[players]; //create another array to hold all player hands
for(int index =0; index < playerHands.length; index++)
{
playerHands[index] = new Hand(cards); //create hand object for each player of the size input by the player
/*for (int index2 =0; index2<cards;index2++)
{
//fill each hand with cards using addcard somehow. i have no object name.
Hand.addCard(readyDeck.dealACard());
}*/
}
}