为了账号安全,请及时绑定邮箱和手机立即绑定

JAVA入门第三季第七章实战练习

标签:
Java

首先,先创建一个Gammer类与一个Poker类。

/*
 * Gammer类包含int类型的id属性与String类型的name属性!
 * id用int类型是为了测试后面创建用户时输入异常的情况。
 */
public class Gammer  {
    public int id;
    public String name;
    public List<Poker> pokers;
    public Gammer(int id,String name){
        this.id=id;
        this.name=name;
        this.pokers=new ArrayList<Poker>();
    }
    public Gammer(){
    }   
}
/*
 * Poker类包含四个属性分别为:
 * String number 表示扑克牌的点数
 * String colors 表示扑克牌的花色
 * Integer colornb 表示扑克牌花色的大小,方便比较(用Integer类型定义,是因为用colletions.sort()方法排序的时候只能用int型的包装类)
 * Integer numbernb 表示扑克牌牌面的大小,方便比较(用Integer类型定义,是因为用colletions.sort()方法排序的时候只能用int型的包装类)
 */

public class Poker implements Comparable<Poker> {
    public String number;
    public String colors;
    public Integer colornb;
    public Integer numbernb;
    public Poker(String number,String colors,int colornb,int numbernb){
        this.number=number;
        this.colors=colors;
        this.colornb=colornb;
        this.numbernb=numbernb;
    }
    public Poker(){

    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((colors == null) ? 0 : colors.hashCode());
        result = prime * result + ((number == null) ? 0 : number.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Poker))
            return false;
        Poker other = (Poker) obj;
        if (colors == null) {
            if (other.colors != null)
                return false;
        } else if (!colors.equals(other.colors))
            return false;
        if (number == null) {
            if (other.number != null)
                return false;
        } else if (!number.equals(other.number))
            return false;
        return true;
    }
    /*
     * 重写compareTo方法
     * 当牌面大小不相同的时候直接比较牌面的大小
     * 当牌面大小相同比较花色。
     */
    @Override
    public int compareTo(Poker o) {
        if(this.numbernb.compareTo(o.numbernb)!=0)
            return this.numbernb.compareTo(o.numbernb);
        else
            return this.colornb.compareTo(o.colornb);
    }
}

创建一个类,改类包含了实现该练习功能的方法,方便在main函数中调用。

public class User {
    public Map<Integer,Gammer> user=new HashMap<Integer,Gammer>();
    public List<Poker> pokerToSelect=new ArrayList<Poker>();
    public Poker[] poker=new Poker[]{new Poker("A","方块",1,14),new Poker("K","方块",1,13),new Poker("Q","方块",1,12),new Poker("J","方块",1,11),new Poker("10","方块",1,10),new Poker("9","方块",1,9),new Poker("8","方块",1,8),new Poker("7","方块",1,7),new Poker("6","方块",1,6),new Poker("5","方块",1,5),new Poker("4","方块",1,4),new Poker("3","方块",1,3),new Poker("2","方块",1,2),
            new Poker("A","梅花",2,14),new Poker("K","梅花",2,13),new Poker("Q","梅花",2,12),new Poker("J","梅花",2,11),new Poker("10","梅花",2,10),new Poker("9","梅花",2,9),new Poker("8","梅花",2,8),new Poker("7","梅花",2,7),new Poker("6","梅花",2,6),new Poker("5","梅花",2,5),new Poker("4","梅花",2,4),new Poker("3","梅花",2,3),new Poker("2","梅花",2,2),
            new Poker("A","红桃",3,14),new Poker("K","红桃",3,13),new Poker("Q","红桃",3,12),new Poker("J","红桃",3,11),new Poker("10","红桃",3,10),new Poker("9","红桃",3,9),new Poker("8","红桃",3,8),new Poker("7","红桃",3,7),new Poker("6","红桃",3,6),new Poker("5","红桃",3,5),new Poker("4","红桃",3,4),new Poker("3","红桃",3,3),new Poker("2","红桃",3,2),
            new Poker("A","黑桃",4,14),new Poker("K","黑桃",4,13),new Poker("Q","黑桃",4,12),new Poker("J","黑桃",4,11),new Poker("10","黑桃",4,10),new Poker("9","黑桃",4,9),new Poker("8","黑桃",4,8),new Poker("7","黑桃",4,7),new Poker("6","黑桃",4,6),new Poker("5","黑桃",4,5),new Poker("4","黑桃",4,4),new Poker("3","黑桃",4,3),new Poker("2","黑桃",4,2)};
    public int usercount; 
    /*
     * numberOfUser()方法是用来实现输入玩家人数,实现多人玩游戏。
     * 当输入出现异常的时候抓住异常,并抛出异常让main方法去处理。
     */
    public void numberOfUser(){
        System.out.println("--------请输入玩家人数---------");
        try{
            Scanner input=new Scanner(System.in);
            int usercount2=input.nextInt();
            usercount=usercount2;
        }catch(InputMismatchException e){
            System.out.println("请输入正整数!");
            InputMismatchException newExc=new InputMismatchException();
            newExc.initCause(e);
            throw newExc;   
        }   
    }
    /*
     * bulidgammer()方法用来创建玩家
     * 当输入异常的时候抓住异常,并抛出异常让main方法去处理
     */

