美文网首页
简易扑克牌游戏

简易扑克牌游戏

作者: 清波ss | 来源:发表于2018-07-28 17:35 被阅读0次

    java 练习3:
    实现:
    创建玩家,玩家在一副扑克牌中(不含大小王)获得手牌,取二人手中点数最大的牌进行比较,点数大的赢,点数相同比花色。
    不足:
    关于功能的拓展

    package pokergame;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Player {
        private int ID;
        private String name;
        private List<Cards> Handcards;
        
        public Player(int ID,String name) {
            this.ID=ID;
            this.name=name;
            this.Handcards=new ArrayList<Cards>();
        }
    
        public int getID() {
            return ID;
        }
    
        public String getName() {
            return name;
        }
    
        public List<Cards> getHandcards() {
            return Handcards;
        }
    }
    
    package pokergame;
    
    import java.util.Arrays;
    import java.util.List;
    
    public class Cards implements Comparable<Cards> {
        private String number;
        private String color;
        
        public Cards() {
        }
        
        public Cards(String number,String color) {
            this.number=number;
            this.color=color;
        }
        
        public String getNumber() {
            return number;
        }
        
        public String getColor() {
            return color;
        }
        
        @Override
        public int compareTo(Cards o) {
            List<String> valueBase = Arrays.asList("2","3","4","5","6",
                    "7","8","9","10","J","Q","K","A");
            List<String> colorBase = Arrays.asList("黑桃","梅花","方块","红桃");
            if(valueBase.indexOf(this.number) == valueBase.indexOf(o.number))
                return Integer.valueOf(colorBase.indexOf(this.color)).
                        compareTo(Integer.valueOf(colorBase.indexOf(o.getColor())));
            else 
                return Integer.valueOf(valueBase.indexOf(this.number)).
                      compareTo(Integer.valueOf(valueBase.indexOf(o.getNumber())));
        }
        
        @Override
        public String toString() {
            return color+number;
        }
    }
    
    package pokergame;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Scanner;
    
    public class Game {
        public static Scanner sc=new Scanner(System.in);
        public static final int CARDSNUM=52;
        public static void main(String[] args) {
            List<Player> players=new ArrayList<Player>();
            System.out.println("欢迎来到扑克世界!~~~");
            System.out.println("------------------创建一副扑克");
            List<Cards> Pokers=createPokers();
            System.out.println("------------------开始洗牌");
            Collections.shuffle(Pokers);
            //System.out.println(Pokers.toString());
            System.out.println("------------------洗牌结束");
            System.out.println("请输入玩家数量(2-17):");
            while(true) {
                int player=playerNum();
                if(player>17) {
                    System.out.println("玩家数量超出限制!请重新输入!");
                }else if(player<2) {
                    System.out.println("Are you kidding me??请重新输入!");
                }else {
                    //输入玩家信息
                    for(int i=0;i<player;i++) {
                        System.out.println("请输入第"+(i+1)+"位玩家ID:");
                        int id=playerNum();
                        System.out.println("请输入第"+(i+1)+"位玩家姓名:");
                        String name=sc.next();
                        players.add(new Player(id, name));
                    }
                    for(int i=0;i<player;i++) {
                        System.out.println("欢迎玩家:"+players.get(i).getName());
                    }
                    //开始发牌
                    System.out.println("----------------------开始发牌!");
                    int handpokers=3;
                    Cards poker;
                    for(int i=0;i<player*handpokers;i++) {
                        poker=Pokers.get(i);
                        players.get(i%player).getHandcards().add(poker);
                    }
                    System.out.println("----------------------发牌完毕!");
                    System.out.println("----------------------开始游戏!");
                    /*
                     * 将玩家手中的牌进行排序,然后进行比较
                     */
                    Cards max=new Cards("2", "黑桃");
                    int winplayer=0;
                    for(int i=0;i<player;i++) {
                        Collections.sort(players.get(i).getHandcards());
                        Collections.reverse(players.get(i).getHandcards());
                        System.out.println("玩家:"+players.get(i).getName()
                                +"最大手牌为:"+players.get(i).getHandcards().get(0));
                        if(max.compareTo(players.get(i).getHandcards().get(0))<0) {
                            max=players.get(i).getHandcards().get(0);
                            winplayer=i;
                        }
                    }
                    System.out.println("玩家"+players.get(winplayer).getName()+":"
                            +"获胜!");
                    System.out.println("玩家各自的手牌为:");
                    for(int i=0;i<player;i++) {
                        System.out.println(players.get(i).getName()+":"
                    +players.get(i).getHandcards());
                    }
                    break;
                }
            }
        }
        
        /*
         * 创建一副扑克牌(不含大小王)
         */
        public static List<Cards> createPokers(){
            List<Cards> cards=new ArrayList<Cards>();
            for(int i=0;i<CARDSNUM;i++) {
                String newNumber;
                switch(i%13) {
                    case 0:newNumber="K";break;
                    case 1:newNumber="A";break;
                    case 11:newNumber="J";break;
                    case 12:newNumber="Q";break;
                    default:newNumber=String.valueOf(i%13);
                }
                String newColor;
                switch(i/13) {
                    case 0:newColor="黑桃";break;
                    case 1:newColor="梅花";break;
                    case 2:newColor="方块";break;
                    default:newColor="红桃";
                }
                Cards newcard=new Cards(newNumber,newColor);
                cards.add(newcard); 
            }
            return cards;
        }
        
        //输入玩家数量或id(异常处理)
        public static int playerNum() {
            int player=2;
            while(true) {
                try {
                    player=sc.nextInt();
                    return player;
                } catch (Exception e) {
                    // 输入字符串的异常
                    System.out.println("请输入整数!不要输入奇怪的东西<<<<");
                    sc=new Scanner(System.in);
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:简易扑克牌游戏

          本文链接:https://www.haomeiwen.com/subject/tloomftx.html