问答详情
源自:7-1 简易扑克牌游戏

经历半年的学习,终于搞定了,欢迎大家讨论!

//扑克类;
public class Poker {
    private String flower;
    private String number;
    public String getFlower() {
        return flower;
    }
    public void setFlower(String flower) {
        this.flower = flower;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public Poker(String flower, String number) {
        this.flower = flower;
        this.number = number;
    }        
}
//玩家类
public class Player {
    private String name;
    private int id;
    private ArrayList <Poker>myPoker= new ArrayList<Poker>();
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public ArrayList<Poker> getMyPoker() {
        return myPoker;
    }
    public void setMyPoker(Poker poker) {
        this.myPoker.add(poker);
    }
    
    
    public Player(String name, int id) {
        this.name = name;
        this.id = id;
        this.myPoker = myPoker;
    }
        
    //显示玩家手牌
    public void show(){
        System.out.print("玩家"+getName()+": ");
        for (Poker p : myPoker) {
            System.out.print(p.getFlower()+p.getNumber()+" ");
        }
        
        System.out.println();
    }
    
    //玩家获胜信息
    
    public void winShow(Player p){
        System.out.println("-----玩家"+p.getName()+"获胜!-----");    
    }
    

}

http://img.mukewang.com/553755c600011bd703880502.jpg结果。

提问者:C是什么 2015-04-22 16:03

个回答

  • Youngguava
    2015-04-22 21:25:53
    已采纳

    写的真棒。。。。额,原来collection.shuffle可以打乱数组。。。。我寒假纠结如何洗牌的问题纠结了很久

  • demo_h
    2015-04-22 20:01:38

    其中一些算法可以更精简点 建议你看些算法

  • C是什么
    2015-04-22 16:24:44

    这网页有点问题,把中间部分注释掉了。

  • C是什么
    2015-04-22 16:04:18

    //游戏工具类
    public class GameUtil {
        private ArrayList <Poker>pokerList = new ArrayList<Poker>(); // 扑克牌集合
    
        public ArrayList<Poker> getPokerList() {
            return pokerList;
        }
    
        public void setPokerList( ArrayList<Poker> pokerList) {
            this.pokerList = pokerList;
        }
        
        /**
         * 创建扑克牌,并存入pokerList中;
         */
        public void makeUp(){
            System.out.println("--------------开始建牌------------");
            Poker[] p= new Poker[52];
            //建立单张
            String f[] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
            String n[] = {"黑桃","红心","梅花","方块"};
            for(int i=0;i<n.length;i++){
                for(int j=0;j<f.length;j++){
                    p[i*13+j] = new Poker(n[i],f[j]);
                }            
            }
            //将单张写入集合
            for (int i = 0; i < p.length; i++) {
                pokerList.add(p[i]);            
            }
            //测试扑克建立情况
            System.out.print("为:[");
            for (Poker pk : pokerList) {
                System.out.print(pk.getFlower()+pk.getNumber()+"、");
            }
            System.out.print("]");
            System.out.println();
            System.out.println("--------------建牌结束------------");
        }
        
        /**
         * 洗牌;
         */    
        public void shuffle(){
            System.out.println("--------------开始洗牌-----------");
            /*System.out.print("为:[");*/
            Collections.shuffle(pokerList);
            /*测试洗牌情况
            for (Poker pk : pokerList) {
                System.err.print(pk.getFlower()+pk.getNumber()+"、");            
            }
            System.out.print("]");*/
            System.out.println();
            System.out.println("--------------洗牌结束-----------");
        }
        /**
         * 发牌
         */
        public void setPoker(Player p1,Player p2){
            System.out.println("--------------开始游戏-----------");
            for (int i = 0; i < 4; i++) {
                if(i%2==0){
                System.out.println("玩家"+p1.getName()+"拿牌!");
                p1.setMyPoker(pokerList.get(i));
                }else{
                    System.out.println("玩家"+p2.getName()+"拿牌!");
                    p2.setMyPoker(pokerList.get(i));
                }
            }         
            
        }
        
        /**
         * 比牌
         */
        