    public void bulidgammer(){      
        try{
            Scanner input=new Scanner(System.in);
            while(user.size()<usercount){               
                System.out.println("请输入第"+(user.size()+1)+"名玩家ID");
                int id=input.nextInt();
                //System.out.println("id:"+id);
                Gammer gm=user.get(id);
                if(gm==null){
                    System.out.println("请输入玩家的姓名");
                    String name =input.next();
                    Gammer gmm=new Gammer(id,name);
                    user.put(id, gmm);
                }
                else{
                    System.out.println("该ID已经存在,请重新输入");
                    continue;
                }           
            }

            Set<Integer> idset=user.keySet();
            for(int kid:idset){
                Gammer gm=user.get(kid);
                if(gm!=null)
                    System.out.println("欢迎玩家:"+gm.name);
                else
                    continue;
            }
        }catch(InputMismatchException e){
            System.out.println("请输入正整数!");  
            InputMismatchException newExc=new InputMismatchException();
            newExc.initCause(e);
            throw newExc;
        }
    }
        /*
         * shuffle()方法是用来洗牌的
         * 该实现方法主要是先随机抽出26张牌依次放入pokerToSelect
         * (可随机抽取更多的牌,但是因为每次都在52张牌里面抽,抽得越多重复得几率就越大,所以要抽出不同的牌花费的时间就越多。)
         * 再把剩下的依次放进pokerToSelect
         */
        public void shuffle(){
            Random rd=new Random();
            int k;
            System.out.println("---------开始洗牌---------");
            for(int i=0;i<26;i++){
                do{
                     k=rd.nextInt(52);
                     //System.out.println("k的值为:"+k);
                }while(pokerToSelect.contains(poker[k]));
                pokerToSelect.add(poker[k]);
            }
            for(int j=0;j<52;j++){
                if(!(pokerToSelect.contains(poker[j])))
                    pokerToSelect.add(poker[j]);
            }
            System.out.println("---------洗牌完成---------");

        }
        /*
         * FP()方法用于发牌与比较玩家牌面的大小,并决出胜者。
         */

    public void FP(){
        Gammer[] gmm=new Gammer[user.size()];
        System.out.println("开始发牌");
        Set<Integer> IDset=user.keySet();
        int k=0;
        for(int id:IDset){
            gmm[k]=user.get(id);
            k++;
        }
            k=0;
       // System.out.println("user:"+user.size());
        /*发牌,每个玩家两张牌,从pokerToSelect的第一张牌开始发*/
        for(int i=0;i<2;i++){
            for(int j=0;j<user.size();j++){
                System.out.println(gmm[j].name+"拿牌");
                gmm[j].pokers.add(pokerToSelect.get(k));
                k++;
            }   
        }
        System.out.println("发牌完成!");
        /*用Collections.sort()方法对每个玩家的牌进行排序,显示每个玩家所得到的牌。并选出最大的牌*/
        for(int i=0;i<user.size();i++){
            Collections.sort(gmm[i].pokers);
            k=1;
            for(Poker pk:gmm[i].pokers){
                System.out.println("玩家"+gmm[i].name+"第"+k+"张牌为:"+pk.colors+pk.number);
                k++;
            }
            System.out.println("玩家"+gmm[i].name+"最大的牌为:"+gmm[i].pokers.get(gmm[i].pokers.size()-1).colors+gmm[i].pokers.get(gmm[i].pokers.size()-1).number);
        }
        /*用冒泡法比较每个玩家最大牌,决出优胜者*/
       for(int p=0;p<user.size()-1;p++){

          if(gmm[p].pokers.get(gmm[p].pokers.size()-1).numbernb==gmm[p+1].pokers.get(gmm[p+1].pokers.size()-1).numbernb){
              if(gmm[p].pokers.get(gmm[p].pokers.size()-1).colornb>gmm[p+1].pokers.get(gmm[p+1].pokers.size()-1).colornb){
                  gmm[p+1].pokers.get(gmm[p+1].pokers.size()-1).colornb=gmm[p].pokers.get(gmm[p].pokers.size()-1).colornb;
                  gmm[p+1].name=gmm[p].name;
              } 
          }
          else if(gmm[p].pokers.get(gmm[p].pokers.size()-1).numbernb>gmm[p+1].pokers.get(gmm[p+1].pokers.size()-1).numbernb){
              gmm[p+1].pokers.get(gmm[p+1].pokers.size()-1).numbernb=gmm[p].pokers.get(gmm[p].pokers.size()-1).numbernb;
              gmm[p+1].pokers.get(gmm[p+1].pokers.size()-1).colornb=gmm[p].pokers.get(gmm[p].pokers.size()-1).colornb;
              gmm[p+1].name=gmm[p].name;
          }
          else
              continue;
       }
       System.out.println("The Winner is:"+gmm[user.size()-1].name);        
    }
}

main方法。

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        User us=new User();
        while(true){
            try{
                us.numberOfUser();
            }catch(Exception e){
                continue;
            }
            break;
        }

        while(true){
            try{
                us.bulidgammer();
            }catch(Exception e){
                continue;
            }
            break;
        }   
        us.shuffle();
        us.FP();
    }
点击查看更多内容
16人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
软件测试工程师
手记
粉丝
1
获赞与收藏
37

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消