I made a server side of an online card game ⑥

Last time here

Create a Player class

We will implement Player. Players have one hand, one field, one graveyard, and one deck. Also, each object is made private so that it cannot be directly touched from the outside. It's a bit verbose, but if you want to mess with just one object, such as a deck shuffle, I'll delegate the process.


public class Player {

    private final Hand hand;
    private final Field field;
    private final Cemetery cemetery;
    private final Deck deck;
    private boolean canAttack;

    public Player(Card[] deck_list) {
        this.hand = new Hand();
        this.field = new Field();
        this.cemetery = new Cemetery();
        this.deck = new Deck(deck_list);
        this.setCanAttack(true);
    }

    public boolean canAttack() {
        return canAttack;
    }

    public void setCanAttack(boolean canAttack) {
        this.canAttack = canAttack;
    }

}

Now, we will implement various other methods.



import javafx.util.Pair;

import java.util.ArrayList;
import java.util.List;

import static com.github.javakky.blackpoker4j.Card.Job.*;

public class Player {

    //Abbreviation
    
    /**
     *Function to draw one card.
     */
    public void draw() {
        this.hand.addCard(this.deck.draw());
    }

    /**
     *Function to shuffle the deck.
     */
    public void shuffle() {
        this.deck.shuffle();
    }

    /**
     *Draw multiple cards and add them to your hand.
     *
     * @param number The number to draw.
     */
    public void draws(int number) {
        for (int i = 0; i < number; i++) {
            this.shuffle();
        }
    }

    /**
     *Do at the beginning of the game,Card flipping process.
     *Put the top of the deck in the graveyard(First attack / second attack decision).
     *
     * @return The suit and number of the rolled card
     */
    public Card firstStep() {
        Card card = this.deck.first_step();
        this.cemetery.addCard(card);
        return card.clone();
    }

    /**
     *Charge all soldiers and barriers in your field.
     */
    public void charge() {
        this.field.chargeAll();
        this.field.recoveryAll();
    }

    /**
     *Find out if you have to discard your hand.
     */
    public boolean mustCleanUp() {
        return this.hand.cardSize() > 7;
    }

    /**
     *Choose your hand and put it in the graveyard.
     * {@link #mustCleanUp()}If is not true,Returns false.
     *
     * @param index The number of the hand to discard
     * @return can be executed(Discard the card)Whether or not
     */
    public boolean cleanUp(int index) {
        if (!mustCleanUp()) return false;
        this.cemetery.addCard(this.hand.getCard(index));
        return true;
    }

    /**
     *Take 1 point of damage(Put one card from the deck into the graveyard).
     */
    public void damage() {
        this.cemetery.addCard(this.deck.damage());
    }

    /**
     *Take damage for points(Put the specified number of cards from the deck into the graveyard).
     *
     * @param number The number of damages received.
     */
    public void damages(int number) {
        for (int i = 0; i < number; i++)
            this.damage();
    }

    /**
     *Summon a barrier.
     *Take 1 point of damage,Set one card selected from your hand.
     *
     * @param index Coordinates of the card to be set.
     */
    public void setBarrier(int index) {
        this.damage();
        this.field.setBarrier(this.hand.getCard(index));
    }

    /**
     *Summon soldiers.
     *Drive one barrier,Received 1 point of damage,Take out a card from your hand face up.
     *Up to 2 to 10 cards can be issued.
     *The selected card does not meet the conditions ・ Returns false if the selected barrier is in the drive state.
     *
     * @param handIndex The number of the card you want to put out is in your hand
     * @param barrierIndex What is the barrier to drive
     * @return Did you succeed in summoning?
     */
    public boolean summonSoldier(int handIndex, int barrierIndex) {
        if (this.field.isDrive(barrierIndex) && this.hand.getJob(handIndex) != SOLDIER) {
            return false;
        }
        this.damage();
        this.field.drive(barrierIndex, false);
        Card card = this.hand.getCard(handIndex);
        this.field.summon(card);
        return true;
    }

    /**
     *Summon Ace.
     *Received 1 point of damage,Take out a card from your hand face up.
     *1 card can be issued.
     *Returns false if the selected card does not meet the conditions.
     *
     * @param handIndex The number of the card you want to put out is in your hand
     * @return Did you succeed in summoning?
     */
    public boolean summonAce(int handIndex) {
        if (this.hand.getJob(handIndex) != ACE) {
            return false;
        }
        this.damage();
        Card card = this.hand.getCard(handIndex);
        this.field.summon(card);
        return true;
    }

