花色:♠ ♥ ♦ ♣
* 牌号:A 2 3 4 5 6 7 8 9 10 J Q K
* 大王、小王
*
* 1.生成一副牌
* 2.然后发牌
PokerCard类
package cn.ds.baidu.demo13.de03;
/*
* 一张扑克牌类
* 花色:♠ ♥ ◆ ♣
* 牌号:A 2 3 4 5 6 7 8 9 10 J Q K
* 大王,小王
*
* */
public class PokerCard {
private String color;
private String CardNum;
@Override
public String toString() {
return color + "" +CardNum;
}
public PokerCard(String color, String cardNumber) {
this.color = color;
this.CardNum = cardNumber;
}
public PokerCard() {
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getCardNumber() {
return CardNum;
}
public void setCardNumber(String cardNumber) {
this.CardNum = cardNumber;
}
}
Poker类
package cn.ds.baidu.demo13.de03;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/*
* 扑克
* 花色:♠ ♥ ◆ ♣
* 牌号:A 2 3 4 5 6 7 8 9 10 J Q K
* 大王,小王
*
* 1.生成一副牌
* */
public class Poker {
//一副牌集合
private List<PokerCard> pokerList;
private String[]colors={"♠","♥","♣" ,"◆" };
//无参构造器
public Poker() {
pokerList = new ArrayList<>();
//A
for (int i = 0; i < colors.length; i++) {//四种花色
PokerCard pc = new PokerCard( colors[i],"A");
pokerList.add(pc);
}
//2-10
for (int i = 2; i <= 10 ; i++) {
for (int j = 0; j < colors.length; j++) {
PokerCard pc = new PokerCard(colors[j], i + " ");
pokerList.add(pc);
}
}
//J
for (int i = 0; i < colors.length; i++) {
PokerCard pc = new PokerCard(colors[i], "J");
pokerList.add(pc);
}
//Q
for (int i = 0; i < colors.length; i++) {
PokerCard pc = new PokerCard(colors[i], "Q");
pokerList.add(pc);
}
//K
for (int i = 0; i < colors.length; i++) {
PokerCard pc = new PokerCard(colors[i], "K");
pokerList.add(pc);
}
//大王,小王
PokerCard pc1 = new PokerCard("", "大王");
pokerList.add(pc1);
PokerCard pc2 = new PokerCard("", "小王");
pokerList.add(pc2);
}
//洗牌
public void xp(){
//获取牌
List<PokerCard> pkcList = this.getPkcList();
Object[] total = pkcList.toArray();
//洗牌是随机的
Random r = new Random();
for (int j = 0; j < total.length; j++) {
int sj = r.nextInt(54);
System.out.print(total[sj]+" ");
}
System.out.println();
}
//发牌
public void deal(){
//获取牌
List<PokerCard> pokerList = this.getPkcList();
Object[] total = pokerList.toArray();
//存储三个玩家的牌
Object[][] plays = new Object[3][17];
//存储当前剩余牌的数量
int leftNum = 54;
//发牌是随机的
Random r = new Random();
//分别把牌分给3个人,留三张底牌,也就是说一个人能分到17张牌
for (int i = 0; i < 17; i++) {
//为每个人发牌
for (int j = 0; j < plays.length; j++) {
//System.out.println(j);
int ranNumber = r.nextInt(leftNum);
//发牌
plays[j][i] = total[ranNumber];
//移动发的牌
total[ranNumber] = total[leftNum - 1];
leftNum--;
}
}
for (int i = 0; i < plays.length; i++) {
System.out.print("玩家:"+i+"的牌");
for (int j = 0; j < plays[i].length; j++) {
System.out.print(plays[i][j]);
}
System.out.println();
}
System.out.print("底牌:");
for(int i = 0;i < 3;i++){
System.out.print(total[i]);
}
System.out.println();
}
public List<PokerCard> getPkcList() {
return pokerList;
}
public void setPkcList(List<PokerCard> pkcList) {
this.pokerList = pkcList;
}
public Poker(List<PokerCard> pkcList) {
this.pokerList = pkcList;
}
/*
* main方法
* */
public static void main(String[] args) {
Poker poker = new Poker();
System.out.println(poker.getPkcList());
poker.xp();
poker.deal();
}
}
[♠A, ♥A, ♣A, ◆A, ♠2 , ♥2 , ♣2 , ◆2 , ♠3 , ♥3 , ♣3 , ◆3 , ♠4 , ♥4 , ♣4 , ◆4 , ♠5 , ♥5 , ♣5 , ◆5 , ♠6 , ♥6 , ♣6 , ◆6 , ♠7 , ♥7 , ♣7 , ◆7 , ♠8 , ♥8 , ♣8 , ◆8 , ♠9 , ♥9 , ♣9 , ◆9 , ♠10 , ♥10 , ♣10 , ◆10 , ♠J, ♥J, ♣J, ◆J, ♠Q, ♥Q, ♣Q, ◆Q, ♠K, ♥K, ♣K, ◆K, 大王, 小王]
◆5 ♥8 ♠10 ♠7 ◆3 ♣K ♣7 ◆2 ♥9 ♣9 ◆Q ♥9 ♠9 ♣10 ♠2 ♣7 ♥J ♠5 ♣K ♠9 ♥10 ♥7 ♥2 ♠2 ◆2 ♣A ♠6 ◆A ◆A ♥Q ♠2 ♣4 ♠3 ◆J ♣3 ♣6 ♣5 ◆6 ◆8 ♥J ♥4 ♥8 ♥A ♣Q ♥9 ♥10 ♠A ◆A ♠A ◆6 ◆5 ◆4 ◆A ◆7
玩家:0的牌♥K♥8 ♣4 ♣A♣J◆8 ♣10 ♠5 ♠2 ♥A♥9 ◆4 ♣3 ◆3 ◆Q♣Q♥10
玩家:1的牌♣7 ♠8 ◆A◆J小王♣6 ♠10 ♣8 ♠9 ♥7 ◆7 ◆9 ♠K◆10 ♥4 ◆6 ♣9
玩家:2的牌♠7 ♠A♣2 ♠3 ♥2 ♠Q♠J♠4 ◆K♥5 ◆5 ♥J♥Q♣K♥3 ♣5 ♠6
底牌:◆2 大王♥6