I worked a little scam. .. .. We will not implement a deck or hand this time.
Instead, I would like to create an abstract class that holds the classes that hold the cards together.
Deck, hand, graveyard, etc. all hold multiple cards in some data structure. So I created a bundle of cards interface.
public interface CardSheaf {
/**
*Returns where the passed card object is in the data structure.
*Please make sure that the data structure is a list or an array.
* @param card The object of the card whose number you want to know
* @The number in the structure of the return argument object
*/
int indexOf(Card card);
/**
*Returns the number of cards.
* @number of return cards
*/
int cardSize();
/**
*Add a card to your data structure.
*/
void addCard(Card card);
/**
*Remove the card from the data structure.
*/
void removeCard(Card card);
/**
*Remove the card from the data structure.
*/
void removeCard(int index);
/**
*Returns a card whose number and mark match.
*/
Card find(int number, Card.Mark mark);
/**
*Returns the number of the card whose number and mark match.
*/
int indexOf(int number, Card.Mark mark);
/**
*Returns the card at the specified number.
*The element is not deleted.
* @param index The number of the card you want
*/
Card getCard(int index);
/**
*Remove the card at the specified number.
*The card is removed from the data structure.
* @param index The number of the card you want
*/
Card takeCard(int index);
}
Also, I want the data structure to be an array or a list, so I created an interface to do so as much as possible.
Also, since it is possible that functions such as retrieval and search can be shared, prepare them with static methods. (I don't need it, but I don't want to write the same process over and over again later)
Also, since it is a static method, I got a list as an argument for the time being, and returned the list if there is a change.
Pairs and tuples are not in the java standard, so I used Map.Entry instead.
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public interface CardSheaf {
//Abbreviation
static int indexOf(List<Card> list, Card card){
int i;
for (i = 0; i < list.size(); i++) {
Card c = list.get(i);
if(c == null) continue;
if(c.equals(card)) break;
}
if(i == list.size()) return -1;
return i;
}
static List<Card> addCard(List<Card> list, Card card){
list.add(card);
return list;
}
static List<Card> removeCard(List<Card> list, Card card){
list.remove(card);
return list;
}
static List<Card> removeCard(List<Card> list, int index){
list.remove(index);
return list;
}
static Card find(List<Card> list, int number, Card.Mark mark){
for (Card card: list){
if(card == null) continue;
if(card.getMark() == mark && card.getNumber() == number) return card;
}
return null;
}
static int indexOf(List<Card> list, int number, Card.Mark mark){
int i;
for (i = 0; i < list.size(); i++){
Card card = list.get(i);
if(card == null) continue;
if(card.getMark() == mark && card.getNumber() == number) break;
}
if(i == list.size()) return -1;
return i;
}
static Card getCard(List<Card> list, int index){
return list.get(index);
}
static Map.Entry<List<Card>, Card> takeCard(List<Card> list, int index){
Card card = list.get(index);
list.remove(card);
return new HashMap.SimpleEntry<List<Card>, Card>(list, card);
}
Recommended Posts