模拟斗地主游戏发牌


一、需求

在启动游戏房间的时候,应该提前准备好54张牌,完成洗牌、发牌、牌排序、逻辑。

二、分析

1、当系统启动的同时需要准备好数据的时候,就可以用静态代码块了。
2、洗牌就是打乱牌的顺序。
3、定义三个玩家、依次发出51张牌
4、给玩家的牌进行排序(拓展)
5、输出每个玩家的牌数据。

三、实现步骤

1、创建Card类


public class Card {
    private String size;
    private String color;
    private int index;//给卡片标序号

    public Card() {
    }

    public Card(String size, String color,int index) {
        this.size = size;
        this.color = color;
        this.index=index;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public String toString() {
        return size+color;
    }
}

2、创建GameDemo类


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

public class GameDemo {

     //1、定义一个静态集合存储54张牌对象
    public static List<Card> allCards=new ArrayList<>();
    //2、做牌:定义静态代码块初始化数据
    static {
        String[] sizes={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
      String[] colors={"♠","♥","♦","♣"};
        int index=0;//记录牌的大小
    for (String size : sizes) {
index++;
        for (String color : colors) {
            //封装成牌对象
            Card c=new Card(size,color,index);
            //存放到集合容器中去
            allCards.add(c);
        }
    }
     //大小王放进去
    Card c1=new Card("","🃏",++index);
    Card c2=new Card("","😀",++index);
    Collections.addAll(allCards,c1,c2);
    System.out.println("新牌:"+allCards);
    }
    public static void main(String[] args) {
        //洗牌:
        Collections.shuffle(allCards);
        System.out.println("洗牌后:"+allCards);

        //发牌
List<Card> linghuchong=new ArrayList<>();
List<Card> jimozhi=new ArrayList<>();
List<Card> renyingying=new ArrayList<>();
        for (int i = 0; i < allCards.size()-3; i++) {
            //先拿到牌对象
            Card c=allCards.get(i);
            if(i%3==0){
                linghuchong.add(c);
            }else if(i%3==1){
                jimozhi.add(c);
            }else if(i%3==2){
                renyingying.add(c);
            }
        }

        //拿到最后三张底牌,把最后三张牌截取成一个子集和
        //allCards.get(51);//楼
        List<Card> lastThreeCards=allCards.subList(allCards.size()-3,allCards.size());
        //给玩家牌排序(从大到小)
        sortCards(linghuchong);
        sortCards(jimozhi);
        sortCards(renyingying);
        //输出玩家牌
        System.out.println("linghuchong:"+linghuchong);
        System.out.println("jimozhi:"+jimozhi);
        System.out.println("renyingying:"+renyingying);
        System.out.println("底牌:"+lastThreeCards);
    }
/*
* 给牌排序
* */
    private static void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                //要知道牌大小才能制定规则
                return o2.getIndex()- o1.getIndex();
            }
        });
    }
}

在这里插入图片描述

3、测试代码


总结

<font color=#999AAA
以上主要是运用ArrayList集合来存储对象,其中,用到了集合里非常重要的 Collections.sort()方法,可以进行排序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

取个锤子名子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值