java斗地主手牌实现
java斗地主代码如下:
public class Splendid {
public static void main(String[] args) {
String[] arr1 = { "♣", "♦", "♥", "♠" };
String[] arr2 = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", };
ArrayList<String> list = new ArrayList<>();
HashMap<Integer, String> map = new HashMap<>();
ArrayList<Integer> key = new ArrayList<>();
ArrayList<Integer> play1 = new ArrayList<>();
ArrayList<Integer> play2 = new ArrayList<>();
ArrayList<Integer> play3 = new ArrayList<>();
ArrayList<Integer> dipai = new ArrayList<>();
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr1.length; j++) {
list.add(arr1 + arr2);
}
}
list.add("小王");
list.add("大王");
// System.out.println(list);
for (int i = 0; i < list.size(); i++) {
key.add(i);
}
Collections.shuffle(key);
for (int i = 0; i < list.size(); i++) {
map.put(i, list.get(i));
if (i>list.size()-4) {
dipai.add(key.get(i));
} else if (i % 3 == 0) {
play1.add(key.get(i));
} else if (i % 3 == 1) {
play2.add(key.get(i));
} else {
play3.add(key.get(i));
}
}
lookpai(map,play1);
lookpai(map,play2);
lookpai(map,play3);
lookpai(map,dipai);
}
private static void lookpai(HashMap<Integer, String> map, ArrayList<Integer> play) {
Collections.sort(play);
for (int i = 0; i <play.size(); i++) {
String string = map.get(play.get(i));
System.out.print(string);
}
System.out.println();
}
咋玩的呀 有画面吗? Java.util.Collections类下有一个静态的shuffle()方法,static void shuffle(List<?> list)使用默认随机源对列表进行置换,所有置换发生的可能性都是大致相等的。这个需要理解一下,其他的都容易看懂 最后的排序需要处理吧,应该是 黑桃3 红桃3···黑桃4 红桃3 ···· ,而不是 黑桃 3、4红桃 3、4
页:
[1]