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

Java入门第三季7-1简易扑克牌游戏

标签:
Java

Factory工厂类

public class Factory {//工厂类
    private Poker pokers =new Poker();//实例化pokers(扑克牌)
    private Palyer palyers=new Palyer();//实例化palyers(玩家)
    Palyer palyer1=null;//玩家1
    Palyer palyer2=null;//玩家2
    public  String[] color= {"方块","梅花","红桃","黑桃"};//花色数组
    public  String[] point= {"2","3","4","5","6","7","8","9","10"
             ,"J","Q","K","A",};//点数数组

    public void createPoker() {//创建扑克
        System.out.println("-----------开始创建扑克...-----------");
        //双重循环添加扑克牌
        for(int i=0;i<color.length;i++) {
            for(int j=0;j<point.length;j++) {
                StringBuffer sb = new StringBuffer();//重置
                sb.append(color[i]);
                sb.append(point[j]);
                pokers.getPoker().add(sb.toString());
            }
        }
        System.out.println("-----------创建扑克结束!-----------");
        //遍历扑克牌
             System.out.println(pokers.getPoker());//调用toString方法打印
    }

    public void shuffleTheCards() {//洗牌
        System.out.println("-----------开始洗牌...-----------");
        Collections.shuffle(pokers.getPoker());
        System.out.println("-----------洗牌结束!-----------");
    }

    public void setPalyer() {//设置用户
        System.out.println("-----------开始创建玩家...-----------");
        //开始添加用户
        for(int i=0;i<2;i++) {
            System.out.println("请输入第"+(i+1)+"位玩家的ID及姓名");
            System.out.println("请输入ID");
            while(true) {
                try {
                    Scanner console=new Scanner(System.in);//重置
                    int ID=console.nextInt();
                    System.out.println("请输入姓名");
                    String name =console.next();
                    palyers.getPalyers().add(new Palyer(ID,name));//添加玩家
                    break;//输入符合要求停止循环
                }catch(InputMismatchException e) {
                    System.out.println("请输入正整数ID");
                    continue;//输入不匹配继续循环输入
                }
            }
        }
        //遍历玩家序列
        for(Palyer palyer:palyers.getPalyers()) {
            System.out.println("欢迎玩家:"+palyer.getName());
        }       
    }

    public void licensing() {//发牌
        System.out.println("-----------开始发牌...-----------");
        //遍历玩家序列发牌
        for(int j=0;j<palyers.getPalyers().size()*2;j+=palyers.getPalyers().size()) {
            //每轮发牌下标为是人数数量翻倍,没人两张牌,故<人数数量*2
            for(int i=0;i<=palyers.getPalyers().size();i++) {//先取出玩家对象
                if(i==0) {
                    this.palyer1=palyers.getPalyers().get(i);
                    System.out.println("玩家:"+palyer1.getName()+"-拿牌");
                    palyer1.getHand().add(pokers.getPoker().get(j+i));
                }
                if(i==1) {
                    this.palyer2=palyers.getPalyers().get(i);
                    System.out.println("玩家:"+palyer2.getName()+"-拿牌");
                    palyer2.getHand().add(pokers.getPoker().get(j+i));
                }
            }
        }
        System.out.println("-----------发牌结束!-----------");
    }

    public void startGame() {//开始游戏
        System.out.println("-----------开始游戏...-----------");
        String palyer1max=Collections.max(palyer1.getHand(),new PokerComparator());
        String palyer2max=Collections.max(palyer2.getHand(),new PokerComparator());
        System.out.println("玩家:"+palyer1.getName()+"最大的手牌为:"+palyer1max);
        System.out.println("玩家:"+palyer2.getName()+"最大的手牌为:"+palyer2max);
        //判决
        Poker first =new Poker();
        first.getPoker().add(palyer1max);
        first.getPoker().add(palyer2max);
        String pokerFirst=Collections.max(first.getPoker(),new PokerComparator());
        for(String str1:palyer1.getHand()) {
            if(str1.indexOf(pokerFirst)!=-1) {
                System.out.println("----------玩家:"+palyer1.getName()+"获胜-----------");
            }
        }
        for(String str2:palyer2.getHand()) {
            if(str2.indexOf(pokerFirst)!=-1) {
                System.out.println("----------玩家:"+palyer2.getName()+"获胜-----------");
            }
        }

        //遍历玩家手牌
        System.out.println("-----------玩家各自的手牌-----------");
        System.out.println(palyer1.getHand());//调用toString方法直接打印
        System.out.println(palyer2.getHand());//调用toString方法直接打印
    }

}

Home主页类

public class Home {

    public static void main(String[] args) {
        Factory ft = new Factory();
        ft.createPoker();//创建扑克牌
        ft.shuffleTheCards();//洗牌
        ft.setPalyer();//创建玩家
        ft.licensing();//发牌
        ft.startGame();//开始游戏
    }

}

Palyer玩家类

public class Palyer {
    private int ID;
    private String name;
    private List<Palyer> palyers;//声明玩家序列
    private List<String> hand;//玩家有手牌

    public Palyer() {//构造器实现玩家序列
        this.palyers=new ArrayList<Palyer>();
        this.hand=new ArrayList<String>();
    }

    public Palyer(int ID,String name) {//构造器设置对象属性
        this();
        this.setID(ID);
        this.setName(name);
    }

    public void setID(int ID) {
        this.ID=ID;
    }

    public void setName(String name) {
        this.name=name;
    }

    public void setHand(List<String> hand) {
        this.hand=hand;
    }

    public int getID() {
        return this.ID;
    }

    public String getName() {
        return this.name;
    }

    public List<Palyer> getPalyers(){
        return this.palyers;
    }

    public List<String> getHand() {
        return this.hand;
    }
}

Poker扑克类

public class Poker {
    private List<String> pokers;//创建pokers序列

    public Poker() {
        this.pokers= new ArrayList<String>();//构造器实现pokers序列
    }

    public List<String> getPoker(){
        return this.pokers;
    }
}

PokerComparator临时比较规则

public class PokerComparator implements Comparator<String> {

    @Override
    public int compare(String obj1, String obj2) {
        int value = 0;
        Integer int1 = 0;
        Integer int2 = 0;
        // 将数组下标转为Integer调用compareTo()方法
        String[] color = new Factory().color;
        String[] point = new Factory().point;
        for (int i = 0; i < point.length; i++) {
            if (point[i].equals(obj1.substring(2))) {
                int1 = i;
            } 
            if (point[i].equals(obj2.substring(2))) {
                int2 = i;
            } 

        }
        if (int1 == int2) {
            Integer index1 = 0;
            Integer index2 = 0;
            for (int i = 0; i < color.length; i++) {
                if (color[i].equals(obj1.substring(0, 2))) {
                    index1 = i;
                } 
                if (color[i].equals(obj2.substring(0, 2))) {
                    index2 = i;
                }
            }
            value = index1.compareTo(index2);
        } else {
            value = int1.compareTo(int2);
        }
        return value;
    }

}
点击查看更多内容
2人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消