继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

扑克牌Esay版游戏--比大小(请大神指正)

慕婉清8869734
关注TA
已关注
手记 1
粉丝 0
获赞 1
package com.Demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class playCards {

    /**
     * 洗牌方法
     */
    public static  void cardShuffle(List<Cards> list){
        Collections.shuffle(list);   // shuffle 打乱顺序    
    }

    /**
     * 玩家比大小
     */
    public static Players comparePlayer(Players o1,Players o2){
        o1.BigPc=o1.compareCard(o1.p1, o1.p2);
        o2.BigPc=o2.compareCard(o2.p1, o2.p2);
        Players pl= new Players();
        if(pl.compareCard(o1.BigPc,o2.BigPc )==o1.BigPc){
        return o1;
        }else if(pl.compareCard(o1.BigPc,o2.BigPc )==o2.BigPc){
            return o2;
        }else  {
            return null;
            }

        }

/**
 * 实现功能
 * 1.创建一副扑克牌:四个花色(黑桃,红桃,梅花,方片),13个点数:2-10,JQ,K,A, 不考虑大小王
 * 2.创建两个玩家
 * 3.洗牌(打乱顺序)
 * 4. 发牌(从第一张开始发,一个人一张,每个人两张)
 * 5. 比较牌的大小,得出结论
 * @param args
 */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StringBuffer sb= new StringBuffer();
        CreatCards cc= new CreatCards();
        List<Cards>  cards= cc.AddCards();
        // Players[] players= new Players[]{};
        List<Players> players= new ArrayList<Players>();
        Players winPlayer;
         int num=0;
         int k=0;

        System.out.println("------------------创建扑克牌----------------------");
        for(Cards c:cards){
            sb.append(c.color+c.num+"  ");
        }
        System.out.println("----------- ----扑克牌创建成功!--- -------------");
        System.out.print("牌为:["+sb+" ]");
        System.out.print("\n");

        System.out.println("-----------------开始洗牌。。。-----------------");
        cardShuffle(cards);
        System.out.println("洗牌后的牌为:");
        for(Cards card:cards){
            System.out.print(card.color+card.num+"  ");
        }
        System.out.print("\n");
        System.out.println("-------------------  洗牌结束-----------------------");
        System.out.println("-------------------创建玩家------------------------");
        while(num<2){
        System.out.println("请输入第"+(num+1)+"位玩家的ID和姓名:");

            try{
                System.out.println("输入ID:");
                Integer  ID =new Scanner(System.in).nextInt();
                int     id=ID;
                num++;
                System.out.println("输入玩家姓名:");
                 String name=new Scanner(System.in).next();
                 String Name=name;
                 players.add( new Players(id,Name,null,null));
            } catch(Exception e){
                   System.out.println("输入的不是整数,请重新输入ID!");   
                   continue;
            }   
        }
        System.out.println("---------------欢迎玩家:"+players.get(0).name);
        System.out.println("---------------欢迎玩家:"+players.get(1).name);
        System.out.println("------------------开始发牌---------------------");

        System.out.println("-----------玩家:"+players.get(0).name+"-拿牌-----");
        players.get(0).p1=cards.get(k++);
        System.out.println("-----------玩家:"+players.get(1).name+"-拿牌-----");
        players.get(1).p1=cards.get(k++);
        System.out.println("-----------玩家:"+players.get(0).name+"-拿牌-----");
        players.get(0).p2=cards.get(k++);
        System.out.println("-----------玩家:"+players.get(1).name+"-拿牌-----");
        players.get(1).p2=cards.get(k++);
        System.out.println("------------------发牌结束!---------------------");
        System.out.println("------------------开始游戏~~~---------------------");
        //玩家手中的最大牌
        players.get(0).BigPc    =players.get(0).compareCard(players.get(0).p1, players.get(0).p2);
        players.get(1).BigPc    =players.get(1).compareCard(players.get(1).p1, players.get(1).p2);

        System.out.println("玩家:"+players.get(0).name+"最大的牌为:"+players.get(0).BigPc.color+players.get(0).BigPc.num);
        System.out.println("玩家:"+players.get(1).name+"最大的牌为:"+players.get(1).BigPc.color+players.get(1).BigPc.num);

        winPlayer=comparePlayer(players.get(0),players.get(1));
        if(winPlayer!=null){
            System.out.println("----------玩家:"+winPlayer.name +"获得胜利!-----");
        }else{
            System.out.println("玩家的牌一样大!");
        }

        System.out.println("玩家各自的牌为:");
        System.out.println(players.get(0).name+":"+players.get(0).p1.color+players.get(0).p1.num+"   " +players.get(0).p2.color+players.get(0).p2.num);
        System.out.println(players.get(1).name+":"+players.get(1).p1.color+players.get(1).p1.num+"   " +players.get(1).p2.color+players.get(1).p2.num);

    }
}

package com.Demo;

public class Cards {

    public String  color;
    public  String  num;
    public Cards(){

    }
    public Cards(String color, String  num){
        this.color=color;
        this.num=num;
    }

    public int compareColor(Cards c){
        if(c.color.equals("黑桃"))
            return  4;
        if(c.color.equals("红桃"))
            return  3;
        if(c.color.equals("梅花"))
            return  2;
        if(c.color.equals("方片"))
            return  1;
        return 0;
    }

    public int compareNum(Cards c){
        if(c.num.equals("2"))
            return 2;
        if(c.num.equals("3"))
            return 3;
        if(c.num.equals("4"))
            return 4;
        if(c.num.equals("5"))
            return 5;
        if(c.num.equals("6"))
            return 6;
        if(c.num.equals("7"))
            return 7;
        if(c.num.equals("8"))
            return 8;
        if(c.num.equals("9"))
            return 9;
        if(c.num.equals("10"))
            return 10;
        if(c.num.equals("J"))
            return 11;
        if(c.num.equals("Q"))
            return 12;
        if(c.num.equals("K"))
            return 13;
        if(c.num.equals("A"))
            return 14;
            return 0;
    }
}

package com.Demo;

import java.util.ArrayList;
import java.util.List;

public class CreatCards {
     List<Cards> cardList= new ArrayList<Cards>();
     public String[] Color={"黑桃","红桃","梅花","方片"};
     public String[] Num={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

//添加牌
 public List<Cards> AddCards(){
     for(int i=0;i<4;i++){
         for(int j=0;j<13;j++){
             cardList.add(new Cards(Color[i],Num[j]));
         }
     }
     return cardList;
    }    
}

package com.Demo;

public class Players {
     public  int  id;
     public String name;
     public     Cards p1;//牌1
     public  Cards p2;//牌2
    public  Cards BigPc; //大牌
    public Players(){   
    }
    public Players(int id,String name,Cards p1,Cards p2){
        this.id=id;
        this.name=name;
        this.p1=p1;
        this.p2=p2;
    }

    /**
     *牌比大小
     */
    public Cards compareCard(Cards c1,Cards c2){
    // 先比点数大小
        if(c1.compareNum(c1)>c2.compareNum(c2))
          return c1;
      else if(c1.compareNum(c1)<c2.compareNum(c2))
          return c2;
     //点数相同,比花色大小
      else {
          if(c1.compareColor(c1)>c2.compareColor(c2))
              return c1;
          else if(c1.compareColor(c1)<c2.compareColor(c2))
              return c2;
          else 
              return null;    
      }
    }
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP