斗地主(洗牌 发牌)

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

public class Game {

    private ArrayList<Player> plays;
    private ArrayList<Poker> pokers;

    public static void main(String[] args) {
        new Game().start();
    }

    private void start() {
        //初始化
        init();
        //洗牌
        xiPai();
        //发牌
        faPai();
        //叫地主
        jiaoDiZhu();
        //整牌
        zhengPai();
    }

    private void xiPai() {
        Collections.shuffle(pokers);//随机打乱顺序
    }

    private void zhengPai() {
        for (int i = 0; i < plays.size(); i++) {

            Player player = plays.get(i);
            Collections.sort(player.getPokerList());//排序。需要给Poker实现Comparable接口
            System.out.println(player);
        }
    }

    private void jiaoDiZhu() {
        //随机玩家当地主
        Random random  = new Random();
        int randomIndex = random.nextInt(3);
        Player diZhu = plays.get(randomIndex);
        diZhu.setRole("地主");

        List<Poker> diPai = pokers.subList(51,54);//底牌
        //把底牌给地主
        diZhu.getPokerList().addAll(diPai);
    }

    private void faPai() {
        for (int i = 0; i < pokers.size()-3; i++) {//-3 剩余3张底牌

            if(i % 3 == 0){
                //第一个玩家
                Player player = plays.get(0);
                player.getPokerList().add(pokers.get(i));//把牌发到每个玩家手里
            }else if( i % 3 == 1){
                //第二个玩家
                Player player = plays.get(1);
                player.getPokerList().add(pokers.get(i));
            }else{
                //第三个玩家
                Player player = plays.get(2);
                player.getPokerList().add(pokers.get(i));
            }

        }
    }

    private void init() {
        //初始化玩家
        plays = new ArrayList<>();
        plays.add(new Player("1001","张三","农民"));
        plays.add(new Player("1002","李四","农民"));
        plays.add(new Player("1003","王五","农民"));

        //初始化牌
        pokers = new ArrayList<>();
        String[] colors = {"♥","♠","♦","♣"};
        String[] values = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        for (int j = 0; j < values.length; j++) {
            for (int i = 0; i < colors.length; i++) {
                pokers.add(new Poker(colors[i],values[j],j));
            }
        }

        pokers.add(new Poker("","小王",14));
        pokers.add(new Poker("","大王",15));

        //System.out.println("pokers = " + pokers);

    }

}
import java.util.ArrayList;

public class Player {

    private String id;
    private String name;
    private String role;//地主或农民
    private ArrayList<Poker> pokerList=new ArrayList<>();//手里的牌

    @Override
    public String toString() {
        return "["+role+"]"+name+":"+pokerList;
    }

    public Player() {
    }

    public Player(String id, String name, String role) {
        this.id = id;
        this.name = name;
        this.role = role;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public ArrayList<Poker> getPokerList() {
        return pokerList;
    }

    public void setPokerList(ArrayList<Poker> pokerList) {
        this.pokerList = pokerList;
    }
}
import java.util.Objects;

public class Poker implements Comparable<Poker>{

    private String color;//花色
    private String value;//图形
    private int num;//数值


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Poker poker = (Poker) o;
        return Objects.equals(color, poker.color) && Objects.equals(value, poker.value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(color, value);
    }

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

    public Poker() {
    }

    public Poker(String color, String value,int num) {
        this.color = color;
        this.value = value;
        this.num = num;
    }

    public String getColor() {
        return color;
    }

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

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public int compareTo(Poker o) {
        return this.num - o.num;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值