//牌类
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.util.ArrayListimport java.util.Random
public class PokerDemo {
ArrayList<Card>poker = new ArrayList<Card>()
Player[] players
int n = 4
String[] name = {"Wang", "Zhang", "Li", "Yuan"}
public PokerDemo() {
players = new Player[n]
for(int i = 0i <players.lengthi++)
players[i] = new Player(name[i])
for(int i = 0i <4i++)
for(int j = 0j <13j++)
poker.add(new Card(i, j))
System.out.println("洗牌前: " + poker)
xipai()
System.out.println("洗牌后: " + poker)
fapai()
System.out.println("发牌后: ")
for(int i = 0i <players.lengthi++)
System.out.println(players[i])
}
public void xipai() {
Random r = new Random()
ArrayList<Card>tmp = new ArrayList<Card>()
while(!poker.isEmpty()) {
int index = r.nextInt(poker.size())
Card card = poker.get(index)
tmp.add(card)
poker.remove(index)
}
poker = new ArrayList<Card>(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 {
ArrayList<Card>cards = new ArrayList<Card>()
String name
public Player(String n) {
name = n
}
public void add(Card card) {
cards.add(card)
}
public String toString() {
return name + ":\t" + cards
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)