I’m working on a solitaire counting program. I already have the main program working but when I try to implement my own class I get an NullPointerException error on line 19 (whenever it reaches c.getRank).
Note that I first created my main program while importing a class called CardDeck that has all the functions I need for it to work but now I’m supposed to create my own class that does the exact same thing. (Note that I don’t have access to the imported CardDeck class).
Here is the main code:
import se.lth.cs.ptdc.cardGames.Card;
public class Patiens {
public static void main(String[] args) {
double good = 0;
double bad = 0;
double result = 0;
for (int a = 0; a < 1000000; a++) {
CardDeck deck = new CardDeck();
deck.shuffle();
double fail = 0;
while (deck.moreCards()) {
for (int i = 1; i <= 3 && deck.moreCards(); i++) {
Card c = deck.getCard();
if (i == 1 && c.getRank() == 1) {
fail++;
}
if (i == 2 && c.getRank() == 2) {
fail++;
}
if (i == 3 && c.getRank() == 3) {
fail++;
}
}
}
if (fail >= 1) {
bad++;
}
else{
good++;
}
}
System.out.println("Good: " + good + " Bad: " + bad);
result = good / bad;
System.out.println("Result= " + result);
}
}
What it does is count the probability that my deck will finish successfully:
It’s counting 1-2-3, 1-2-3 while at the same time drawing a card. Now IF the card happens to be an ACE when it counts “1” the current deck will fail. Same goes for a card of rank 2 while the program counts “2” etc. The probability that it will finish without failing once is 0.8% .
Here is the CardDeck class I’m creating:
import se.lth.cs.ptdc.cardGames.Card;
import java.util.Random;
public class CardDeck {
private Card[] cards;
private int current;
private static Random rand = new Random();
public CardDeck() {
cards = new Card[52];
for(int suit = Card.SPADES; suit <= Card.CLUBS; suit++) {
for (int i = 0; i < 13; i++) {
cards[i * suit] = new Card(suit, i);
}
}
current = 0;
}
public void shuffle() {
Card k;
for(int i = 1000; i > 0; i--) {
int nbr = rand.nextInt(52);
int nbr2 = rand.nextInt(52);
k = cards[nbr2];
cards[nbr2] = cards[nbr];
cards[nbr] = k;
}
}
/**
*Checks for more cards
*/
public boolean moreCards() {
if(current > 51) {
return false;
} else {
return true;
}
}
/**
*Draws the card lying on top.
*/
public Card getCard() {
return cards[current++];
}
}
Here is the import se.lth.cs.ptdc.cardGames.Card; If needed, It is the class that creates the cards.
package se.lth.cs.ptdc.cardGames;
public class Card {
public static final int SPADES = 1;
public static final int HEARTS = SPADES + 1;
public static final int DIAMONDS = SPADES + 2;
public static final int CLUBS = SPADES + 3;
private int suit;
private int rank;
public Card(int suit, int rank) {
this.suit = suit;
this.rank = rank;
}
public int getSuit() {
return suit;
}
public int getRank() {
return rank;
}
}
(Note that I’m not supposed to change the above class)
Your problem is here:
If you change this to:
It will do what you expect.
Two things to consider: firstly, arrays are zero-based, so your first card needs to be at index 0. Secondly, by multiplying by the suit, you will get multiples of that number, e.g.:
So some elements will be filled more than once (12 is filled four times), and some (particular prime numbers > 13) elements (e.g. 23) will be null. In general, it’s probably enough to represent the index with another variable, like so: