需求
1、生成一副斗地主的扑克牌
2、输出洗牌后的扑克
3、发牌,并留下的3张底牌
4、排序三人的牌
源码
看代码注释哦
方案一(暴力模式,就硬排) 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 import java.util.*;public class DouDiZhu { public static void main (String[] args) { List<String> pokeList = new ArrayList <>(); List<String> shapes = new ArrayList <>(Arrays.asList("♠" , "♥" , "♣" , "♦" )); List<String> numbers = new ArrayList <>(Arrays.asList("3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" )); for (String s : shapes) { for (String n : numbers) { pokeList.add(s + n); } } pokeList.add("小王" ); pokeList.add("大王" ); System.out.println("构建一副扑克:" + pokeList); Collections.shuffle(pokeList); System.out.println("洗牌之后:" + pokeList); System.out.println(); String pai; List<String> me = new ArrayList <>(); List<String> jzy = new ArrayList <>(); List<String> hgh = new ArrayList <>(); List<String> dipai = new ArrayList <>(); for (int i = 0 ; i < pokeList.size(); i++) { pai = pokeList.get(i); if (i >= 51 ) { dipai.add(pai); } else if (i % 3 == 0 ) { me.add(pai); } else if (i % 3 == 1 ) { jzy.add(pai); } else { hgh.add(pai); } } System.out.println("开始发牌" ); System.out.println("底牌" + dipai); System.out.println("排序前我的牌:" + me); System.out.println("排序前Jzy的牌:" + jzy); System.out.println("排序前Hgh的牌:" + hgh); System.out.println(); List<String> myFinalPai = DouDiZhu.sortPai(me); List<String> jzyFinalPai = DouDiZhu.sortPai(jzy); List<String> hghFinalPai = DouDiZhu.sortPai(hgh); System.out.println("排序后我的牌:" + myFinalPai); System.out.println("排序后Jzy的牌:" + jzyFinalPai); System.out.println("排序后Hgh的牌:" + hghFinalPai); } private static List<String> sortPai (List<String> list) { List<String> paiNumberList = new ArrayList <>(); List<String> paiCharacterList = new ArrayList <>(); List<String> kingList = new ArrayList <>(); for (String tempPai : list) { if (tempPai.charAt(1 ) < 'A' ) { paiNumberList.add(tempPai); } else if (tempPai.charAt(1 ) >= 'A' && tempPai.charAt(1 ) <= 'Z' ) { paiCharacterList.add(tempPai); } else { kingList.add(tempPai); } } Queue<String> paiCharacterQueue = new ArrayDeque <>(paiCharacterList); paiCharacterList.clear(); List<String> jList = new ArrayList <>(); List<String> qList = new ArrayList <>(); List<String> kList = new ArrayList <>(); List<String> aList = new ArrayList <>(); while (!paiCharacterQueue.isEmpty()) { if (paiCharacterQueue.peek().charAt(1 ) == 'A' ) { aList.add(0 , paiCharacterQueue.poll()); } else if (paiCharacterQueue.peek().charAt(1 ) == 'J' ) { jList.add(0 , paiCharacterQueue.poll()); } else if (paiCharacterQueue.peek().charAt(1 ) == 'Q' ) { qList.add(0 , paiCharacterQueue.poll()); } else { kList.add(0 , paiCharacterQueue.poll()); } } paiCharacterList.addAll(aList); paiCharacterList.addAll(kList); paiCharacterList.addAll(qList); paiCharacterList.addAll(jList); for (int i = 0 ; i < paiNumberList.size(); i++) { int max = Integer.parseInt(paiNumberList.get(i).substring(1 , 2 )); if (paiNumberList.get(i).length() == 3 ) { max = Integer.parseInt(paiNumberList.get(i).substring(1 , 3 )); } if (max == 2 ) { paiCharacterList.add(0 , paiNumberList.get(i)); continue ; } for (int j = i + 1 ; j < paiNumberList.size(); j++) { int maxNew = Integer.parseInt(paiNumberList.get(j).substring(1 , 2 )); if (paiNumberList.get(j).length() == 3 ) { maxNew = Integer.parseInt(paiNumberList.get(j).substring(1 , 3 )); } if (max < maxNew) { String a = paiNumberList.get(i); String b = paiNumberList.get(j); paiNumberList.set(i, b); paiNumberList.set(j, a); max = maxNew; } } } paiNumberList.removeIf(item -> item.contains("2" )); List<String> finalPokeList = new ArrayList <>(); if (kingList.size() == 2 ) { finalPokeList.add("小王" ); finalPokeList.add("大王" ); } else { finalPokeList.addAll(kingList); } finalPokeList.addAll(paiCharacterList); finalPokeList.addAll(paiNumberList); return finalPokeList; } }
输出结果
Map方法(较优雅)
前面的发牌方法都一样,就是构建扑克的时候,把花色和数字组合起来,当作map的key加进去,序号当做value,序号也就是排序序号
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 import java.util.*;public class MapSortPai { public static void main (String[] args) { List<String> shapes = List.of("♠" , "♥" , "♣" , "♦" ); List<String> numbers = List.of("2" , "A" , "K" , "Q" , "J" , "10" , "9" , "8" , "7" , "6" , "5" , "4" , "3" ); Map<String, Integer> pokeMap = new HashMap <>(); pokeMap.put("大王" , 0 ); pokeMap.put("小王" , 1 ); int sort = 2 ; for (String n : numbers){ for (String s : shapes) { pokeMap.put(s + n, sort); sort++; } } System.out.println("扑克哈希表:" + pokeMap); List<String> pokeList = new ArrayList <>(); for (Map.Entry<String, Integer> entry : pokeMap.entrySet()) { pokeList.add(entry.getKey()); } Collections.shuffle(pokeList); System.out.println("洗牌之后:" + pokeList); String pai; List<String> me = new ArrayList <>(); List<String> jzy = new ArrayList <>(); List<String> hgh = new ArrayList <>(); List<String> dipai = new ArrayList <>(); for (int i = 0 ; i < pokeList.size(); i++) { pai = pokeList.get(i); if (i >= 51 ) { dipai.add(pai); } else if (i % 3 == 0 ) { me.add(pai); } else if (i % 3 == 1 ) { jzy.add(pai); } else { hgh.add(pai); } } System.out.println("开始发牌" ); System.out.println("底牌" + dipai); System.out.println("排序前我的牌:" + me); System.out.println("排序前Jzy的牌:" + jzy); System.out.println("排序前Hgh的牌:" + hgh); System.out.println(); me.sort(Comparator.comparingInt(pokeMap::get)); jzy.sort(Comparator.comparingInt(pokeMap::get)); hgh.sort(Comparator.comparingInt(pokeMap::get)); System.out.println("排序后我的牌:" + me); System.out.println("排序后Jzy的牌:" + jzy); System.out.println("排序后Hgh的牌:" + hgh); } }
结语
不太喜欢研究算法,用的是都能看懂的笨办法,如果有大佬有更棒的排序方案,一定要叫我去观摩!