I will make a hand class. This time, I was only planning to finish in the hand class, but since the hand class was almost the same as Deck, I will do something else.
The method implementation is the same as Deck, and the field adopted ArrayList due to the large number of random accesses.
import java.util.ArrayList;
import java.util.List;
public class Hand implements CardSheaf {
private List<Card> hand = new ArrayList<>();
@Override
public int indexOf(Card card) {
return CardSheaf.indexOf(hand, card);
}
@Override
public int cardSize() {
return hand.size();
}
@Override
public void addCard(Card card) {
CardSheaf.addCard(hand, card);
}
@Override
public void removeCard(Card card) {
CardSheaf.removeCard(hand, card);
}
@Override
public void removeCard(int index) {
CardSheaf.removeCard(hand, index);
}
@Override
public Card find(int number, Card.Mark mark) {
return CardSheaf.find(hand, number, mark);
}
@Override
public int indexOf(int number, Card.Mark mark) {
return CardSheaf.indexOf(hand, number, mark);
}
@Override
public Card getCard(int index) {
return CardSheaf.getCard(hand, index);
}
@Override
public Card takeCard(int index) {
return CardSheaf.takeCard(hand, index).getValue();
}
}
The graveyard class is more like a deck than your hand, so I won't explain it.
import java.util.Stack;
public class Cemetery implements CardSheaf {
Stack<Card> cemetery = new Stack<>();
@Override
public int indexOf(Card card) {
return CardSheaf.indexOf(cemetery, card);
}
@Override
public int cardSize() {
return cemetery.size();
}
@Override
public void addCard(Card card) {
CardSheaf.addCard(cemetery, card);
}
@Override
public void removeCard(Card card) {
CardSheaf.removeCard(cemetery, card);
}
@Override
public void removeCard(int index) {
CardSheaf.removeCard(cemetery, index);
}
@Override
public Card find(int number, Card.Mark mark) {
return CardSheaf.find(cemetery, number, mark);
}
@Override
public int indexOf(int number, Card.Mark mark) {
return CardSheaf.indexOf(cemetery, number, mark);
}
@Override
public Card getCard(int index) {
return CardSheaf.getCard(cemetery, index);
}
@Override
public Card takeCard(int index) {
return CardSheaf.takeCard(cemetery, index).getValue();
}
}
First, implement it like any other class. Also, prepare a list with separate front and back cards for easy removal. Considering the large number of random accesses, ArrayList is used.
package blackpoker;
import java.util.ArrayList;
import java.util.List;
public class Field implements CardSheaf {
private List<Card> front = new ArrayList<>();
private List<Card> back = new ArrayList<>();
private List<Card> fields = new ArrayList<>();
@Override
public int indexOf(Card card) {
if (card.isFront()) {
return CardSheaf.indexOf(front, card);
}
return CardSheaf.indexOf(back, card);
}
@Override
public int cardSize() {
return front.size() + back.size();
}
/**
*Returns the number of cards.
* @param isFront Whether it is about the front card
* @number of return cards
*/
public int cardSize(boolean isFront) {
return isFront ? front.size() : back.size();
}
@Override
public void addCard(Card card) {
CardSheaf.addCard(fields, card);
if (card.isFront()) {
CardSheaf.addCard(front, card);
return;
}
CardSheaf.addCard(back, card);
}
/**
*Add a card to your data structure.
* @param isFront Whether the card you want to delete is the front side.
*/
public void addCard(Card card, boolean isFront) {
CardSheaf.addCard(fields, card);
if (isFront) {
CardSheaf.addCard(front, card);
return;
}
CardSheaf.addCard(back, card);
}
@Override
public void removeCard(Card card) {
CardSheaf.addCard(fields, card);
if (card.isFront()) {
CardSheaf.removeCard(front, card);
return;
}
CardSheaf.removeCard(back, card);
}
@Override
public void removeCard(int index) {
Card rm = CardSheaf.getCard(fields, index);
CardSheaf.removeCard(fields, rm);
if (rm.isFront()) {
CardSheaf.removeCard(front, rm);
return;
}
CardSheaf.removeCard(back, rm);
}
/**
*Remove the card from the data structure.
* @param isFront Whether the card you want to delete is the front side.
*/
public void removeCard(int index, boolean isFront) {
if (isFront) {
CardSheaf.removeCard(front, index);
return;
}
CardSheaf.removeCard(back, index);
}
@Override
public Card find(int number, Card.Mark mark) {
Card tmp = CardSheaf.find(front, number, mark);
return tmp != null ? tmp : CardSheaf.find(back, number, mark);
}
@Override
public int indexOf(int number, Card.Mark mark) {
int tmp = CardSheaf.indexOf(front, number, mark);
return tmp >= 0 ? tmp : CardSheaf.indexOf(back, number, mark);
}
@Override
public Card getCard(int index) {
return CardSheaf.getCard(fields, index);
}
public Card getCard(int index, boolean isFront) {
if (isFront) {
return CardSheaf.getCard(front, index);
}
return CardSheaf.getCard(back, index);
}
@Override
public Card takeCard(int index) {
return CardSheaf.takeCard(fields, index).getValue();
}
/**
*Remove the card at the specified number.
*The card is removed from the data structure.
* @param index Index of the card you want
* @param isFront card is table(Not a barrier)whether. True for tables
*/
public Card takeCard(int index, boolean isFront) {
if (isFront) {
return CardSheaf.takeCard(front, index).getValue();
}
return CardSheaf.takeCard(back, index).getValue();
}
}
For functions other than indexOf, prepare functions that can be specified for each card on the back and front sides. Also, if there is no second argument in add or remove, I decided to refer to isFront of the passed Card object.
import java.util.ArrayList;
import java.util.List;
public class Field implements CardSheaf {
private List<Card> front = new ArrayList<>();
private List<Card> back = new ArrayList<>();
private List<Card> fields = new ArrayList<>();
//Abbreviation
/**
*Set the barrier.
*/
public void setBarrier(Card card) {
if (card.isFront()) card.setFront(false);
if (card.isCharge()) card.setCharge(false);
if (card.canAttack()) card.setFront(false);
addCard(card, false);
}
/**
*Put out the card face up.(Soldier etc.)
*/
public void summon(Card card) {
if (!card.isFront()) card.setFront(true);
if (card.isCharge()) card.setCharge(false);
addCard(card, true);
switch (card.getJob()) {
case ACE:
case MAGICIAN:
card.setCanAttack(true);
break;
default:
card.setCanAttack(false);
break;
}
}
/**
*Can attack(Front side display)Return the card in a list.
*/
public List<Card> lookAttackable(){
List<Card> attackable = new ArrayList<>();
for (Card tmp: front){
if(tmp.canAttack()) attackable.add(tmp);
}
return attackable;
}
/**
*Designated(Front side display)Make the card attackable.
* @param index The index of the card you want to be able to attack.
*/
public void recovery(int index){
front.get(index).setCanAttack(true);
}
/**
*All(Front side display)Make the card attackable.
*/
public void recoveryAll(){
for(Card tmp: front){
if(tmp != null) tmp.setCanAttack(true);
}
}
/**
*Charge the specified card.
* @param index The index of the card you want to charge.
* @param isFront Whether the card is on the front side.
*/
public void charge (int index, boolean isFront){
if(isFront){
front.get(index).setCharge(true);
}else{
back.get(index).setCharge(true);
}
}
/**
*Charge all face-up cards.
*/
public void chargeAllFront(){
for(Card tmp: front){
tmp.setCharge(true);
}
}
/**
*Charge all the cards displayed on the back side.
*/
public void chargeAllBack(){
for(Card tmp: back){
tmp.setCharge(true);
}
}
/**
*Charge all cards.
*/
public void chargeAll(){
chargeAllFront();
chargeAllBack();
}
/**
*Drive the specified card.
* @param index The index of the card you want to drive.
* @param isFront Whether the card is on the front side.
*/
public void drive (int index, boolean isFront){
if(isFront){
front.get(index).setCharge(false);
}else{
back.get(index).setCharge(false);
}
}
/**
*Drive all face-up cards.
*/
public void driveAllFront(){
for(Card tmp: front){
tmp.setCharge(false);
}
}
/**
*Drive all the cards on the back side.
*/
public void driveAllBack(){
for(Card tmp: back){
tmp.setCharge(false);
}
}
/**
*Drive all the cards.
*/
public void driveAll(){
driveAllFront();
driveAllBack();
}
/**
*Destroy the card.
* @param index The index of the card you want to destroy.
* @param isFront Whether the card you want to destroy is face up.
* @return The object of the destroyed card.
*/
public Card destruction (int index, boolean isFront){
return takeCard(index, isFront);
}
/**
*Turn the back card to the front.
* @param index The index of the card you want to be on the front side.
*/
public void open(int index){
Card tmp = takeCard(index, false);
tmp.setFront(true);
addCard(tmp, true);
}
/**
*Whether the specified card is in the drive state.
*/
public boolean isDrive(int barrierIndex) {
return !back.get(barrierIndex).isCharge();
}
/**
*Find out if the selected soldier can attack.
* @param index Order of soldiers you want to look up(In the face-up card)
*/
public boolean canAttack(int index) {
return front.get(index).canAttack();
}
/**
*Returns a copy of the selected soldier's object.
* @param index Order of soldiers you want to look up(In the face-up card)
* @return A copy of the soldier card object
*/
public Card getSoldierData(int index) {
return front.get(index).clone();
}
/**
*Returns the number of the selected soldier.
* @param index Order of soldiers you want to look up(In the same direction card)
* @Is the param isFront card a table?
* @return card number
*/
public int getNumber(int index, boolean isFront) {
List<Card> list = isFront ? front: back;
return list.get(index).getNumber();
}
}
Basically as I wrote in the comment. However, I will explain only the summoning for the time being. The summon method, like setBarrier, adds cards to the entire list and the displayed card list, and aligns whether they are front or not and whether they are charged. In addition, ace (1) and magician (joker) set canAttack to true because of the rule that they do not get sick.
Up to here for this time. Next time, I would like to create a Player class.
Recommended Posts