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.
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;
}
}
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.
Recommended Posts