Yes, this time I will finally make a deck. Let's firmly use the paper bundle interface that we made last time.
public class Deck{
private Stack<Card> deck = new Stack();
}
Yes, we have a deck.
That's a joke, but now we have a Deck class that has a card as a Stack. By the way, Java Stack is a subclass of List.
Since the deck basically only takes out from the top, I used the stack instead of the array or linear list.
public class Deck implements CardSheaf {
private Stack<Card> deck = new Stack<>();
public Deck(List<Card> deck) {
this.deck.addAll(deck);
}
public Deck(Card... deck) {
this.deck.addAll(Arrays.asList(deck));
}
@Override
public int indexOf(Card card) {
return CardSheaf.indexOf(deck, card);
}
//The CardSheaf class does not provide a standard implementation of cardSize, so we implemented it.
//Just retrieve the number of elements.
@Override
public int cardSize() {
return deck.size();
}
@Override
public void addCard(Card card) {
CardSheaf.addCard(deck, card);
}
@Override
public void removeCard(Card card) {
CardSheaf.removeCard(deck, card);
}
@Override
public void removeCard(int index) {
CardSheaf.removeCard(deck, index);
}
@Override
public Card find(int number, Card.Mark mark) {
return CardSheaf.find(deck, number, mark);
}
@Override
public int indexOf(int number, Card.Mark mark) {
return CardSheaf.indexOf(deck, number, mark);
}
@Override
public Card getCard(int index) {
return CardSheaf.getCard(deck, index);
}
@Override
public Card takeCard(int index) {
return CardSheaf.takeCard(deck, index).getValue();
}
}
In the Deck class, since the card is handled as a List, the static method of CardSheaf is used as it is.
import java.util.*;
public class Deck implements CardSheaf {
private Stack<Card> deck = new Stack<>();
/**
*Initialize the deck with the cards in the list.
* @param deck A list of all the cards you want to put in your deck first
*/
public Deck(List<Card> deck) {
this.deck.addAll(deck);
}
/**
*Initialize the deck with the cards in the array.
* @param deck An array of all the cards you want to put in your deck first
*/
public Deck(Card... deck) {
this.deck.addAll(Arrays.asList(deck));
}
//Abbreviation
/**
*Take out the top card of the deck.
*/
public Card take_top() {
return deck.pop();
}
/**
*Used to judge the first attack at the start of the game.
* @return The top card in the deck
*/
public Card first_step() {
return this.take_top();
}
/**
*Draw a card.
* @return The top card in the deck
*/
public Card draw() {
return this.take_top();
}
/**
*Called when damaged.
* @return The top card in the deck
*/
public Card damage() {
return this.take_top();
}
/**
*Shuffle the deck.
*/
public void shuffle() {
Collections.shuffle(deck);
}
/**
*Determine if there are any cards in the deck.
* @return true if there are cards in the deck, false otherwise
*/
public boolean hasCard() {
return cardSize() > 0;
}
}
So that's it for this time.
Next time, I plan to implement my hand.
Recommended Posts