summaryrefslogtreecommitdiff
path: root/aux/set/CardSet.java
blob: 8df6055ed95bddbcfd303c0e62229f6c44a3ff2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package mahjong.aux.set;

import mahjong.aux.Card;

import java.util.Vector;

/**
 * Created by joe on 12/2/14.
 */
public class CardSet {
    public static final int cardTypeTotal = 34;
    public static final int[] deckIds = new int[]
            {
                    4,  6,  8,  10, 12, 14, 16, 18, 20,
                    36, 38, 40, 42, 44, 46, 48, 50, 52,
                    68, 70, 72, 74, 76, 78, 80, 82, 84,
                    100,106,112,118,
                    132,138,144,146
            };
    private static final int[] id2OrdLut = new int[]
            {
                   34, 34,  0,  1,  2,  3,  4,  5,  6,  7,  8, 34, 34, 34, 34, 34,
                   34, 34,  9,  10, 11, 12, 13, 14, 15, 16, 17,34, 34, 34, 34, 34,
                   34, 34,  18, 19, 20, 21, 22, 23, 24, 25, 26,34, 34, 34, 34, 34,
                   34, 34,  27,34, 34,  28,34, 34,  29,34, 34,  30,34, 34, 34, 34,
                   34, 34,  31,34, 34,  32,34, 34,  33,34, 34, 34, 34, 34, 34, 34,
            };

    protected int[] cardRem = new int[cardTypeTotal+1];

    public void init(int cnt)
    {
        for (int i=0;i<cardTypeTotal; ++i)
            cardRem[i]=cnt;
    }
    public CardSet(int cnt)
    {
        init(cnt);
    }

    public void add(Set cards,int mult)
    {
        for (Object card:cards.getCards())
            add((Card)card,mult);
    }

    public void add(Card card,int mult)
    {
        cardRem[getOrd(card.getStdId())]+=mult;
    }

    protected int std2Id(int std)
    {
        return std>>1;
    }
    protected int getOrd(int id)
    {
        return id2OrdLut[std2Id(id)];
    }

    public boolean validify()
    {
        for (int i:cardRem)
            if (i<0||i>4)
                return false;
        return true;
    }

    private int cnt=0;

    public int calTing()
    {
        //cnt=0;
        int res=calTing(0,cardRem[0],0,0,0);
        //System.out.println(cnt);
        return res;
    }

    private int nextCardOrd(int i)
    {
        return getOrd(deckIds[i]+2);
    }

    private int next2CardOrd(int i)
    {
        return nextCardOrd(nextCardOrd(i));
    }

    private int calTing(int pos,int rems,int shoupai,int ting, int mian)
    {
        //++cnt;
        int res=6;
        switch (rems)
        {
            case 0:
                if (pos == cardTypeTotal)
                    return st2T(shoupai,ting,mian);
                else
                    res = Math.min(res,calTing(pos+1, cardRem[pos+1], shoupai, ting, mian));
                break;
            case 1:
            case 2:
                if (cardRem[nextCardOrd(pos)]>0) {
                    if (cardRem[next2CardOrd(pos)] > 0) {
                        if (cardRem[nextCardOrd(pos)] > 3)
                            res = Math.min(res,calTing(next2CardOrd(pos), cardRem[next2CardOrd(pos)] - 1, shoupai, ting, mian + 2));
                        else if (cardRem[nextCardOrd(pos)] > 2)
                            res = Math.min(res,calTing(next2CardOrd(pos), cardRem[next2CardOrd(pos)] - 1, shoupai + 1, ting, mian + 1));
                        else
                            res = Math.min(res,calTing(next2CardOrd(pos), cardRem[next2CardOrd(pos)] - 1, shoupai, ting, mian + 1));
                    } else
                        res = Math.min(res,calTing(nextCardOrd(pos), cardRem[nextCardOrd(pos)]-1, shoupai, ting + 1, mian));
                }
                else
                if (cardRem[next2CardOrd(pos)] > 0)
                    res = Math.min(res,calTing(next2CardOrd(pos), cardRem[next2CardOrd(pos)]-1, shoupai, ting + 1, mian));
                if (rems>1)
                    res = Math.min(res,calTing(pos, rems-2, shoupai + 1, ting, mian));
                else
                    res = Math.min(res,calTing(pos, 0, shoupai, ting, mian));
                break;
            case 3:
            case 4:
                res = Math.min(res,calTing(pos, rems-3, shoupai, ting, mian+1));
                break;
        }
        return res;
    }

    private int st2T(int shoupai,int ting, int mian)
    {
        int res=0;
        if (shoupai==0)
            res=1;
        else
            ting+=shoupai-1;
        res+=Math.min(4,ting)+Math.max(0,4-ting-mian)*2;

        //System.out.println("S".concat(Integer.toString(shoupai)).concat("T").concat(Integer.toString(ting)).concat("M").concat(Integer.toString(mian)));
        //System.out.println(Math.min(6 - shoupai, res));
        return Math.min(6-shoupai,res);
    }

    public Vector<Card> jinZhang()
    {
        Vector<Card> cards = new Vector<Card>();
        int res=0;
        int ref=calTing();
        for (int i=0;i<cardTypeTotal;++i)
            if (cardRem[i]<3)
            {
                Card card=new Card(deckIds[i]);
                add(card,1);
                if (calTing()<ref) {
                    ++res;
                    cards.add(card);
                }
                add(card, -1);
            }
        return cards;
    }

    public Vector<Card> ameliorate()
    {
        Vector<Card> cards = new Vector<Card>();
        int res=0;
        int ref=calTing();
        int jinref=jinZhang().size();
        for (int i=0;i<cardTypeTotal;++i)
            if (cardRem[i]<3)
            {
                Card card=new Card(deckIds[i]);
                add(card,1);
                if (calTing()==ref) {
                    if (maxJinZhang(ref)>jinref) {
                        ++res;
                        cards.add(card);
                        //System.out.println(card.getNormalName());
                    }
                }
                add(card, -1);
            }
        return cards;
    }

    public int maxJinZhang(int xt)
    {
        int res=0;
        for (int i=0;i<cardTypeTotal;++i)
            if (cardRem[i]>0)
            {
                --cardRem[i];
                if (calTing()==xt)
                    res=Math.max(res,jinZhang().size());
                ++cardRem[i];
            }
        return res;
    }
}