I'm trying to find a way to delete a specific card or a random card in a deck and return that deleted card. I've created classes for Card and DeckHand. In the DeckHand class, I'm trying to create a delete method that allows the user to pick a card value, delete ONE instance of that card value from the deck, then returns the card that was deleted, and lastly, shorten the array by 1. I'm also trying to make a deleteAny method that deletes a random card from the deck, returns the card that was deleted, and shorten the array by 1.
For the delete method, I'm having trouble finding a way to say:
*if the value input by the user isn't in the deck, print an error message. *if it is, then find the first instance of a card with that value, delete it, and return the card.
I don't understand how to find the first instance of a card with the value and then finding a way to set an available suit to create the instance of the card to then delete it and shift the positions in the array.
I started trying to do an deleteAny method that deletes a random card. I'm able to get the card output to the user that's getting removed, but I'm getting an error with my method. Any ideas?
Card Class:
class Card {
private int _value, _suit;
private String[] _cardValues = {null, "Ace", "2", "3", "4","5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"};
private String[] _cardSuits = {null, "Clubs", "Diamonds", "Hearts", "Spades"};
public Card(int value,int suit) {
_value = value;
_suit = suit;
}
public int getCardValue() {
return _value;
}
public int getCardSuit() {
return _suit;
}
public String toString() {
return _cardValues[_value] + " of " + _cardSuits[_suit];
}
} Deck class:
class DeckHand{
private Card[] _deckHand;
private int _deckSize;
private static final int MAXSIZE = 52;
private Card[] newDeck;
public DeckHand() {
_deckHand = new Card[MAXSIZE];
_deckSize = 0;
}
public DeckHand(int deckSize) {
_deckHand = new Card[MAXSIZE];
int index = 0;
for (int suit = 1; suit <= 4; suit++) {
for (int rank = 1; rank <= 13; rank++) {
_deckHand[index] = new Card(rank, suit);
index++;
}
}
_deckSize = deckSize;
}
//Here's the delete method, but I have no idea what I'm doing here.
public void delete(int value) {
for (int i = 0; i<_deckSize; i++) {
if(_deckHand[i].getCardValue()==value) {
_deckHand[value] = _deckHand[_deckSize-1];
newDeck = new Card[_deckHand.length-1];
} else
System.out.println("\n--------------------------------------"
+ "\nThe deck does not contain that value"
+ "\n--------------------------------------");
}
}
//Here's the deleteAny method, but I'm getting an error
public void deleteAny(Card newCard) {
if(_deckSize >= MAXSIZE) {
newDeck = new Card[_deckHand.length-1];
for(int i = 0; i<_deckSize; ++i)
if(_deckHand[i].equals(newCard)) {
newDeck[i] = _deckHand[i];
}
_deckHand = newDeck;
}
//the error says it has to do with this next line
_deckHand[_deckSize-1] = newCard;
_deckSize-=1;
}
}
Main: Here's part of my main method that uses these delete and deleteAny methods:
case 3:
System.out.println("\nWhich card would you "
+ "like to remove from the deck?");
valueOption();
System.out.print("\tOption: ");
value = keyboard.nextInt();
if(pickDeck == 1) {
standard.delete(value);
} else {
System.out.println("\n-------------------------"
+ "-------------------------------\n"
+ "The card value \"" + values[value]
+ "\" appears "
+ empty.count(value)
+ " times in the deck."
+ "\n---------------------------------"
+ "-----------------------");
}
break;
case 4:
Random generator = new Random();
value = generator.nextInt(13)+1;
suit = generator.nextInt(4)+1;
newCard = new Card(value,suit);
System.out.println("\n--------------------------"
+ "---------------------"
+ "\n" + newCard + " was removed from the "
+ "deck."
+ "\n--------------------------"
+ "---------------------");
if(pickDeck==1)
standard.deleteAny(newCard);
else
empty.deleteAny(newCard);
break;