java 斗地主发牌

/**
* 
* @Title: Controller.java
* @Package cn.com.starit.servlet
* @Description: TODO(用一句话描述该文件做什么)
* @author gt
* @date 2012-7-8 下午08:57:39
* @copyright STARIT   
*/
package cn.com.starit.servlet;

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

/**
 * 发牌主程序
 *
 */
public class Controller {

	/**
	 * @Title: main
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param args    
	 * @return void 
	 * @throws
	 */
	public static void main(String[] args) {
		   Player p1=new Player(1,"huang");
           Player p2=new Player(2,"zhou");
           Player p3=new Player(3,"li");
          
           Player []players={p1,p2,p3};
          
           List<Card> cards=new ArrayList<Card>();
           //除了大王和小王分别加入到Card中 牌的数目为13*4
           for(int rank=Card.THREE;rank<=Card.DEUCE;rank++){
                 
                  cards.add(new Card(rank,Card.DIAMOND));
                  cards.add(new Card(rank,Card.HEART));
                  cards.add(new Card(rank,Card.CLUB));
                  cards.add(new Card(rank,Card.SPADE));
                 
           }
            //加入大小王  牌的数目为2
           cards.add(new Card(Card.BLACK,Card.JOKER));
           cards.add(new Card(Card.COLOR,Card.JOKER));
          
           //统计牌数一共是54=13*4+2
           System.out.println(cards.size());
         
           //就是随机打乱原来的顺序,和洗牌一样
           Collections.shuffle(cards);
           
        
          //给玩家发牌
           for(int i=0;i<players.length;i++){
                  for (int j = 0; j < 17; j++) {
                	     //cards.remove(0)  一张一张的获取牌数   每次获取17张牌
                         players[i].getList().add(cards.remove(0));
                  }
           }
          
          //玩家获得牌
           for (int i = 0; i < players.length; i++) {
                  System.out.println(players[i].getName()+players[i].getList());
           }
          
           System.out.println("剩余牌数");
           for(int i=0;i<cards.size();i++){
                  System.out.print(cards.get(i)+"  ");
           }

	}

}


/**
* 
* @Title: Player.java
* @Package cn.com.starit.servlet
* @Description: TODO(用一句话描述该文件做什么)
* @author gt
* @date 2012-7-8 下午08:56:41
* @copyright STARIT   
*/
package cn.com.starit.servlet;

import java.util.ArrayList;
import java.util.List;

/**
 *  玩家
 *
 */
public class Player {
	 private   int  id;
     private   String  name;
     //用来存储玩家的Card
     private  List<Card> list=new ArrayList<Card>();
     public  void  add(Card  card){
          list.add(card);
     }
     public Player() {
     }
     public Player(int id, String name) {
         this.id = id;
         this.name = name;
     }
     public int getId() {
         return id;
     }
     public void setId(int id) {
         this.id = id;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public List<Card> getList() {
         return list;
     }
     public void setList(List<Card> list) {
         this.list = list;
     }
    
    

}
/**
* 
* @Title: Card.java
* @Package cn.com.starit.servlet
* @Description: TODO(用一句话描述该文件做什么)
* @author gt
* @date 2012-7-8 下午08:56:11
* @copyright STARIT   
*/
package cn.com.starit.servlet;

/**
 * 扑克牌类
 *
 */
public class Card {
	 // 点数
    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 TEEN = 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 int DIAMOND = 0;
    // 红桃
    public static final int HEART = 1;
    // 梅花
    public static final int CLUB = 2;
    // 黑桃
    public static final int SPADE = 3;
    // 王
    public static final int JOKER = 4;
    // 花色
    public  static String[] suit_names = { "方块", "红桃", "梅花", "黑桃", "王" };
    public  static String[] rank_names = { "3", "4", "5", "6", "7", "8", "9",
            "10", "J", "Q", "K", "A", "2", "小", "大" };
    // 点数
    private int rank;
    // 花色
    private int suit;
 
    public Card() {
 
    }
 
    public Card(int rank, int suit) {
        setRank(rank);
        setSuit(suit);
    }
 
    public int getRank() {
        return rank;
    }
 
    public void setRank(int rank) {
        if (Card.THREE > rank || rank > Card.COLOR)
            throw new RuntimeException("超了");
        this.rank = rank;
    }
 
    public int getSuit() {
        return suit;
    }
 
    public void setSuit(int suit) {
        if (Card.DIAMOND > suit || suit > Card.JOKER)
            throw new RuntimeException("超了");
        this.suit = suit;
    }
 
    @Override
    public String toString() {
    	if(rank_names[rank]=="小"){
    		return rank_names[rank]+suit_names[suit];
    	}
    	else if(rank_names[rank]=="大"){
    		return rank_names[rank]+suit_names[suit];
    		
    	}
       return  suit_names[suit] + rank_names[rank];
    }

}
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值