import java.util.ArrayList;
創(chuàng)新互聯(lián)建站主要從事成都做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)三江侗,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
import java.util.Random;
public class PokerDemo {
ArrayListCard poker = new ArrayListCard();
Player[] players;
int n = 4;
String[] name = {"Wang", "Zhang", "Li", "Yuan"};
public PokerDemo() {
players = new Player[n];
for(int i = 0; i players.length; i++)
players[i] = new Player(name[i]);
for(int i = 0; i 4; i++)
for(int j = 0; j 13; j++)
poker.add(new Card(i, j));
System.out.println("洗牌前: " + poker);
xipai();
System.out.println("洗牌后: " + poker);
fapai();
System.out.println("發(fā)牌后: ");
for(int i = 0; i players.length; i++)
System.out.println(players[i]);
}
public void xipai() {
Random r = new Random();
ArrayListCard tmp = new ArrayListCard();
while(!poker.isEmpty()) {
int index = r.nextInt(poker.size());
Card card = poker.get(index);
tmp.add(card);
poker.remove(index);
}
poker = new ArrayListCard(tmp);
}
public void fapai() {
int p = 0;
while(!poker.isEmpty()) {
Card card = poker.get(0);
players[p].add(card);
p = (p + 1) % n;
poker.remove(0);
}
}
public static void main(String[] args) {
new PokerDemo();
}
}
class Card {
int val, sty;
final String[] value = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
final String[] style = {"黑桃", "紅心", "梅花", "方塊"};
public Card(int s, int v) {
sty = s;
val = v;
}
public String toString() {
return style[sty]+ " " + value[val];
}
}
class Player {
ArrayListCard cards = new ArrayListCard();
String name;
public Player(String n) {
name = n;
}
public void add(Card card) {
cards.add(card);
}
public String toString() {
return name + ":\t" + cards;
}
}
1.定義牌類
public class Card{
public int id;//牌大小
public String number;//牌值名稱
public String color;//花色
}
2.定義牌組,用ArrayList存放所有牌
ArrayListCard cardGroup =new ArrayListCard();
3.定義花色枚舉
public static final String[] COLORS=new String[]{"方塊","梅花","紅桃","黑桃"};
4.定義牌值大小枚舉
public static fina String[] CARDVALUES=new String[]{"A","1","2","3",
"4","5","6","7","8","9","10","J","Q","K","King_Small","King_Big"};
5.初始化牌組,這就是一副新牌,沒切過,己經(jīng)排好序的
public void initCardGroup(){
//加入A~K
for(int i=0;i13;i++){
for (int j=0;j4;j++){
//生成一張牌的對(duì)象
Card card=new Card();
card.id=i;
card.color=COLORS[j];
card.munber=CARDVALUES[i];
//將對(duì)象加入牌組
cardGroup.add(card);
}
}
//加入大小王
Card card_k1=new Card();
card_k1.id=13;
card_k1.color="King_Small";
card_k1.number="King_Small";
cardGroup.add(card_k1);
Card card_k2=new Card();
card_k2.id=14;
card_k2.color="King_big";
card_k2.number="King_big";
cardGroup.add(card_k2);
}
6. 洗牌,將原牌組打亂,得到新的牌組
public ArrayListCard flushCardsGroup(ArrayListCard src){
ArrayListCard result=new ArrayListCard();
while(src.size0){
int size=src.size();
//以size為限,生成0~size之間的隨機(jī)數(shù)
Random r=new Random();
int index=r.nextInt(size);
//對(duì)index做個(gè)檢查
if(index0){index=0;}
if(indexsize-1){index=size-1;}
//index就是撿出來的牌,加到新的數(shù)組中,從原來數(shù)組中刪除
Card card=src.get(index);
result.add(card);
card.remove(index);
}
return result;
}
你指的是Card里的toString方法吧
public?String?toString()?{
String?aa?=?suit?+?"?"?+?num?+?"??";
return?aa;
}
toString這個(gè)方法一般在?System.out.print時(shí)使用,這個(gè)是打印出String,JVM就是默認(rèn)調(diào)用類的toSting方法
注:所有類都有toString方法,默認(rèn)是當(dāng)前對(duì)象的hashcode,即內(nèi)存地址
所以在發(fā)牌是打印
public?void?dealcard()//發(fā)牌
{
for(int?i=0;i52;i++)
{
if(i%4==0i!=0){
System.out.println();??//每發(fā)4張牌輸出換行
}
//?就是這里,默認(rèn)調(diào)用card的toString方法
System.out.print(card[i]);?//依次輸出?發(fā)的牌
}
}