//牌类
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"}
List<String>cards=new ArrayList<String>()//将一副牌54张放入待洗牌的集合中
for(int i=0i<types.lengthi++){
for(int j=0j<value.lengthj++){
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()
}
}
}
我在这里把所有的花色都加入一起洗牌了,楼主可根据需要进行特殊花色的洗牌!!你多执行几次就会发现每次洗牌后的牌的位置都不是一样的!!关键在一个洗牌动作的方法!!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)