So I'm trying to write a program to play the card game Go Fish, and I can't get the following code to compile:
import java.util.Random;
public class CardPile {
public static final int DECKSIZE = 52;
private Card[] cards;
private int numCards;
public CardPile() {
cards = new Card[DECKSIZE];
numCards = 0;
}
public void addToBottom(Card c) {
if (numCards == 52) {
System.out.println("The deck is full, you cannot add a card.");
} else {
for(int i = 0;i < 52;i++) {
int count = 0;
while (count <= 1) {
if (cards[i] == null) {
cards[i] = c;
numCards++;
}
count++;
}
}
}
}
public Card removeCard(Card c) {
for(int i = 0;i < 52;i++) {
if (cards[i].getValue() == c.getValue() && cards[i].getSuit() == c.getSuit()) {
cards[i] = null;
for(int shift = 1; shift < (cards.length() - i); shift++) {
cards[(i+shift)] = cards[(i+(shift-1))];
}
return c;
} else {
return null;
}
}
}
public Card removeTop() {
cards[0] = null;
int i = 0;
for(int shift = 1; shift < cards.length(); shift++) {
cards[shift] = cards[(shift+1)];
}
}
public int removeAll(int value) {
int cardsRemoved = 0;
for(int i = 0;i < 52;i++) {
if (value == cards[i].getValue()) {
cards[i] = null;
cardsRemoved++;
}
}
return cardsRemoved;
}
public int searchValue(int value) {
int count = 0;
for(int i = 0;i < 52;i++) {
if (cards[i].getValue() == value) {
count++;
}
}
return count;
}
public int getNumberCards() {
return numCards;
}
public String toString() {
for(int i = 0;i < 52;i++) {
System.out.print(cards[i].toString() + ", ");
}
}
private static Random r = new Random(1);
public void shuffle() {
int i = r.nextInt(numCards-1);
int j = r.nextInt(numCards-1);
for(int repeat = 0;repeat <=100000;repeat++) {
Card temp = new Card(0, 0);
temp = cards[i];
cards[i] = cards[j];
cards[j] = cards[i];
}
}
public static Card[] makeFullDeck() {
for (int i = 0;i < 52;i++) {
for (int suitCount = 0;suitCount < 4; suitCount++) {
for (int valueCount = 1;valueCount < 14; valueCount++) {
cards[i] = new Card(suitCount, valueCount);
//Card c = new Card(suitCount, valueCount);
//cards.addToBottom( c);
}
}
}
CardPile.shuffle();
return cards;
}
}
This code is the type definition for objects like the deck and the players' hands. Here's the error I get when I try to compile it:
CardPile.java:31: cannot find symbol
symbol : method length()
location: class Card[]
for(int shift = 1; shift < (cards.length() - i); shift++) {
^
CardPile.java:43: cannot find symbol
symbol : method length()
location: class Card[]
for(int shift = 1; shift < cards.length(); shift++) {
^
CardPile.java:89: non-static variable cards cannot be referenced from a static context
cards[i] = new Card(suitCount, valueCount);
^
CardPile.java:95: non-static method shuffle() cannot be referenced from a static context
CardPile.shuffle();
^
CardPile.java:96: non-static variable cards cannot be referenced from a static context
return cards;
^
5 errors
I just don't get the whole non/static thing. I thought I did, but apparently not... Also, if "cards" is a Card array (I've definied the Card type in another class), why can't I get the length of it like I would say an int array? Sorry it's so long, I just thought I might as well post all of it.
Thanks!