/* * TASK 4 * archived at https://git.femboy.science/femsci/ppj/src/branch/task/4 * by femsci */ import java.util.Arrays; public class Card { public Card(Colour colour, Rank rank) { this.colour = colour; this.rank = rank; } public static Card fromRaw(int colour, int rank) { return new Card(Colour.from(colour), Rank.from(rank)); } public static Card random() { return fromRaw((int) (Math.random() * 4 + 1), (int) (Math.random() * 13 + 2)); } private Colour colour; private Rank rank; public String getCardString() { return String.format("%s of %ss", rank.getName(), colour.getName()); } protected enum Colour { SPADE(1), CLUB(2), DIAMOND(3), HEART(4); private int raw; Colour(int c) { raw = c; } public static Colour from(int colour) { // modification-proof mapping return Arrays.stream(Colour.values()).filter(c -> c.raw == colour).findFirst().orElseThrow(); } public String getName() { return String.format("%s%s", name().charAt(0), name().substring(1).toLowerCase()); } } protected enum Rank { DEUCE(2), TREY(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14); private int raw; Rank(int c) { raw = c; } public static Rank from(int rank) { // modification-proof mapping return Arrays.stream(Rank.values()).filter(r -> r.raw == rank).findFirst().orElseThrow(); } public String getName() { return String.format("%s%s", name().charAt(0), name().substring(1).toLowerCase()); } } }