斗地主游戏发牌java程序

1、定义排
    public class Card {

    private int suit;
    private int rank;    //花色和牌数

    public Card() {}

    public Card(int suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }
    public int getSuit() {
        return suit;
    }

    public void setSuit(int suit) {
        this.suit = suit;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + rank;
        result = prime * result + suit;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Card other = (Card) obj;
        if (rank != other.rank)
            return false;
        if (suit != other.suit)
            return false;
        return true;
    }

    public static final int DIAMOND = 0;
    public static final int CLUB = 1;
    public static final int HEART = 2;
    public static final int SPADE = 3;
    public static final int JOKER = 4;

    public static final int THREE = 0;
    public static final int FOUR = 1;
    public static final int FIVE = 2;
    public static final int SIX = 3;
    public static final int SEVEN = 4;
    public static final int EIGHT = 5;
    public static final int NINE = 6;
    public static final int TEN = 7;
    public static final int JACK = 8;
    public static final int QUEEN = 9;
    public static final int KING = 10;
    public static final int ACE = 11;
    public static final int DEUCE = 12;
    
    public static final int BLACK = 13;  // 小王
    public static final int COLOR = 14;  // 大王

    public static final String[] SUIT_NAME = { "方块", "梅花", "红桃", "黑桃", "王" };
    public static final String[] RANK_NAME = { "3", "4", "5", "6", "7", "8",
            "9", "10", "J", "Q", "k", "A", "2", "小王", "大王" };

    public String toString(){
        return SUIT_NAME[this.suit]+RANK_NAME[this.rank];
    }  
}

2、定义玩家
import java.util.ArrayList;
import java.util.Collections;

    public class Player {
    private String name;
    private int id;
    private ArrayList cards = new ArrayList();

    public Player(ArrayList cards, int id, String name) {
        super();
        this.cards = cards;
        this.id = id;
        this.name = name;
    }

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

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;

        result = prime * result + id;
        return result;
    }

    public String getName() {
        return name;
    }

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

    public ArrayList getCards() {
        return cards;
    }

    public void setCards(ArrayList cards) {
        this.cards = cards;
    }
   
    public void add(Card a)
    {
        cards.add(a);
    }
   
    public String toString(){
        return this.name+this.cards;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Player other = (Player) obj;
        if (id != other.id)
            return false;
        return true;
    }
}

3、发牌程序
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Iterator;

public class playgame {

    public static void main(String[] args) {

        ArrayList<Card> cards = new ArrayList<Card>();   //定义ArrayList<Card>型的cards容器

        for (int rank = Card.THREE; rank <= Card.DEUCE; rank++) {   //循环获取整副牌
            cards.add(new Card(Card.CLUB, rank));
            cards.add(new Card(Card.SPADE, rank));
            cards.add(new Card(Card.DIAMOND, rank));
            cards.add(new Card(Card.HEART, rank));
        }
        cards.add(new Card(Card.JOKER, Card.BLACK));
        cards.add(new Card(Card.JOKER, Card.BLACK));

        Collections.shuffle(cards);         //随机发牌

        System.out.println("整副牌是" + cards);         //此处是输出整副牌
---------------------------------------------------------------------------------
1、第一种发牌程序写法
        List<Player> players = new ArrayList<Player>();
        players.add(new Player(1, "张三"));
        players.add(new Player(2, "李四"));
        players.add(new Player(3, "王五"));       //定义三个玩家:张三、李四、王五

        Iterator<Card> it = cards.iterator();
        int idx = 0;
        while (it.hasNext()) {
            Card c = it.next();
            Player p = players.get(idx++ % players.size());  //给三个玩家发牌
            p.add(c);
            it.remove();
            if (cards.size() == 3) {           //直至最后剩下三张牌
                break;
            }
        }
        System.out.println("玩家1的牌是:" + players.get(0));
        System.out.println("玩家2的牌是:" + players.get(1));
        System.out.println("玩家3的牌是:" + players.get(2));
   
--------------------------------------------------------------------------------
2、第二种发牌程序写法
        ArrayList<Card> p1 = new ArrayList<Card>();
        ArrayList<Card> p2 = new ArrayList<Card>();
        ArrayList<Card> p3 = new ArrayList<Card>();
        ArrayList<Card> p4 = new ArrayList<Card>();  //定义四个ArrayList<Card>容器,用来盛牌

        for (int i = 0; i < 51; i += 3) {
            p1.add(cards.get(i));
            p2.add(cards.get(i + 1));
            p3.add(cards.get(i + 2));         //循环发牌给前三个容器
        }
        for (int i = 0; i < 3; i++) {       //最后三张牌放在最后一个容器
            p4.add(cards.get(i));
             cards.remove(0);
        }
*******************************************************************
承第二种发牌程序,只是发牌方式不同
      for (int i = 0; i < 51; i += 3) {    //另一种发牌的方式
            cards1.add(cards.get(0));
            cards.remove(0);
            cards2.add(cards.get(0));
            cards.remove(0);
            cards3.add(cards.get(0));
            cards.remove(0);
        }  
       for (int i = 0; i < 3; i++) {
            cards4.add(cards.get(0));
            cards.remove(0);
        }                                 
*******************************************************************
        System.out.println("玩家1的牌是:" + p1);
        System.out.println("玩家2的牌是:" + p2);
        System.out.println("玩家3的牌是:" + p3);
        System.out.println("地主的最后三张牌是:" + p4);   //输出容器里的牌
-------------------------------------------------------------------------------      
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值