        public void contrast(Player p1,Player p2){
            //玩家1自己的牌進行比較
            Poker myp1 = this.eqMyPoker(p1); //第一个玩家的最大牌
            //玩家2自己的牌進行比較
            Poker myp2 = this.eqMyPoker(p2); //第二个玩家的最大牌
            System.out.println("玩家"+p1.getName()+"的最大手牌为:"+myp1.getFlower()
                    +myp1.getNumber());
            System.out.println("玩家"+p2.getName()+"的最大手牌为:"+myp2.getFlower()
                    +myp2.getNumber());    
            
            //两个玩家进行比较
            if(this.eqNumber(myp1)!=this.eqNumber(myp2)){
                if(this.eqNumber(myp1)>this.eqNumber(myp2)){
                    p1.winShow(p1);
                    System.out.println("玩家各自的手牌为:");
                    p1.show();
                    p2.show();
                }else{
                    p2.winShow(p2);                
                    System.out.println("玩家各自的手牌为:");
                    p1.show();
                    p2.show();
                }
                
            }else{
                if(this.eqFlower(myp1)>this.eqFlower(myp2)){
                    p1.winShow(p1);                
                    System.out.println("玩家各自的手牌为:");
                    p1.show();
                    p2.show();
                }
            }
        }
        /**
         *  玩家的牌进行比较
         */
        public int eqFlower(Poker p){//比较花色
            int flower = 0;
            for(int i = 0;i<2;i++){
                switch(p.getFlower()){
                case "黑桃": flower = 4;break;
                case "红心": flower = 3;break;
                case "梅花": flower = 2;break;
                case "方块": flower = 1;break;
                default : System.out.println("花色2失败!"); break;
                }
            }        
            return flower;
            
        }
        
        public int eqNumber(Poker p){//比较点数
    
            int number = 0;
            for(int i = 0;i<2;i++){
                switch(p.getNumber()){
                case "1": number = 1;break;
                case "2": number = 2;break;
                case "3": number = 3;break;
                case "4": number = 4;break;
                case "5": number = 5;break;
                case "6": number = 6;break;
                case "7": number = 7;break;
                case "8": number = 8;break;
                case "9": number = 9;break;
                case "10": number = 10;break;
                case "J": number = 11;break;
                case "Q": number = 12;break;
                case "K": number = 13;break;
                case "A": number = 14;break;
                default : System.out.println("点数失败!"); break;
                }
            }        
            return number;
            
        }
        //内部比较,确定最大的一张牌
        public Poker eqMyPoker(Player p){
            if(eqNumber(p.getMyPoker().get(0))!=eqNumber(p.getMyPoker().get(1))){
                if(eqNumber(p.getMyPoker().get(0))>eqNumber(p.getMyPoker().get(1))){
                    return p.getMyPoker().get(0);
                            
                }else{
                    return p.getMyPoker().get(1);
                }
            }else{
                if(eqFlower(p.getMyPoker().get(0))>eqFlower(p.getMyPoker().get(1))){
                    return p.getMyPoker().get(0);
                }else{
                    return p.getMyPoker().get(1);
                }
                
            }
            
        }
        /**
         * 创建玩家
         * 
         */
        
        public void setPlayer(){
            System.out.println("---------开始创建玩家------------");
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入第一位玩家ID:");
            try {
                int id = sc.nextInt();        
                System.out.print("请输入第一位玩家姓名:");
                String name = sc.next();        
                Player p1 = new Player(name, id);
                System.out.println("欢迎玩家"+p1.getName()+"!");
                
                System.out.print("请输入第二位玩家ID:");
                int id2 = sc.nextInt();
                System.out.print("请输入第二位玩家姓名:");
                String name2 = sc.next();        
                Player p2 = new Player(name2, id2);
                System.out.println("欢迎玩家"+p2.getName()+"!");
                sc.close();
                System.out.println("---------创建玩家结束------------");
                setPoker(p1,p2); //发牌;
                this.contrast(p1, p2); //比牌;
                
            } catch (Exception e) {
                System.err.println("请输入整数!");
                setPlayer();
            }
        }
        
        
    }
    
    //游戏启动类
    public class Game {
    
        public static void main(String[] args) {
            GameUtil gu = new GameUtil();
            gu.makeUp();
            gu.shuffle();
            gu.setPlayer();
            
    
        }
    
    }