    /**
     *Summon a hero.
     *Drive two barriers,Received 1 point of damage,Take out a card from your hand face up.
     *Cards that can be issued are 11 to 13.
     *The selected card does not meet the conditions ・ Returns false if the selected barrier is in the drive state.
     *
     * @param handIndex The number of the card you want to put out is in your hand
     * @param barrierIndex1 What is the barrier to drive(1st)
     * @param barrierIndex2 What is the barrier to drive(2nd)
     * @return Did you succeed in summoning?
     */
    public boolean summonHero(int handIndex, int barrierIndex1, int barrierIndex2) {
        if (this.field.isDrive(barrierIndex1) && this.field.isDrive(barrierIndex2) && this.hand.getJob(handIndex) != HERO) {
            return false;
        }
        this.damage();
        this.field.drive(barrierIndex1, false);
        this.field.drive(barrierIndex2, false);
        Card card = this.hand.getCard(handIndex);
        this.field.summon(card);
        return true;
    }

    /**
     *Summon a magician.
     *Drive one barrier,Discard 1 card from your hand,Take out a card from your hand face up.
     *The card that can be issued is Joker.
     *The selected card does not meet the conditions ・ Returns false if the selected barrier is in the drive state.
     *
     * @param handIndex The number of the card you want to put out is in your hand
     * @param barrierIndex What is the barrier to drive
     * @param costHandIndex What number is in your hand to discard
     * @return Did you succeed in summoning?
     */
    public boolean summonMagician(int handIndex, int costHandIndex, int barrierIndex) {
        if (this.field.isDrive(barrierIndex) && this.hand.getJob(handIndex) != MAGICIAN) {
            return false;
        }
        this.field.drive(barrierIndex, false);
        if (handIndex > costHandIndex) {
            Card card = this.hand.getCard(handIndex);
            this.field.summon(card);
            this.cemetery.addCard(this.hand.getCard(costHandIndex));
        } else {
            this.cemetery.addCard(this.hand.getCard(costHandIndex));
            Card card = this.hand.getCard(handIndex);
            this.field.summon(card);
        }
        return true;
    }

    /**
     *Returns the card information of the attacking soldier.
     *Of the cards present at the selected coordinates,Attackable card object(copy)return it.
     *The card drives the returning soldier.
     *
     * @param soldierIndex List of soldier numbers you want to attack
     * @return Card list of soldiers who could actually attack(copy)
     */
    public Card[] declarationAttack(int[] soldierIndex) {
        List<Card> attackers = new ArrayList<>();
        for (int index : soldierIndex) {
            if (this.field.canAttack(index)) {
                attackers.add(this.field.getSoldierData(index));
                this.field.drive(index, false);
            }
        }
        return attackers.toArray(new Card[attackers.size()]);
    }

    /**
     *Alternation of generations.
     *Called when a non-soldier face-up card is destroyed.
     *Send from deck to graveyard until non-soldier turns,If you turn it over, add it to your hand.
     */
    public void alternation() {
        Card card;
        while (true) {
            card = this.deck.takeTop();
            if(Card.isNotSoldier(card.getJob())) break;
            this.cemetery.addCard(card);
        }
        this.hand.addCard(card);
    }

    /**
     *Called when a card on the field is destroyed.
     * @param isFront Whether the card is face up.
     * @param index What is the number of the card to be destroyed in that direction?
     */
    public void destruction(boolean isFront, int index) {
        Card card = this.field.destruction(index, isFront);
        if(Card.isNotSoldier(card.getJob())) {
            this.alternation();
        }
        this.cemetery.addCard(card);
    }

    /**
     *Block with soldiers(Multiple)
     * @param soldierIndex A list of the order of soldiers you want to block
     * @return Total power when blocking
     */
    public int declarationDefense(int[] soldierIndex) {
        int number = 0;
        for (int index: soldierIndex) {
            this.field.drive(index, true);
            number += this.field.getAttack(index);
        }
        return number;
    }

    /**
     *Block with a barrier(Multiple)
     * @param index The order of the barriers you want to block
     * @return The number of the barrier to block
     */
    public int declarationDefenseBarrier(int index) {
        this.field.drive(index, false);
        this.field.open(index);
        return this.field.getNumber(index, false);
    }

}

We have implemented possible actions such as draws, attacks, and attacks. See Official Page for rules.

Next time, we plan to implement a game board (game console?).

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 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
Ruby: I made a FizzBuzz program!
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