重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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.初始化牌组,这就是一副新牌,没切过,己经排好序的
public void initCardGroup(){
//加入A~K
for(int i=0;i13;i++){
for (int j=0;j4;j++){
//生成一张牌的对象
Card card=new Card();
card.id=i;
card.color=COLORS[j];
card.munber=CARDVALUES[i];
//将对象加入牌组
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之间的随机数
Random r=new Random();
int index=r.nextInt(size);
//对index做个检查
if(index0){index=0;}
if(indexsize-1){index=size-1;}
//index就是捡出来的牌,加到新的数组中,从原来数组中删除
Card card=src.get(index);
result.add(card);
card.remove(index);
}
return result;
}
//牌类
public class Card {
private int face; //牌的面值
private int suit; //牌的花色
public Card(int face, int suit) {
super();
this.face = face;
this.suit = suit;
}
public int getFace() {
return face;
}
public void setFace(int face) {
this.face = face;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
}
//模拟洗牌和发牌
public class Test {
public static void main(String[] args) {
Card[] deck = new Card[52];
fillDeck(deck);
deal(deck);
System.out.println();
}
private static void fillDeck(Card[] deck) {
int i, j=0;
for (i = 0; i 52; i++) {
j = (int) (Math.random() * 52);
deck[i]=new Card(j%13,j/13);
}
}
private static void deal(Card[] deck) {
int k1, k2;
System.out.println();
for (k1 = 0, k2 = k1 + 26; k1 26; k1++, k2++) {
if (k1 % 13 == 0)
System.out.println("\n第" + (k1 / 13 + 1) +
"组牌:\t\t\t第"+ (k1 / 13 + 3) + "组牌:");
System.out.print("\nCard: " + deck[k1].getFace() +
" Suit: "+ deck[k1].getSuit());
System.out.print("\t\t\tCard: " + deck[k2].getFace() +
" Suit: "+ deck[k2].getSuit());
}
}
}
洗牌过程?一句话就完成洗牌过程了!!
代码如下:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CardGame {
public static void main(String[] args) throws IOException {
String[] types={"黑桃","草花","红心","方块"};
String[] value={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] king={"JOKER","joker"};
ListString cards=new ArrayListString();//将一副牌54张放入待洗牌的集合中
for(int i=0;itypes.length;i++){
for(int j=0;jvalue.length;j++){
cards.add(types[i]+value[j]);//每个花色的2-A
}
}
cards.add(king[0]);//大鬼
cards.add(king[1]);//小鬼
int m=0;
System.out.println("洗牌之前----------------------------------------------------\n");
for(String str:cards)
{
System.out.print(str+"\t");
m++;
if(m%13==0)
System.out.println();
}
Collections.shuffle(cards);//洗牌动作
m=0;
System.out.println("\n\n洗牌之后-----------------------------------------------------\n");
for(String str:cards)//输出洗牌后的结果
{
System.out.print(str+"\t");
m++;
if(m%13==0)
System.out.println();
}
}
}
我在这里把所有的花色都加入一起洗牌了,楼主可根据需要进行特殊花色的洗牌!!你多执行几次就会发现每次洗牌后的牌的位置都不是一样的!!关键在一个洗牌动作的方法!!
使用JAVA编写以下程序:
1、请定义一个名为Card的扑克牌类,该类有两个private访问权限的字符串变量face和suit;分别描述一张牌的牌面值(如:A、K、Q、J、10、9、……3、2等)和花色(如:“黑桃”、“红桃”、“梅花”、“方块”);定义Card类中的public访问权限的构造方法,为类中的变量赋值;定义protected访问权限的方法getFace(),得到扑克牌的牌面值;定义protected访问权限的方法getSuit(),得到扑克牌的花色;定义方法toString(),返回表示扑克牌的花色和牌面值字符串(如“红桃A”、“梅花10”等)。
2、若应用程序的main方法中,定义字符串数组f和s;分别表示扑克牌的牌面值和花色;定义52个元素的Card类型数组deck,用来存放4个花色的52张牌。如下所示。
String f[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String s[] = {"黑桃","红桃","梅花","方块"};
Card deck = new Card[52];
(1)使用Card类的构造方法给deck数组的52张牌赋值,要求数组中先存放黑桃花色的A、2、3、……、K;然后是红桃花色的A、2、3、……、K;梅花花色的A、2、3、……、K;方块花色的A、2、3、……、K。请写出实现上述功能的程序段。
(2)请编写模拟洗牌的完整程序,即把数组deck中的扑克牌随机打乱存放顺序。
原题如下:
引用内容
private String face; // A, K, Q, J, 10, ... 2
private String suit; // Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face;
this.suit = suit;
}
protected String getFace() {
return face;
}
protected String getSuit() {
return suit;
}
public String toString() {
return suit + " " + face;
}
解题,完成程序解:
程序代码
package poker;
public class Card {
private String face; // A, K, Q, J, 10, ... 2
private String suit; // Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face;
this.suit = suit;
}
protected String getFace() {
return face;
}
protected String getSuit() {
return suit;
}
public String toString() {
return suit + " " + face;
}
public static void shuffle(Card[] deck, int startIndex, int size,
int splitIndex) { //shuffle洗牌
System.out.println("startIndex:" + startIndex);
if (splitIndex * 2 size) { //if rand is 30
Card.swap(deck, startIndex, splitIndex, size - splitIndex); //[52]、0、30、22
shuffle(deck, size - splitIndex, splitIndex, size - splitIndex); //[52]、22、30、22
} else if (splitIndex * 2 size) { //if rnd is 10
Card.swap(deck, startIndex, size - splitIndex, splitIndex); //[52]、0、42、10
shuffle(deck, startIndex, size - splitIndex, splitIndex); //[52]、0、42、10
} else {
Card.swap(deck, startIndex, splitIndex, splitIndex);
}
}
public static void swap(Card[] deck, int srcIndex, int dstIndex, int size) { //交换
//SRC是在本位置显示:source的缩写,源的意思 HREF是点击后连接的目标:HyperlinkReference,超链接引用
String face = "";
String suit = "";
for (int i = 0; i size; i++) {
face = deck[srcIndex + i].face;
suit = deck[srcIndex + i].suit;
deck[srcIndex + i].face = deck[dstIndex + i].face;
deck[srcIndex + i].suit = deck[dstIndex + i].suit;
deck[dstIndex + i].face = face;
deck[dstIndex + i].suit = suit;
}
}
/**
* @param args
*/
public static void main(String[] args) {
//第一问解答
Card[] deck = new Card[52];
String f[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K"};
String s[] = {"Spade", "Heart", "Club", "Diamond"};
for (int i = 0; i s.length; i++) {
for (int j = 0; j f.length; j++) {
deck[i * 13 + j] = new Card(s[i], f[j]); //依次存入数组之中,每13张一回合
}
}
//第二问解答
int rnd = 0;
int numOfShuffle = 10;
for (int i = 0; i numOfShuffle; i++) {
rnd = (int) Math.abs(Math.random() * 52); //Math.random()返回大于或等于 0.0 但小于 1.0 的伪随机 double 值。
Card.shuffle(deck, 0, deck.length, rnd);
}
// Test
/*
for (int i=0; ideck.length; i++)
{
System.out.println(deck[i]);
} */
}
}