I am getting a java null error on this line when I run main
int rank = cards[i].getRank();
int points = cards[i].getPoints();
I am simply trying to get the countHighCardPoints() point values of the cards in the hand array and add the points to a sum. I am also going to assume my countDistributionPoints() will work if I was not also getting a null on char suit = cards[i].getSuit();
I also have a Card class and a Deck class already made.
Thus my Hand class which I need help on.
public class Hand
{
//Holds an array of card objects
private Card [] cards = new Card [13];
/**
* Constructor takes array of Cards and assigns that parameter to
* the instance variable
*/
public Hand(Card [] cards)
{
Card [] hand = cards;
}
/**
* Looks through each card in hand array and adds its points
* if the card has any to a sum highPoints
*/
public int countHighCardPoints()
{
int highPoints = 0;
for (int i = 0; i < cards.length; i++) {
int points = cards[i].getPoints();
highPoints += points;
}
return highPoints;
}
/**
* Counts the number of cards in each suit and will add points
* for the suits value, so if 3 or more suits zero points,
* 2 suits 1 point, 1 suit 2 points, 0 suits 3 points
*/
public int countDistributionPoints()
{
int countPoints = 0;
for (int i = 0; i < cards.length; i++) {
//char suit = cards[i].getSuit();
if (cards[i].getSuit() >= 3)
countPoints = 0;
else if (cards[i].getSuit() == 2)
countPoints++;
else if (cards[i].getSuit() == 1)
countPoints += 2;
else if (cards[i].getSuit() == 0)
countPoints += 3;
}
return countPoints;
}
/**
* Will print out the hand information in a neat format using a
* StringBuilder will print 4 cards containing rank,suit,points each line
* in order from Clubs,Diamonds,Hearts,Spades if no cards of the suit it
* will print a blank line
*/
public String toString()
{
StringBuilder hand = new StringBuilder();
// for (int i = 0; i < cards.length; i++) {
// char suit = cards[i].getSuit();
for (int j = 0; j < 5; j++) {
if (cards[j].getSuit() == 'C')
hand.append(cards[j] + " ");
else if (j == 4)
hand.append("\n");
}
for (int j = 0; j < 5; j++) {
if (cards[j].getSuit() == 'D')
hand.append(cards[j] + " ");
else if (j == 4)
hand.append("\n");
}
//
// for (int j = 0; j < 5; j++) {
// if (cards[j].getSuit() == 'H')
// hand.append(cards[j] + " ");
// else if (j == 4)
// hand.append("\n");
// }
//
// for (int j = 0; j < 5; j++) {
// if (cards[j].getSuit() == 'S')
// hand.append(cards[j]);
// else if (j == 4)
// hand.append("\n");
// }
// }
return hand.toString();
}
}
Deck class for reference
public class Deck
{
//Holds an array of card objects
private Card [] cards = new Card [52];
//Holds number of cards remaining in deck
private int count;
/**
* Constructor to fill in card objects in order of suits
* Clubs,Diamonds,Hearts, and Spades and keep count of remaining
* cards in deck
*/
public Deck()
{
//Fills in Club suit
for (int i = 0; i <= 12; i++) {
cards[i] = new Card(i+2, 'C');
count = 52-13;
}
//Fills in Diamond suit
for (int i = 13; i <= 25; i++) {
cards[i] = new Card(i-13+2, 'D');
count = 39-13;
}
//Fills in Heart suit
for (int i = 26; i <= 38; i++) {
cards[i] = new Card(i-26+2, 'H');
count = 26-13;
}
//Fills in Spade suit
for (int i = 39; i <= 51; i++) {
cards[i] = new Card(i-39+2, 'S');
count = 13-13;
}
}
/**
* Gets the value for count
*/
public int getCount()
{
return count;
}
/**
* The original cards in Card [] cards will be randomly shuffled
* by Math.random() and positions will be swapped
*/
public void shuffle()
{
for (int i = 0; i <= 51; i++) {
int j = (int)(Math.random() * 52);
int k = (int)(Math.random() * 52);
//Swaps card positions
Card temp = cards[j];
cards[j] = cards[k];
cards[k] = temp;
}
}
/**
* Creates a Card [] arrayOfCards which is 13 cards for each player
* and will determine number of cards that was dealt with count.
*/
public Card [] dealThirteenCards()
{
Card [] arrayOfCards = new Card [13];
for (int i = 0; i <= 12 && count < 52; i++) {
arrayOfCards[i] = cards[i];
count++;
}
return arrayOfCards;
}
/**
* Creates a StringBuilder which will print 13 cards per line
* containing their rank,suit,points
*/
public String toString()
{
StringBuilder info = new StringBuilder();
for (int i = 0; i < 13; i++) {
info.append(cards[i] + "," + " ");
}
info.append("\n");
for (int i = 13; i < 26; i++) {
info.append(cards[i] + "," + " ");
}
info.append("\n");
for (int i = 26; i < 39; i++) {
info.append(cards[i] + "," + " ");
}
info.append("\n");
for (int i = 39; i < 51; i++) {
info.append(cards[i] + "," + " ");
}
//Will exclude a comma because last card printed
for (int i = 51; i < 52; i++)
info.append(cards[i]);
return info.toString();
}
}
Aucun commentaire:
Enregistrer un commentaire