summaryrefslogtreecommitdiff
path: root/aux/set/Set.java
blob: be1cdcac528c9a76427cfa503421e7ca5c1173fd (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
package mahjong.aux.set;

import mahjong.aux.Card;

import java.util.Random;
import java.util.Vector;

/**
 * Created by joe on 12/1/14.
 */
public class Set {
    static protected final Random rand = new Random();
    protected Vector<Card> cards = new Vector<Card>();

    public Set(){}
    public Set(String[] cardNames)
    {
        for (String name:cardNames)
            add(new Card(name));
    }
    public Set(Card[] cards)
    {
        for (Card card:cards)
            add(card);
    }
    public Set(String str)
    {
        int typeid=0;
        for (int i=str.length()-1;i>=0;--i)
        {
            if (!Character.isDigit(str.charAt(i)))
                switch (Character.toUpperCase(str.charAt(i)))
                {
                    case 'M':
                        typeid=0;
                        break;
                    case 'P':
                        typeid=1;
                        break;
                    case 'S':
                        typeid=2;
                        break;
                    case 'Z':
                        typeid=3;
                        break;
                }
            else
            {
                int posid=str.charAt(i)-48;
                add(new Card(Card.idLut[typeid][posid]));
            }
        }
    }

    public void sort()
    {
        cards.sort((Card x,Card y) -> x.getId()-y.getId());
    }
    public void add(Card in)
    {
        cards.add(in);
    }
    public void remove(Card out)
    {
        for (int i=0; i<cards.size();++i)
            if (cards.elementAt(i).getId() == out.getId())
            {
                cards.remove(i);
                break;
            }
    }
    public void replace(Card out, Card in)
    {
        remove(out);
        add(in);
    }
    public Card draw()
    {
        int ord = rand.nextInt(getSize());
        Card card = cards.elementAt(ord);
        remove(card);
        return card;
    }

    public Object[] getCards()
    {
        return cards.toArray();
    }
    public String getTypeName()
    {
        return "Misc";
    }
    public int getSize()
    {
        return cards.size();
    }

    static char typeId2Char(int typeid)
    {
        switch (typeid)
        {
            case 0:
                return 'm';
            case 1:
                return 'p';
            case 2:
                return 's';
            case 3:
                return 'z';
        }
        return ' ';
    }

    @Override
    public String toString() {
        int typeid=-1;
        String str="";
        sort();
        for (Card card:cards) {
            if (typeid!=Math.min(3, card.getTypeId()))
            {
                if (typeid>=0)
                    str=str.concat(Character.toString(typeId2Char(typeid)));
            }
            typeid=Math.min(3, card.getTypeId());
            int i=0;
            for (int j=0;j<10;++j)
                if (Card.idLut[typeid][j]==card.getId())
                    i=j;
            str=str.concat(Integer.toString(i));
        }
        if (typeid>=0)
            str=str.concat(Character.toString(typeId2Char(typeid)));
        return str;
    }

    public void print()
    {
        for (Card card:cards)
            System.out.println(card.getNormalName());
    }
}