I made a server side of an online card game ③

Last time here

apology

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.

Make a bunch of cards interface

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

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
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
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 created a Docker image of a container for learning OpenAI Gym
How to check for the contents of a java fixed-length string
A list of rawValues for UITextContentType.
micronaut document table of contents memo
Let's improve our business! Table of contents