码迷,mamicode.com
首页 > 其他好文 > 详细

集合练习之模拟扑克发牌

时间:2017-04-24 20:14:37      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:port   rgs   个人   otto   imp   public   new   void   bottom   

package com.sxt.other;
/*
 * 字符串数组+集合ArrayList
 * 模拟扑克发牌
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PokerCard {
    public static void main(String[] args) {
        String[] color = {"?","?","?","?"};
        String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        
        //集合存储每一张牌
        List<String> pokerList = new ArrayList<>();
        for(int i=0; i<color.length; i++){
            for(int j=0; j<num.length; j++){
                pokerList.add(color[i]+num[j]);//字符串 数字和花色组合
            }
        }
        pokerList.add("小王");
        pokerList.add("大王");
        System.out.println(pokerList);
        System.out.println("扑克牌的张数:"+pokerList.size());
        //洗牌
        Collections.shuffle(pokerList);
        System.out.println(pokerList);
        
        //发牌 3个人每人17张牌 还有3张底牌
        List<String> client1 = new ArrayList<>();
        List<String> client2 = new ArrayList<>();
        List<String> client3 = new ArrayList<>();
        List<String> bottomCard = new ArrayList<>();
        for(int i=0; i<pokerList.size(); i++){
            if(i<51){
                if(i%3==0){
                    client1.add(pokerList.get(i));
                }else if(i%3==1){
                    client2.add(pokerList.get(i));
                }else{
                    client3.add(pokerList.get(i));
                }    
            }else{
                bottomCard.add(pokerList.get(i));
            }
        }
        System.out.println("玩家1的牌:"+client1);
        System.out.println("玩家2的牌:"+client2);
        System.out.println("玩家3的牌:"+client3);
        System.out.println("底牌:"+bottomCard);
    }
}

 

集合练习之模拟扑克发牌

标签:port   rgs   个人   otto   imp   public   new   void   bottom   

原文地址:http://www.cnblogs.com/qingfengzhuimeng/p/6758733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!