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

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

标签:
Java

玩家类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();
    }
}
点击查看更多内容
2人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消