- package Demo2;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- /*
- * 斗地主
- * 1,造牌
- * 2,洗牌
- * 3,发牌看牌
- * 将每一步封装到方法中
- */
- public class DouMethodDemo {
- public static void main(String[] args) {
- HashMap<Integer,String> map=makeCard();
- ArrayList<Integer> key=sendCard();
- sendCard(map,key);
- }
-
- private static HashMap<Integer, String> makeCard() {
- //造花色
- ArrayList<String> color=new ArrayList<String>();
- Collections.addAll(color, "♥","♣","♠","♦");
- //造牌点
- ArrayList<String> number=new ArrayList<String>();
- Collections.addAll(number, "3","4","5","6","7","8","9","10","J","Q","K","A","2");
- //将花色 和 牌点 加入Map集合中
- HashMap<Integer,String> map=new HashMap<>();
- int index=0;
- for(String num:number){
- for(String co:color){
- map.put(index++, num+co);
- }
- }
- //加大小王
- map.put(index++, "小王");
- map.put(index++,"大王");
- return map;
- }
- private static ArrayList<Integer> sendCard() {
- ArrayList<Integer> key=new ArrayList<Integer>();
- for(int i=0;i<54;i++){
- key.add(i);
- }
- Collections.shuffle(key);
- return key;
- }
- private static void sendCard(HashMap<Integer, String> map, ArrayList<Integer> key) {
- //把54张牌发到3个玩家和底牌的集合中
- ArrayList<Integer> keyPlay1=new ArrayList<>();
- ArrayList<Integer> keyPlay2=new ArrayList<>();
- ArrayList<Integer> keyPlay3=new ArrayList<>();
- ArrayList<Integer> keyCards=new ArrayList<>();
- //把打乱数字的集合发给玩家和底牌
- for(int i=0;i<key.size();i++){
- if(i>50){
- int num=key.get(i);
- keyCards.add(num);
- }
- else if(i%3==0){
- keyPlay1.add(key.get(i));
- }
- else if(i%3==1){
- keyPlay2.add(key.get(i));
- }
- else{
- keyPlay3.add(key.get(i));
- }
- }
- //将玩家手中的牌进行排序
- Collections.sort(keyPlay1);
- Collections.sort(keyPlay2);
- Collections.sort(keyPlay3);
- //创建玩家手中牌的集合
- ArrayList<String> valuePlay1=new ArrayList<String>();
- ArrayList<String> valuePlay2=new ArrayList<String>();
- ArrayList<String> valuePlay3=new ArrayList<String>();
- ArrayList<String> valueCards=new ArrayList<String>();
- //通过玩家集合中的key找到Map集合中的Value
- for(Integer k:keyPlay1){
- String value=map.get(k);
- valuePlay1.add(value);
- }
- for(Integer k:keyPlay2){
- valuePlay2.add(map.get(k));
- }
- for(Integer k:keyPlay3){
- valuePlay3.add(map.get(k));
- }
- for(Integer k:keyCards){
- valueCards.add(map.get(k));
- }
- //看牌
- System.out.println(valuePlay1);
- System.out.println(valuePlay2);
- System.out.println(valuePlay3);
- System.out.println(valueCards);
- }
- }
复制代码 |
|