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

java第三季:简单扑克牌游戏

dandelionLYY
关注TA
已关注
手记 1
粉丝 3
获赞 4

玩家类Player

public class Player {
    public String id;
    public String name;
    List<GetCard> getCard;
    public Player(String id,String name){
        this.id=id;
        this.name=name;
        getCard=new ArrayList<GetCard>();
    }

}

牌类型Card

/**
 * 该类用作对牌类型的处理,
 * @author Dandelion
 *  
 */

public class Card {

    public String[] cardColor={"黑桃","红桃","梅花","方片"};
    public String[] cardNum={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
}

单张牌获取GetCard

/**
 * 
 * 单张牌
 * 
 * @author Dandelion
 *
 */
public class GetCard {
    public String color;
    public String num;
    /**
     * 牌的花色和点数
     * @param color 
     * @param num
     */
    public GetCard(String color,String num){
        this.color=color;
        this.num=num;
    }

}

测试工具类TestCard

public class TestCard {
    List<GetCard> getCards;

    List<Player> players=new ArrayList<Player>();;

    public void testCreateCard(){
        Card card=new Card();
        getCards=new ArrayList<GetCard>();
        for(int i=0;i<card.cardColor.length;i++)
        {
            for(int j=0;j<card.cardNum.length;j++)
            {
                getCards.add(new GetCard(card.cardColor[i], card.cardNum[j]));

            }
        }
        System.out.println("牌创建成功!");
        for (GetCard getCard : getCards) {
            System.out.print(" "+getCard.color+":"+getCard.num);
        }

         System.out.println("开始洗牌!!!");
         /**
          * 使用Collections.shuffle(list)进行随机排序
          */
         Collections.shuffle(getCards);

        System.out.println("洗牌完毕!!!");

          System.out.println("洗牌结果如下:");
         for (GetCard getCard : getCards) {
            System.out.print(" "+getCard.color+":"+getCard.num);
        }
         System.out.println();

        /*for (GetCard getCard : getCards) {
            System.out.print(" "+getCard.color+":"+getCard.num);
        }
        */
    }
    public void testCreatePlayer(){

        System.out.println("请创建玩家进行游戏!!!");
            Scanner console=new Scanner(System.in);
            for(int i=0;i<2;i++){
                System.out.println("请创建第"+(i+1)+"玩家:");
                System.out.println("ID:");
                String idplay=console.next();
                System.out.println("Name:");
                String nameplay=console.next();
                Player player=new Player(idplay, nameplay);
                players.add(player);
            }
        System.out.println("创建完毕");
        System.out.println(" "+players.get(0).name+"  "+
        players.get(1).name);

    }

    public void testStartGame(){

        System.out.println("开始发牌!!!");
        for(int i=0;i<4;i++){
            if(i%2==0){
                System.out.println("玩家:"+players.get(0).name+"获得手牌");
                players.get(0).getCard.add(getCards.get(i));
            }
            else {
                System.out.println("玩家:"+players.get(1).name+"获得手牌");
                players.get(1).getCard.add(getCards.get(i));
            }
        }

        for(int i=0;i<2;i++)
        {
            System.out.print("玩家 "+players.get(i).name+"的手牌:");
            for(int j=0;j<players.get(i).getCard.size();j++)
            {

                System.out.print(" "+players.get(i).getCard.get(j).color+" "+
                players.get(i).getCard.get(j).num);
            }
            System.out.println();
        }
    }

    public void compareRule(){
        Card poker=new Card();
        int k[][]=new int[2][2];
        for (int p=0;p<players.size();p++) {
            GetCard card1=players.get(p).getCard.get(0);
            GetCard card2=players.get(p).getCard.get(1);
            int tmp1=0,tmp2=0;
            for(int i=0;i<poker.cardNum.length;i++)
            {
                if(card1.num.equals(poker.cardNum[i]))
                    tmp1=i;
                if(card2.num.equals(poker.cardNum[i]))
                    tmp2=i;
            }

                int colortmp1=0,colortmp2=0;
                for(int i=0;i<poker.cardColor.length;i++)
                {
                    if(card1.color.equals(poker.cardColor[i]))
                        colortmp1=i;
                    if(card2.color.equals(poker.cardColor[i]))
                        colortmp2=i;
                }
                if(tmp1>tmp2)
                    {k[p][0]=tmp1;k[p][1]=colortmp1;}
                else if(tmp1<tmp2)
                    {k[p][0]=tmp2;k[p][1]=colortmp2;}

                else if(tmp1==tmp2)
                {   
                    if(colortmp1>colortmp2)
                        {
                            k[p][0]=tmp2;
                            k[p][1]=colortmp2;
                        }
                    else
                        {k[p][0]=tmp1;k[p][1]=colortmp1;}
                }

            }
            if(k[0][0]>k[1][0])
                System.out.println("玩家"+players.get(0).name+"的牌大,获胜!!!!");
            else if(k[0][0]<k[1][0])
                System.out.println("玩家"+players.get(1).name+"的牌大,获胜!!!!");
            else if(k[0][0]==k[1][0])
            {
                if(k[0][1]>k[1][1])
                    System.out.println("玩家"+players.get(1).name+"的牌大,获胜!!!!");
                else 
                    System.out.println("玩家"+players.get(0).name+"的牌大,获胜!!!!");  
            }
        }

    public static void main(String[] args) {
        TestCard tc=new TestCard();
        tc.testCreateCard();
        tc.testCreatePlayer();
        tc.testStartGame();
        tc.compareRule();
    }
}
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP

热门评论

你好 请问一下运行结果是什么?

查看全部评论