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

Last time here

Create a Deck class

Yes, this time I will finally make a deck. Let's firmly use the paper bundle interface that we made last time.

First, make a Class

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.

Implement CardSheaf.

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.

Define the methods required for Deck

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.

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 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 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