1. package Test19_Demo.Demo08;/*
    2. @create 2020--12--09--16:07
    3. */
    4. import java.util.ArrayList;
    5. import java.util.Collections;
    6. import java.util.HashMap;
    7. import java.util.Map;
    8. public class CardDemo {
    9. public static void main(String[] args) {
    10. //确定牌&数字的对应关系
    11. Map<Integer, String> map = new HashMap<>();
    12. //花色集合
    13. ArrayList<String> colors = new ArrayList<>();
    14. colors.add("♠");
    15. colors.add("♣");
    16. colors.add("♦");
    17. colors.add("♥");
    18. //数字集合
    19. ArrayList<String> numbers = new ArrayList<>();
    20. for (int i = 2; i < 11; i++) {
    21. numbers.add(i + " ");
    22. }
    23. Collections.addAll(numbers, "J", "Q", "K", "A");
    24. //设置对应关系
    25. int cardNumber = 0;
    26. //遍历数字集合,使用每一种颜色匹配每一个花色
    27. for (String thisNumber: numbers
    28. ) {
    29. for (String thisColor: colors
    30. ) {
    31. String thisCard = thisColor + thisNumber;
    32. map.put(cardNumber, thisCard);
    33. cardNumber++;
    34. }
    35. }
    36. //添加大小王
    37. map.put(cardNumber++, "大");
    38. map.put(cardNumber++, "小");
    39. ArrayList<Integer> poker = new ArrayList<>();
    40. for (int i = 0; i < 54; i++) {
    41. poker.add(i);
    42. }
    43. Collections.shuffle(poker);
    44. System.out.println(poker);
    45. //发牌
    46. ArrayList<Integer> player01 = new ArrayList<>();
    47. ArrayList<Integer> player02 = new ArrayList<>();
    48. ArrayList<Integer> player03 = new ArrayList<>();
    49. ArrayList<Integer> dipai = new ArrayList<>();
    50. for (int index = 0; index <poker.size() ; index++) {
    51. //通过索引,获取代表牌的数字
    52. Integer integerCard = poker.get(index);
    53. if (index>=poker.size() - 3){
    54. dipai.add(integerCard);
    55. }else if (index % 3 == 0) {
    56. player01.add(integerCard);
    57. }else if (index % 3 == 1) {
    58. player02.add(integerCard);
    59. }else{
    60. player03.add(integerCard);
    61. }
    62. }
    63. // for (int index = 0; index < poker.size() - 3; index++) {
    64. // //通过索引,获取代表牌的数字
    65. // Integer integerCard = poker.get(index);
    66. // if (index % 3 == 0) {
    67. // player01.add(integerCard);
    68. // }else if (index % 3 == 1) {
    69. // player02.add(integerCard);
    70. // }else{
    71. // player03.add(integerCard);
    72. // }
    73. // }
    74. //
    75. // for (int index = poker.size()-3; index <poker.size() ; index++) {
    76. // Integer integerCard = poker.get(index);
    77. // dipai.add(integerCard);
    78. //
    79. // }
    80. //看牌 - 排序
    81. Collections.sort(player01);
    82. Collections.sort(player02);
    83. Collections.sort(player03);
    84. Collections.sort(dipai);
    85. for (int i = player01.size() - 1; i >= 0; i--) {
    86. Integer integer = player01.get(i);
    87. String realCard = map.get(integer);
    88. System.out.print(realCard+" ");
    89. }
    90. System.out.println();
    91. for (int i = player02.size() - 1; i >= 0; i--) {
    92. Integer integer = player02.get(i);
    93. String realCard = map.get(integer);
    94. System.out.print(realCard+" ");
    95. }
    96. System.out.println();
    97. for (int i = player03.size() - 1; i >= 0; i--) {
    98. Integer integer = player03.get(i);
    99. String realCard = map.get(integer);
    100. System.out.print(realCard+" ");
    101. }
    102. System.out.println();
    103. /*for (int i = player01.size() - 1; i >= 0; i--) {
    104. Integer integer = player01.get(i);
    105. String realCard = map.get(integer);
    106. System.out.print(realCard+" ");
    107. }
    108. System.out.println();
    109. for (int i = player02.size() - 1; i >= 0; i--) {
    110. Integer integer = player02.get(i);
    111. String realCard = map.get(integer);
    112. System.out.print(realCard+" ");
    113. }
    114. System.out.println();
    115. for (int i = player03.size() - 1; i >= 0; i--) {
    116. Integer integer = player03.get(i);
    117. String realCard = map.get(integer);
    118. System.out.print(realCard+" ");
    119. }
    120. System.out.println();*/
    121. for (Integer integer:dipai) {
    122. String realCard = map.get(integer);
    123. System.out.print(realCard+" ");
    124. }
    125. }
    126. }