[JAVA] I made a server side of an online card game ⑤

Last time here

Create a hand class

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();
    }

}

Make a graveyard class

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();
    }   
}

Create a field class

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.

Next time

Recommended Posts

I made a server side of an online card game ⑤
I made a server side of an online card game ③
I made a server side of an online card game ⑥
I made a server side of an online card game ④
I made a server side of an online card game ②
I made the server side of an online card game ①
I made an eco server with scala
I made a rock-paper-scissors game in Java (CLI)
I made a Docker image of SDAPS for Japanese
I made a simple calculation problem game in Java
I made a Restful server and client in Spring.
A brief explanation of a maze game made in Java for a cousin of an elementary school student
I made a Japanese version of Rails / devise automatic email
Created a server-side for online card games [Table of Contents]
I made a chat app.
I tried JAX-RS and made a note of the procedure
A story of connecting to a CentOS 8 server with an old Ansible
I made a gem to post the text of org-mode to qiita
I made a tool to output the difference of CSV file
I made a shopify app @java
I made a GUI with Swing
I made a simple recommendation function.
I made an annotation in Java.
I made a matching app (Android app)
I made a package.xml generation tool.
[Android] I made a pedometer app.
I made an app to scribble with PencilKit on a PDF file
[Ruby] I made a simple Ping client
I made a risky die with Ruby
I made a plugin for IntelliJ IDEA
I made a rock-paper-scissors app with kotlin
I made a calculator app on Android
I made a new Java deployment tool
I made a rock-paper-scissors app with android
A simple example of an MVC model
I made a bulletin board using Docker 1
Learn Java with Progate → I will explain because I made a basic game myself
Rails6 I want to make an array of values with a check box
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit