import java.util.Arrays;

/**
 * author:gongyu
 * description:only one
 */
public class CardStorage {
    private long cards1; // 存储一副牌,0~12表示红桃1~K，13~25表示梅花，26~38表示方块，39~51表示黑桃，52大王，53小王
    private long cards2; // 存储另一幅牌

    private final int [] numberOfEveryHandCards;  //1~K 大王  小王

    private int numberOfHandCards;   // 当前玩家手里牌的张数


    public CardStorage() {
        cards1 = 0L;  //俩整型
        cards2 = 0L;
        numberOfHandCards = 0;
        numberOfEveryHandCards = new int[15];
    }

    public long getCards1(){
        return cards1;
    }

    public long getCards2(){
        return cards2;
    }

    public int getNumberOfHandCards(){
        numberOfHandCards = Long.bitCount(cards1) + Long.bitCount(cards2);
        return numberOfHandCards;
    }

    public int[] getNumberOfEveryHandCards(){
        long [][] hand_card_i_j = new long[15][2];
        for(int i=0;i<hand_card_i_j.length-2;i++){
            for(int j=0;j<2;j++){
                hand_card_i_j[i][j] = (1L << (i)) | (1L << (i + 13)) | (1L << (i + 26)) | (1L << (i + 39));
            }
        }
        hand_card_i_j[13][0] = (1L << 52);
        hand_card_i_j[13][1] = (1L << 52);
        hand_card_i_j[14][0] = (1L << 53);
        hand_card_i_j[14][1] = (1L << 53);
        for(int i=0;i<15;i++){
            numberOfEveryHandCards[i] = Long.bitCount(cards1 & hand_card_i_j[i][0]) + Long.bitCount(cards2 & hand_card_i_j[i][1]);
        }
        return numberOfEveryHandCards;
    }


    public int getNumberOfLaiZi(int XIndex){
        int numberoflaizi =0;
        long cards1_laizi = (1L << XIndex);
        long cards2_laizi = (1L << XIndex);
        if ((cards1_laizi & getCards1()) != 0 && (cards2_laizi & getCards2()) != 0){
            numberoflaizi = 2;
        }else if((cards1_laizi & getCards1()) != 0 && (cards2_laizi & getCards2()) == 0){
            numberoflaizi = 1;
        }
        return numberoflaizi;
    }


    // 设置指定位置的牌为1
    public void setCard(int index) {
        if (index >= 0 && index < 108) {
            if((cards1 & (1L << (index % 54))) == 0){
                cards1 |= (1L << (index % 54));
            }else{
                cards2 |= (1L << (index % 54));
            }
        } else {
            throw new IllegalArgumentException("Invalid card index: " + index);
        }
    }

    public void setCardDirectly(long c1, long c2){
        cards1 = c1;
        cards2 = c2;
    }

    public void removeCard(long cards1_played, long cards2_played) {
        if(cards1_played >= cards2_played){
            cards1 ^= cards1_played;
            cards2 ^= cards2_played;
            long temp = cards1;
            cards1 |= cards2;
            cards2 &= temp;
        }else{
            throw new IllegalArgumentException("Invalid cards played");
        }

    }

    public boolean hasCards(){
        return (cards1==0) && (cards2==0);  //手里是否还有牌
    }

    public void resetValues(){
        cards1 = 0L;  //俩整型
        cards2 = 0L;
        numberOfHandCards = 0;
        Arrays.fill(numberOfEveryHandCards, 0);
    }


    public static void main(String[] args) {
        CardStorage sacc = new CardStorage();   //初始化

        sacc.setCard(1);
        sacc.setCard(2);
        sacc.setCard(5);
        sacc.setCard(7);
        sacc.setCard(10);
        sacc.setCard(13);
        sacc.setCard(15);
        sacc.setCard(16);
        sacc.setCard(17);
        sacc.setCard(21);
        sacc.setCard(23);
        sacc.setCard(25);
        sacc.setCard(77);
        sacc.setCard(27);
        sacc.setCard(30);
        sacc.setCard(33);
        sacc.setCard(34);
        sacc.setCard(35);
        sacc.setCard(37);
        sacc.setCard(40);
        sacc.setCard(41);
        sacc.setCard(42);
        sacc.setCard(45);
        sacc.setCard(46);
        sacc.setCard(50);
        sacc.setCard(52);
        sacc.setCard(53);
        String binaryString1;
        String binaryString2;
        binaryString1 = Long.toBinaryString(sacc.cards1);
        binaryString2 = Long.toBinaryString(sacc.cards2);
        System.out.println("整数 " + sacc.cards1 + " 的二进制表示为：" + binaryString1);
        System.out.println("整数 " + sacc.cards2 + " 的二进制表示为：" + binaryString2);

        System.out.println("======================================================");

        int count = 0;
        int [] count_i = new int[15];

        count = Long.bitCount(sacc.cards1) + Long.bitCount(sacc.cards2);
        long [][] hand_card_i_j = new long[15][2];
        for(int i=0;i<hand_card_i_j.length-2;i++){
            for(int j=0;j<2;j++){
                hand_card_i_j[i][j] = (1L << (i)) | (1L << (i + 13)) | (1L << (i + 26)) | (1L << (i + 39));
            }
        }
        hand_card_i_j[13][0] = (1L << 52);
        hand_card_i_j[13][1] = (1L << 52);
        hand_card_i_j[14][0] = (1L << 53);
        hand_card_i_j[14][1] = (1L << 53);

        long startTime;
        long endTime;
        long duration;
        startTime = System.currentTimeMillis();
        for(int i=0;i<15;i++){
            count_i[i] = Long.bitCount(sacc.cards1 & hand_card_i_j[i][0]) + Long.bitCount(sacc.cards2 & hand_card_i_j[i][1]);
        }
        endTime = System.currentTimeMillis();
        duration = endTime - startTime;
        System.out.println("duration："+duration);
        System.out.println("手牌的张数：" + count);
        for(int i=0;i<15;i++){
            if(i==10){
                System.out.println("手牌中J的张数：" + count_i[i]);
            } else if (i==11) {
                System.out.println("手牌中Q的张数：" + count_i[i]);
            }else if(i==12){
                System.out.println("手牌中K的张数：" + count_i[i]);
            }else if(i==13){
                System.out.println("手牌中大王的张数：" + count_i[i]);
            }else if(i==14){
                System.out.println("手牌中小王的张数：" + count_i[i]);
            }else {
                int X = i+1;
                System.out.println("手牌中"+X+"的张数：" + count_i[i]);
            }
        }
    }
}
