The issue will be pretty clear to those of you with experience. As the title suggests, I'm having trouble accessing private variables from other classes. I'll tried to attach all the relevant files, but it wouldn't let me, so I'll try to explain my issue first then post all the code (sorry):
I'm trying to write a program to play Go Fish, and I've defined several types - I'll give you the short version: Card objects have the private variable "value" (the Card rank), CardPile objects have the private variable "cards" (a Card array), and Player objects have the private variable "pile" (a CardPile representing their hand). I want to know access the "value" of a Card in the "cards" of "pile"... Make sense?
Card class:
public class Card {
//Ace = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack = 11, etc.
//Spades = 0, Hearts = 1, Clubs = 2, Diamonds = 3
private int suit;
private int value;
public Card(int theSuit, int theValue) {
this.suit = theSuit;
this.value = theValue;
if (theSuit < 0 && theSuit > 3) {
System.out.println("Not a valid suit!");
}
if (theValue < 1 && theValue > 13) {
System.out.println("Not a valid card value!");
}
}
public int getSuit() {
return suit;
}
public int getValue() {
return value;
}
public String getSuitAsString() {
if (suit == 0) {
return "Spades";
} else if (suit == 1) {
return "Hearts";
} else if (suit == 2) {
return "Clubs";
} else if (suit == 3) {
return "Diamonds";
} else {
return "Invalid Suit";
}
}
public String getValueAsString() {
if (value == 1) {
return "Ace";
} else if (value == 2) {
return "Two";
} else if (value == 3) {
return "Three";
} else if (value == 4) {
return "Four";
} else if (value == 5) {
return "Five";
} else if (value == 6) {
return "Six";
} else if (value == 7) {
return "Seven";
} else if (value == 8) {
return "Eight";
} else if (value == 9) {
return "Nine";
} else if (value == 10) {
return "Ten";
}
// else if (value >= 2 && value <= 10) {
// return value;
// }
else if (value == 11) {
return "Jack";
} else if (value == 12) {
return "Queen";
} else if (value == 13) {
return "King";
} else {
return "Invalid Card Value";
}
}
public String toString() {
String cardName = getValueAsString() + " of " + getSuitAsString();
System.out.println(cardName);
return cardName;
}
public boolean equals(Card other) {
if (other.suit == this.suit && other.value == this.value) {
return true;
} else {
return false;
}
}
}
CardPile class:
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) {
Card removeMe = new Card(0, 0);
removeMe = c;
for(int i = 0;i < 52;i++) {
if (cards[i].getValue() == c.getValue()) {
//Add the following code to the line above if suit
//needs to match as well: && cards[i].getSuit() == c.getSuit()
cards[i] = null;
for(int shift = 1; shift < (cards.length - i); shift++) {
cards[(i+shift)] = cards[(i+(shift-1))];
}
numCards--;
} else {
return null;
}
}
return removeMe;
}
public Card removeTop() {
Card topCard = new Card(0, 0);
topCard = cards[0];
cards[0] = null;
int i = 0;
for(int shift = 1; shift < cards.length; shift++) {
cards[shift] = cards[(shift+1)];
}
return topCard;
}
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() {
String cardPileString = "";
for(int i = 0;i < 52;i++) {
cardPileString = cards[i].toString() + ", ";
}
return cardPileString;
}
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 CardPile makeFullDeck() {
CardPile deck = new CardPile();
for (int i = 0;i < 52;i++) {
for (int suitCount = 0;suitCount < 4; suitCount++) {
for (int valueCount = 1;valueCount < 14; valueCount++) {
deck.cards[i] = new Card(suitCount, valueCount);
//Card c = new Card(suitCount, valueCount);
//addToBottom( c);
}
}
}
deck.shuffle();
return deck;
}
}
Player class
public class Player {
private boolean[] books;
private CardPile pile;
public Player() {
books = new boolean[13];
for (int i = 0;i < 13; i++) {
books[i] = false;
}
pile = new CardPile();
}
public boolean hasCard(int value) {
if (pile.searchValue(value) != 0) {
return true;
} else {
return false;
}
/* for (int i = 0; i < pile.getNumberCards();i++) {
* if (pile.cards[i].getValue() == value) {
* return true;
* } else {
* return false;
* }
* }
*/
}
public Card[] removeAll(int value) {
Card[] toRemove = new Card[3];
for (int j = 0; j < 3;j++) {
for (int i = 0; i < pile.getNumberCards();i++) {
if (pile.removeCard(value) == value) {
toRemove[j] = pile.cards[i];
}
}
}
return toRemove;
}
public void addAll(Card[] cards) {
Card[] toAdd = cards;
for (int i = 0;i < toAdd.length;i++) {
for (int count = 0; count < 1;count++) {
for (int j = 0;j < pile.getNumberCards();j++) {
if (pile.cards[j] == null) {
pile.cards[j] = toAdd[i];
}
}
}
}
}
public int getNumberCards() {
int countCards = 0;
for (int i = 0;i < pile.getNumberCards();i++) {
if (pile.cards[i] != null) {
countCards++;
}
}
return countCards;
}
public int countBooks() {
int numBooks = 0;
for (int i = 0;i < books.length;i++) {
if (books[i] == true) {
numBooks++;
}
}
return numBooks;
}
public void addBook(int value) {
books[value - 1] = true;
}
public void printHand() {
pile.toString();
}
}
Card and CardPile compile fine - I think...