您的位置:首页 > 其它

扑克类的实现

2016-01-22 13:46 155 查看
package com.loco;
/**
* 扑克类(一副扑克)
* @author Administrator
*
*/
public class Poker {
private static String[] suites = {"黑桃","红桃","草花","方块"};
private static int[] faces = {1,2,3,4,5,6,7,8,9,10,11,12,13};
private Card[] cards;
/**
* 构造器
*/
public Poker(){
cards = new Card[52];
for(int i=0;i<suites.length;i++){
for(int j=0;j<faces.length;j++){
cards[i*13 + j] = new Card(suites[i],faces[j]);
}
}
}
/**
* 洗牌
* @author Administrator
*
*/
public void shuffle(){
for(int i=0,len=cards.length;i<len;i++){
int index = (int) (Math.random() * len);
Card temp = cards[index];
cards[index] = cards[i];
cards[i] = temp;
}
}
/**
* 发牌
* @author Administrator
*
*/
public Card deal(int index){
return cards[index];
}
/**
* 卡片类(一张扑克)
* @author Administrator
*
*/
public class Card{
private String suite;
private int face;
public Card(String suite, int face) {
this.suite = suite;
this.face = face;
}
public String toString(){
String faceStr = "";
switch(face){
case 10:faceStr = "A";break;
case 11:faceStr = "J";break;
case 12:faceStr = "Q";break;
case 13:faceStr = "K";break;
default:faceStr = String.valueOf(face);
}
return suite + faceStr;
}
}
}

package com.loco;

public class PokerTest {

public static void main(String[] args) {
Poker poker = new Poker();
poker.shuffle();
// Poker.Card c1 = poker.deal(0);
for(int i=1;i<=52;i++){
if(i%10==0){
System.out.println(poker.deal(i-1));
}else{
System.out.print(poker.deal(i-1) + " ");
}
}
}
}

引用地址:http://blog.csdn.net/jackfrued/article/details/17339393
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: