Java 实现 斗地主发牌+ 排序
import java.util.ArrayList;
import java.util.Collections;
public class Text {
public static void main(String[] args) {
ArrayList<String> poker = new ArrayList<>();
String[] colors = {"♠", "♥", "♣", "♦"};
String[] numbers = {"A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
poker.add("大王");
poker.add("小王");
for (String color : colors) {
for (String number : numbers) {
poker.add(color + number);
}
}
Collections.shuffle(poker);
ArrayList<String> play01 = new ArrayList<>();
ArrayList<String> play02 = new ArrayList<>();
ArrayList<String> play03 = new ArrayList<>();
ArrayList<String> dipai = new ArrayList<>();
//发牌
for (int i = 0; i < poker.size(); i++) {
String s = poker.get(i);
if (i >= 51) {
dipai.add(s);
} else if (i % 3 == 0) {
play01.add(s);
} else if (i % 3 == 1) {
play02.add(s);
} else if (i % 3 == 2) {
play03.add(s);
}
}
System.out.println("周星驰" + play01);
System.out.println("周杰伦" + play02);
System.out.println("周润发" + play03);
System.out.println("底牌" + dipai);
}
}
排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class Text {
public static void main(String[] args) {
ArrayList<Integer> sor = new ArrayList<>();
HashMap<Integer,String> poker = new HashMap<>();
String[] colors = {"♠", "♥", "♣", "♦"};
String[] numbers = {"A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
int index = 0;
poker.put(index,"大王");
sor.add(index);
index++;
poker.put(index,"小王");
sor.add(index);
index++;
for (String number : numbers) {
for (String color : colors) {
poker.put(index,color + number);
sor.add(index);
index++;
}
}
System.out.println(poker);
Collections.shuffle(sor);
ArrayList<Integer> play01 = new ArrayList<>();
ArrayList<Integer> play02 = new ArrayList<>();
ArrayList<Integer> play03 = new ArrayList<>();
ArrayList<Integer> dipai = new ArrayList<>();
//发牌
for (int i = 0; i < poker.size(); i++) {
Integer s = sor.get(i);
if (i >= 51) {
dipai.add(s);
} else if (i % 3 == 0) {
play01.add(s);
} else if (i % 3 == 1) {
play02.add(s);
} else if (i % 3 == 2) {
play03.add(s);
}
}
System.out.println(play01);
System.out.println(play02);
System.out.println(play03);
System.out.println(dipai);
Collections.sort(play01);
Collections.sort(play02);
Collections.sort(play03);
Collections.sort(dipai);
System.out.println("=======================");
System.out.println(play01);
System.out.println(play02);
System.out.println(play03);
System.out.println(dipai);
look("周星驰",play01,poker);
look("周杰伦",play02,poker);
look("周润发",play03,poker);
look("底牌" ,dipai ,poker);
}
public static void look(String name,ArrayList<Integer> sor,HashMap<Integer,String> poker){
System.out.print(name+": ");
for (Integer key : sor) {
String value = poker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}