All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.godmao.poker.Poker Maven / Gradle / Ivy

There is a newer version: 0.2.6.RELEASE
Show newest version
package com.godmao.poker;

import java.util.*;

public class Poker {

    private int id;
    private int color;
    private int value;

    public Poker(int id, int color, int value) {
        this.id = id;
        this.color = color;
        this.value = value;
    }

    public Poker(int id, Color color, Value value) {
        this(id, color.getType(), value.getType());
    }

    public Poker(int id, Poker poker) {
        this(id, poker.getColor(), poker.getValue());
    }

    @Deprecated
    public Poker(int id) {
        this(id, Cache.idTocv.get(id)[0], Cache.idTocv.get(id)[1]);
    }


    @Deprecated
    public Poker(int color, int value) {
        this(-1, color, value);
    }

    public int getColor() {
        return color;
    }

    public int getValue() {
        return value;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Poker copy() {
        return new Poker(this.id, this.color, this.value);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Poker poker = (Poker) o;
        return color == poker.color && value == poker.value;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return toString(false);
    }

    public String toString(boolean complete) {
        final Color color = Color.valueOf(getColor());
        final Value value = Value.valueOf(getValue());
        final String strcolor = null != color ? color.getDesc() : null;
        final String strvalue = null != value ? value.getDesc() : null;
        final String str;
        if (complete) {
            str = strcolor + strvalue + "(" + getId() + ")";
        } else {
            str = strcolor + strvalue;
        }

        return str;
    }

    public static final Poker 红桃A  = new Poker(1);
    public static final Poker 梅花A  = new Poker(2);
    public static final Poker 黑桃A  = new Poker(3);
    public static final Poker 方块A  = new Poker(4);
    public static final Poker 红桃1  = 红桃A;
    public static final Poker 梅花1  = 梅花A;
    public static final Poker 黑桃1  = 黑桃A;
    public static final Poker 方块1  = 方块A;
    public static final Poker 红桃a  = 红桃A;
    public static final Poker 梅花a  = 梅花A;
    public static final Poker 黑桃a  = 黑桃A;
    public static final Poker 方块a  = 方块A;
    public static final Poker 红桃2  = new Poker(5);
    public static final Poker 梅花2  = new Poker(6);
    public static final Poker 黑桃2  = new Poker(7);
    public static final Poker 方块2  = new Poker(8);
    public static final Poker 红桃3  = new Poker(9);
    public static final Poker 梅花3  = new Poker(10);
    public static final Poker 黑桃3  = new Poker(11);
    public static final Poker 方块3  = new Poker(12);
    public static final Poker 红桃4  = new Poker(13);
    public static final Poker 梅花4  = new Poker(14);
    public static final Poker 黑桃4  = new Poker(15);
    public static final Poker 方块4  = new Poker(16);
    public static final Poker 红桃5  = new Poker(17);
    public static final Poker 梅花5  = new Poker(18);
    public static final Poker 黑桃5  = new Poker(19);
    public static final Poker 方块5  = new Poker(20);
    public static final Poker 红桃6  = new Poker(21);
    public static final Poker 梅花6  = new Poker(22);
    public static final Poker 黑桃6  = new Poker(23);
    public static final Poker 方块6  = new Poker(24);
    public static final Poker 红桃7  = new Poker(25);
    public static final Poker 梅花7  = new Poker(26);
    public static final Poker 黑桃7  = new Poker(27);
    public static final Poker 方块7  = new Poker(28);
    public static final Poker 红桃8  = new Poker(29);
    public static final Poker 梅花8  = new Poker(30);
    public static final Poker 黑桃8  = new Poker(31);
    public static final Poker 方块8  = new Poker(32);
    public static final Poker 红桃9  = new Poker(33);
    public static final Poker 梅花9  = new Poker(34);
    public static final Poker 黑桃9  = new Poker(35);
    public static final Poker 方块9  = new Poker(36);
    public static final Poker 红桃10 = new Poker(37);
    public static final Poker 梅花10 = new Poker(38);
    public static final Poker 黑桃10 = new Poker(39);
    public static final Poker 方块10 = new Poker(40);
    public static final Poker 红桃j  = new Poker(41);
    public static final Poker 梅花j  = new Poker(42);
    public static final Poker 黑桃j  = new Poker(43);
    public static final Poker 方块j  = new Poker(44);
    public static final Poker 红桃J  = 红桃j;
    public static final Poker 梅花J  = 梅花j;
    public static final Poker 黑桃J  = 黑桃j;
    public static final Poker 方块J  = 方块j;
    public static final Poker 红桃11 = 红桃j;
    public static final Poker 梅花11 = 梅花j;
    public static final Poker 黑桃11 = 黑桃j;
    public static final Poker 方块11 = 方块j;
    public static final Poker 红桃q  = new Poker(45);
    public static final Poker 梅花q  = new Poker(46);
    public static final Poker 黑桃q  = new Poker(47);
    public static final Poker 方块q  = new Poker(48);
    public static final Poker 红桃Q  = 红桃q;
    public static final Poker 梅花Q  = 梅花q;
    public static final Poker 黑桃Q  = 黑桃q;
    public static final Poker 方块Q  = 方块q;
    public static final Poker 红桃12 = 红桃q;
    public static final Poker 梅花12 = 梅花q;
    public static final Poker 黑桃12 = 黑桃q;
    public static final Poker 方块12 = 方块q;
    public static final Poker 红桃k  = new Poker(49);
    public static final Poker 梅花k  = new Poker(50);
    public static final Poker 黑桃k  = new Poker(51);
    public static final Poker 方块k  = new Poker(52);
    public static final Poker 红桃K  = 红桃k;
    public static final Poker 梅花K  = 梅花k;
    public static final Poker 黑桃K  = 黑桃k;
    public static final Poker 方块K  = 方块k;
    public static final Poker 红桃13 = 红桃k;
    public static final Poker 梅花13 = 梅花k;
    public static final Poker 黑桃13 = 黑桃k;
    public static final Poker 方块13 = 方块k;
    public static final Poker 小王   = new Poker(53);
    public static final Poker 大王   = new Poker(54);
    public static final Poker 王小   = 小王;
    public static final Poker 王大   = 大王;

    @Deprecated
    public static Poker[] values() {
        return Cache.pokers;
    }

    public static class PokerBuilder {
        private int color = -1;
        private int value = -1;
        private int id    = -1;

        public PokerBuilder() {
        }

        public PokerBuilder setColor(int color) {
            this.color = color;
            return this;
        }

        public PokerBuilder setValue(int value) {
            this.value = value;
            return this;
        }

        public PokerBuilder setId(int id) {
            this.id = id;
            return this;
        }

        public PokerBuilder setColor(Color color) {
            return this.setColor(color.getType());
        }

        public PokerBuilder setValue(Value value) {
            return this.setValue(value.getType());
        }


        public PokerBuilder color(Color color) {
            return this.setColor(color.getType());
        }

        public PokerBuilder value(Value value) {
            return this.setValue(value.getType());
        }

        public PokerBuilder color(int color) {
            return this.setColor(color);
        }

        public PokerBuilder value(int value) {
            return this.setValue(value);
        }

        public PokerBuilder id(int id) {
            return this.setId(id);
        }

        public Poker build() {
            if (color < 0 && value < 0 && id > 0) {
                return new Poker(id);
            }
            if (color < 0 && value < 0 && id < 0) {
                return new Poker((int) color, (int) value);
            }
            return new Poker(id, (int) color, (int) value);
        }
    }


    public static PokerBuilder builder() {
        return new PokerBuilder();
    }


    public enum Color {
        HONGTAO((int) 1, "♥\uFE0F"),// ♥ 红桃 ♥️
        MEIHUA((int) 2, "♣\uFE0F"),// ♣ 梅花 ♣️
        HEITAO((int) 3, "♠\uFE0F"),// ♠ 黑桃 ♠️
        FANGKUAI((int) 4, "♦\uFE0F"),// ♦ 方块 ♦️
        KING((int) 5, "\uD83E\uDD21"),// 🃏王 🤡
        ;

        private final int    type;
        private final String desc;

        Color(int type, String desc) {
            this.type = type;
            this.desc = desc;
        }

        public int getType() {
            return type;
        }

        public String getDesc() {
            return desc;
        }

        public boolean equals(int type) {
            return this.type == type;
        }

        public static Color valueOf(int type) {
            for (Color color : values()) {
                if (color.equals(type)) {
                    return color;
                }
            }
            return null;
        }
    }

    public enum Value {

        V_A((int) 1, "A"),
        V_2((int) 2, "2"),
        V_3((int) 3, "3"),
        V_4((int) 4, "4"),
        V_5((int) 5, "5"),
        V_6((int) 6, "6"),
        V_7((int) 7, "7"),
        V_8((int) 8, "8"),
        V_9((int) 9, "9"),
        V_10((int) 10, "10"),
        V_J((int) 11, "J"),
        V_Q((int) 12, "Q"),
        V_K((int) 13, "K"),
        V_KING_XIAO((int) 99, "小"),
        V_KING_DA((int) 100, "大");

        private final int    type;
        private final String desc;

        Value(int type, String desc) {
            this.type = type;
            this.desc = desc;
        }

        public int getType() {
            return type;
        }

        public String getDesc() {
            return desc;
        }

        public boolean equals(int type) {
            return this.type == type;
        }

        public static Value valueOf(int type) {
            for (Value value : values()) {
                if (value.equals(type)) {
                    return value;
                }
            }
            return null;
        }
    }


    static class Cache {
        static final int                 pair = 54;
        static final Poker[]             pokers;
        static final Map idTocv;

        static {
            pokers = new Poker[pair];
            idTocv = new HashMap<>();

            int id = 0;
            for (Value value : Value.values()) {
                if (value.equals(Value.V_KING_XIAO) || value.equals(Value.V_KING_DA)) continue;

                for (Color color : Color.values()) {
                    if (color.equals(Color.KING)) continue;
                    pokers[id++] = new Poker(id, color.getType(), value.getType());
                }
            }
            pokers[id++] = new Poker(id, Color.KING.getType(), Value.V_KING_XIAO.getType());
            pokers[id++] = new Poker(id, Color.KING.getType(), Value.V_KING_DA.getType());

            for (int i = 0; i < 2; i++) {
                for (Poker poker : pokers) {
                    idTocv.put(i * pair + (int) poker.getId(), new int[]{poker.getColor(), poker.getValue()});
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy