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

Last time here

policy

At the end of the last article, I showed a simple UML diagram. In future articles, I would like to implement it from the bottom to the top of the figure.

Make a card class

First, let's make a minimum card class. BlackPoker is a game that uses playing cards, so you will need marks and numbers. There are 4 types of marks, ♠, ♦, ♥, and ♣, but since this game uses a joker, let's assume that there are 5 types of marks. The mark type can be int, but it is desirable not to enter invalid values as much as possible, so we will define an enumeration.

public class Card {
    
    public int number;
    public Mark mark;
    
    public Card(int number, Mark mark){
        this.number = number;
        this.mark = mark;
    }
    
    public enum Mark {
        SPADE, DIAMOND, HEART, CLOVER, JOKER;
    }
    
}

It's not finished yet, right? In the card class, in addition to the information held by the card of the playing card, it is necessary to hold the data virtually held in the game.

The parameters required for each card, such as barriers and soldiers, will change depending on the situation. This time, for the sake of simplicity of implementation, we will ask you to have parameters in all situations. This is because the barrier does not require any parameters other than the card number, so the Card class has only the parameters for soldiers at best.

This time, we will have the following data retained.

Parameters Mold Supplement
Offensive power int It may change when spelling is implemented in the future.
Profession Enumerator More efficient than comparing numbers every time
Is it possible to attack boolean
Is it charged boolean Used by both barriers and soldiers
Card orientation boolean If it's the back, it's a barrier

import static blackpoker.Card.Job.*;

public class Card {

    public int number;
    public int attack;
    public boolean canAttack;
    public Mark mark;
    public boolean isCharge;
    public boolean isFront;
    public Job job;

    public Card(int number, Mark mark) {
        this.number = number;
        this.attack = this.number;
        this.canAttack = false;
        this.mark = mark;
        this.isCharge = true;
        this.isFront = false;
        this.job = null;
        if (this.number >= 2 && this.number <= 10) {
            this.job = SOLDIER;
        } else if (this.number >= 11 && this.number <= 13) {
            this.job = HERO;
        } else if (this.number == 1) {
            this.job = ACE;
            this.canAttack = true;
        } else if (this.number == 0) {
            this.job = MAGICIAN;
            this.canAttack = true;
        }
    }

    enum Mark {
        SPADE, DIAMOND, HEART, CLOVER, JOKER;
    }

    enum Job {
        SOLDIER, HERO, ACE, MAGICIAN;
    }
}

Postscript

After all public variables are likely to get angry, so I will write them properly.

import static blackpoker.Card.Job.*;

public class Card {

    private int number;
    private int attack;
    private boolean canAttack;
    private Mark mark;
    private boolean isCharge;
    private boolean isFront;
    private Job job;

    //Abbreviation

    public int getNumber() {
        return number;
    }

    private void setNumber(int number) {
        this.number = number;
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public boolean isCanAttack() {
        return canAttack;
    }

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

    public Mark getMark() {
        return mark;
    }

    private void setMark(Mark mark) {
        this.mark = mark;
    }

    public boolean isCharge() {
        return isCharge;
    }

    public void setCharge(boolean charge) {
        isCharge = charge;
    }

    public boolean isFront() {
        return isFront;
    }

    public void setFront(boolean front) {
        isFront = front;
    }

    public Job getJob() {
        return job;
    }

    private void setJob(Job job) {
        this.job = job;
    }
}

Also, implement Clonable.


public class Card implements Cloneable {
    //Abbreviation
    @Override
    public Card clone(){
        return new Card(number, attack, canAttack, mark, isCharge, isFront, job);
    }
}

Next time, I will make it in my hand or deck.

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 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 an annotation in Java.
I made a matching app (Android app)
I made a package.xml generation tool.
[Android] I made a pedometer app.
[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 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