“Your team is missing the numPacks instance variable even though the assignment requires it.”
“set this.numPacks = numPacks here”
“Use the set method so that the data is validated and the errorFlag is set correctly. Don’t directly set the member instance variables in the constructors.”
“Since myCards was created with:
new Card[MAX_CARDS];
then myCards.length always equals MAX_CARDS, so instead check if numCards 10)
{
System.out.println(“**** INVALID NUMBER ****”);
System.out.println(“That’s invalid, please enter a number between 1-10 for the number of players: “);
} else
{
haveValidNumberOfPlayers = true;
}
}
userInput.close();
System.out.println("The number of players is: " + numberOfPlayers);
// create a deck with one pack of cards.
Deck gameDeck = new Deck(1);
// create hand array with same # of players
dealIntoHands(numberOfPlayers, gameDeck);
// resets the deck, the hands are reset in dealIntoHands
gameDeck = new Deck(1);
// shuffles deck
gameDeck.shuffle();
// re-deals newly shuffled cards into hands.
System.out.println("Shuffled Deck");
dealIntoHands(numberOfPlayers, gameDeck);
}
// creates a new deck, deals the cards, and shuffles the cards.
public static void deckDealTest(int numPacks)
{
Deck newDeck = new Deck(numPacks);
// deals cards from the deck in standard order
dealCards(newDeck);
newDeck.init(numPacks);
System.out.println("***** SHUFFLED DECK *****");
newDeck.shuffle();
// deals cards from deck in shuffled order
dealCards(newDeck);
}
// method to deal cards into varying amount of hands. Hands are reset in method.
public static void dealIntoHands(int numberOfPlayers, Deck gameDeck)
{
Hand[] playerHands = new Hand[numberOfPlayers];
// puts hands in the array playerHands
for (int i = 0; i 0)
{
Card dealtCard = gameDeck.dealCard();
int arrayIndex = cardsDealt % numberOfPlayers;
Hand hand = playerHands[arrayIndex];
cardsDealt++;
hand.takeCard(dealtCard);
}
for (int i = 0; i 0)
{
Card dealtCard = packsInADeck.dealCard();
System.out.println(dealtCard);
}
}
}
class Deck
{
// constants
private final static int MAX_PACKS = 6;
private final static int NUM_CARDS_PER_PACK = Card.NUM_SUITS * Card.NUM_VALUES;
private final static int MAX_CARDS_PER_DECK = MAX_PACKS * NUM_CARDS_PER_PACK;
// static Card[] masterPack will be used throughout entire class.
public static Card[] masterPack;
private int topCard;
private Card[] cards;
Your team is missing the numPacks instance variable even though the assignment requires it.
// Constructor assumes there is one pack if no parameter is entered
public Deck()
{
// calls another constructor with parameter for numPacks
this(1);
}
// Constructor used when a number of packs is stated
public Deck(int numPacks)
{
allocateMasterPack();
init(numPacks);
}
// Called by constructor
private static void allocateMasterPack()
{
/*
* use if to make sure it only runs once. Early escape w/ return. MasterPack
* will not be null if method did not run
*/
if (masterPack != null)
{
return;
}
masterPack = new Card[NUM_CARDS_PER_PACK];
int masterPackIndex = 0;
// loops through the suits after the first suit is given all of the values in
// the second loop
for (int suitIndex = 0; suitIndex MAX_PACKS)
{
validNumPacks = false;
} else
{
// reinitializes topCard to the correct number.
topCard = numPacks * NUM_CARDS_PER_PACK;
set this.numPacks = numPacks here
cards = new Card[topCard];
for (int packCount = 0; packCount 0; i--)
{
Random random = new Random();
int randInt = random.nextInt(i + 1);
Card myCard = cards[i];
cards[i] = cards[randInt];
cards[randInt] = myCard;
}
}
// accessor for getting the topCard
public int getNumCards()
{
return topCard;
}
public Card dealCard()
{
// tests if the array is empty
if (topCard == 0)
{
// creates invalid card
Card invalidCard = new Card('1');
return invalidCard;
}
Card copiedCard = getCardCopy(--topCard);
return copiedCard;
}
public Card inspectCard(int k)
{
if (k >= topCard || k = MAX_CARDS)
{
return false;
}
// code is working only w/ valid cards and an array w/ room.
Card clonedCard = new Card(card);
myCards[numCards++] = clonedCard;
return true;
}
}
public Card playCard()
{
// tests if the array is empty
if (numCards == 0)
{
// creates invalid card
Card invalidCard = new Card('1');
return invalidCard;
}
Card playedCard = myCards[--numCards];
return playedCard;
}
public String toString()
{
// string when the hand is empty
if (numCards == 0)
{
return "*** The hand is empty ***";
}
StringBuffer output = new StringBuffer();
for (int i = 0; i = numCards || k < 0)
{
// returns a card w/ errorFlag true because Card('1') goes to constructor
Card invalidCard = new Card('1');
return invalidCard;
}
return myCards[k];
}
}
/*
- DECK WITH 2 PACKS
X of spades
A of spades
K of spades
Q of spades
J of spades
T of spades
9 of spades
8 of spades
7 of spades
6 of spades
5 of spades
4 of spades
3 of spades
2 of spades
X of hearts
A of hearts
K of hearts
Q of hearts
J of hearts
T of hearts
9 of hearts
8 of hearts
7 of hearts
6 of hearts
5 of hearts
4 of hearts
3 of hearts
2 of hearts
X of diamonds
A of diamonds
K of diamonds
Q of diamonds
J of diamonds
T of diamonds
9 of diamonds
8 of diamonds
7 of diamonds
6 of diamonds
5 of diamonds
4 of diamonds
3 of diamonds
2 of diamonds
X of clubs
A of clubs
K of clubs
Q of clubs
J of clubs
T of clubs
9 of clubs
8 of clubs
7 of clubs
6 of clubs
5 of clubs
4 of clubs
3 of clubs
2 of clubs
X of spades
A of spades
K of spades
Q of spades
J of spades
T of spades
9 of spades
8 of spades
7 of spades
6 of spades
5 of spades
4 of spades
3 of spades
2 of spades
X of hearts
A of hearts
K of hearts
Q of hearts
J of hearts
T of hearts
9 of hearts
8 of hearts
7 of hearts
6 of hearts
5 of hearts
4 of hearts
3 of hearts
2 of hearts
X of diamonds
A of diamonds
K of diamonds
Q of diamonds
J of diamonds
T of diamonds
9 of diamonds
8 of diamonds
7 of diamonds
6 of diamonds
5 of diamonds
4 of diamonds
3 of diamonds
2 of diamonds
X of clubs
A of clubs
K of clubs
Q of clubs
J of clubs
T of clubs
9 of clubs
8 of clubs
7 of clubs
6 of clubs
5 of clubs
4 of clubs
3 of clubs
2 of clubs
SHUFFLED DECK
9 of hearts
8 of spades
Q of clubs
9 of spades
K of hearts
T of clubs
J of diamonds
2 of clubs
6 of spades
K of spades
4 of spades
7 of spades
4 of clubs
3 of spades
9 of clubs
J of clubs
X of clubs
T of hearts
A of diamonds
J of hearts
X of spades
8 of diamonds
Q of clubs
J of spades
T of spades
4 of hearts
6 of clubs
5 of hearts
Q of spades
Q of hearts
X of clubs
9 of clubs
6 of diamonds
4 of clubs
2 of diamonds
3 of clubs
A of spades
4 of spades
5 of hearts
8 of spades
2 of diamonds
5 of clubs
K of hearts
6 of diamonds
A of diamonds
A of clubs
J of clubs
K of clubs
Q of diamonds
7 of clubs
8 of hearts
A of clubs
K of diamonds
X of diamonds
2 of clubs
9 of hearts
3 of clubs
5 of clubs
4 of hearts
A of hearts
6 of hearts
7 of spades
J of diamonds
2 of spades
Q of spades
T of clubs
2 of hearts
5 of spades
8 of clubs
7 of hearts
5 of diamonds
K of clubs
K of spades
J of spades
4 of diamonds
A of spades
8 of hearts
7 of hearts
J of hearts
7 of clubs
X of diamonds
9 of diamonds
9 of diamonds
A of hearts
Q of diamonds
7 of diamonds
5 of diamonds
K of diamonds
X of hearts
8 of diamonds
Q of hearts
4 of diamonds
7 of diamonds
T of spades
3 of hearts
2 of spades
6 of hearts
9 of spades
8 of clubs
3 of spades
2 of hearts
X of hearts
X of spades
6 of clubs
3 of hearts
T of hearts
3 of diamonds
6 of spades
3 of diamonds
5 of spades
T of diamonds
T of diamonds
DECK WITH 1 PACK
X of spades
A of spades
K of spades
Q of spades
J of spades
T of spades
9 of spades
8 of spades
7 of spades
6 of spades
5 of spades
4 of spades
3 of spades
2 of spades
X of hearts
A of hearts
K of hearts
Q of hearts
J of hearts
T of hearts
9 of hearts
8 of hearts
7 of hearts
6 of hearts
5 of hearts
4 of hearts
3 of hearts
2 of hearts
X of diamonds
A of diamonds
K of diamonds
Q of diamonds
J of diamonds
T of diamonds
9 of diamonds
8 of diamonds
7 of diamonds
6 of diamonds
5 of diamonds
4 of diamonds
3 of diamonds
2 of diamonds
X of clubs
A of clubs
K of clubs
Q of clubs
J of clubs
T of clubs
9 of clubs
8 of clubs
7 of clubs
6 of clubs
5 of clubs
4 of clubs
3 of clubs
2 of clubs
SHUFFLED DECK
2 of diamonds
6 of spades
6 of hearts
3 of hearts
8 of hearts
J of clubs
4 of clubs
X of spades
T of clubs
3 of clubs
J of spades
9 of diamonds
T of spades
3 of spades
Q of diamonds
5 of clubs
A of clubs
7 of spades
2 of clubs
7 of diamonds
Q of hearts
K of hearts
2 of spades
9 of clubs
J of diamonds
7 of hearts
A of diamonds
8 of diamonds
4 of diamonds
Q of clubs
9 of spades
K of diamonds
3 of diamonds
X of hearts
T of hearts
4 of hearts
6 of clubs
X of clubs
X of diamonds
A of spades
4 of spades
2 of hearts
A of hearts
9 of hearts
8 of spades
J of hearts
Q of spades
7 of clubs
8 of clubs
5 of hearts
K of spades
6 of diamonds
5 of diamonds
5 of spades
K of clubs
T of diamonds
Please enter the number of players (1-10):
6
The number of players is: 6
Hand of Player #1
X of spades
9 of spades
3 of spades
J of hearts
5 of hearts
K of diamonds
7 of diamonds
X of clubs
9 of clubs
3 of clubs
Hand of Player #2
A of spades
8 of spades
2 of spades
T of hearts
4 of hearts
Q of diamonds
6 of diamonds
A of clubs
8 of clubs
2 of clubs
Hand of Player #3
K of spades
7 of spades
X of hearts
9 of hearts
3 of hearts
J of diamonds
5 of diamonds
K of clubs
7 of clubs
Hand of Player #4
Q of spades
6 of spades
A of hearts
8 of hearts
2 of hearts
T of diamonds
4 of diamonds
Q of clubs
6 of clubs
Hand of Player #5
J of spades
5 of spades
K of hearts
7 of hearts
X of diamonds
9 of diamonds
3 of diamonds
J of clubs
5 of clubs
Hand of Player #6
T of spades
4 of spades
Q of hearts
6 of hearts
A of diamonds
8 of diamonds
2 of diamonds
T of clubs
4 of clubs
Shuffled Deck
Hand of Player #1
K of clubs
8 of hearts
6 of diamonds
5 of hearts
Q of hearts
A of clubs
4 of spades
K of diamonds
K of hearts
7 of clubs
Hand of Player #2
A of hearts
4 of clubs
T of hearts
4 of diamonds
7 of spades
A of spades
5 of spades
9 of diamonds
T of spades
A of diamonds
Hand of Player #3
2 of hearts
9 of spades
3 of spades
2 of spades
X of spades
K of spades
3 of clubs
2 of clubs
Q of spades
Hand of Player #4
7 of diamonds
J of diamonds
Q of clubs
X of clubs
7 of hearts
6 of spades
8 of diamonds
9 of clubs
3 of hearts
Hand of Player #5
9 of hearts
4 of hearts
5 of clubs
8 of clubs
6 of clubs
T of clubs
2 of diamonds
5 of diamonds
X of hearts
Hand of Player #6
6 of hearts
J of spades
T of diamonds
Q of diamonds
3 of diamonds
J of hearts
X of diamonds
8 of spades
J of clubs
*/


0 comments