用java实现简单的扑克洗牌,发牌,理牌
1.新建54张牌
List<String> pokes = new ArrayList<>();
List<String> colors = new ArrayList<>();
List<String> numbers = new ArrayList<>();
List<Integer> pokesIndex = new ArrayList<>();
int index = 0;
colors.add("♥");
colors.add("♠");
colors.add("♦");
colors.add("♣");
numbers.add("A");
for (int i = 2; i <= 10; i++) {
numbers.add(i + "");
}
numbers.add("J");
numbers.add("Q");
numbers.add("K");
for (String c : numbers) {
for (String n : colors) {
String pai = c + n;
pokes.add(pai);
pokesIndex.add(index);
index++;
}
}
pokes.add("小王");
pokesIndex.add(index);
index++;
pokes.add("大王");
pokesIndex.add(index);
index++;
System.out.println(pokes);
2.洗牌
List<String> pokesNew = new ArrayList<>();
Collections.shuffle(pokesIndex);
for (int i = 0; i < pokesIndex.size(); i++) {
int j = pokesIndex.get(i);
// String poke = pokes.get(j);
pokesNew.add(pokes.get(j));
// System.out.println(pokesNew);
}
System.out.println(pokesNew);
3.发牌
ArrayList<String> A1=new ArrayList<>();
ArrayList<String> A2=new ArrayList<>();
ArrayList<String> A3=new ArrayList<>();
ArrayList<String> dipai=new ArrayList<>();
List<Integer> dipaiIndex=new ArrayList<>();
List<Integer> A1Index=new ArrayList<>();
List<Integer> A2Index=new ArrayList<>();
List<Integer> A3Index=new ArrayList<>();
for(int i=0;i<pokesIndex.size();i++){
Integer pai=pokesIndex.get(i);
if (i>=51){
dipaiIndex.add(pai);
int j=pokesIndex.get(i);
dipai.add(pokes.get(j));
}else {
if (i%3==0){
A1Index.add(pai);
int j=pokesIndex.get(i);
A1.add(pokes.get(j));
}else if (i%3==1){
A2Index.add(pai);
int j=pokesIndex.get(i);
A2.add(pokes.get(j));
}else {
A3Index.add(pai);
int j=pokesIndex.get(i);
A3.add(pokes.get(j));
}
}
}
System.out.println("整理前-------------------");
System.out.println("dipai:"+dipai);
System.out.println("A1:"+A1);
System.out.println("A2:"+A2);
System.out.println("A3:"+A3);
5.理牌
新建一个pokesIndex数组,新建一张牌就加1存入数组,使之与牌一一对应。然后根据随机打乱后的数据进行重新排序
List<String> dipaiNew=new ArrayList<>();
List<String> A1New=new ArrayList<>();
List<String> A2New=new ArrayList<>();
List<String> A3New=new ArrayList<>();
Collections.sort(dipaiIndex);
Collections.sort(A1Index);
Collections.sort(A2Index);
Collections.sort(A3Index);
for (int i=0;i<dipaiIndex.size();i++){
int j=dipaiIndex.get(i);
dipaiNew.add(pokes.get(j));
}
for (int i=0;i<A1Index.size();i++){
int j=A1Index.get(i);
A1New.add(pokes.get(j));
}
for (int i=0;i<A2Index.size();i++){
int j=A2Index.get(i);
A2New.add(pokes.get(j));
}
for (int i=0;i<A3Index.size();i++){
int j=A3Index.get(i);
A3New.add(pokes.get(j));
}
System.out.println("整理后-------------------");
System.out.println("dipai:"+dipaiNew);
System.out.println("A1:"+A1New);
System.out.println("A2:"+A2New);
System.out.println("A3:"+A3New);