编写一个自动发牌程序,模拟三人斗地主的摸牌场景。首先要给出提示,谁首先开始摸牌,并且摸牌要和现实摸牌一样,三人循环摸牌,最后还要剩余三张底牌,同时给出地主牌,摸到地主牌的玩家拥有三张底牌。三张底牌三人都可以看到。当三张底牌派发给地主后提示玩家摸牌结束
实现思路
(1)首先将一副牌的四种花色和对应的牌面值随机组合放进Set集合,因为Set集合是非重复集合,所以无需考虑重复的问题,另外,因为每个牌面值出现的次数只能是四次,所以,当该牌面值出现了四次以后,将该牌面删除。
(2)获取洗牌结束的牌组(链表,用Set集合作为初始化数据集),随机额抽取三张牌,作为底牌,不对玩家展示,并从剩余的牌组中随机选取一个张牌,作为地主牌,对所有人展示但不移动其位置。
(3)顺序循环发牌,直到牌组没有牌为止,将底牌展示并发给地主。提示玩家发牌结束。
实现效果:
地主牌是:heart 9
派牌开始!
...
派牌结束!
底牌是:[spade 6, diamond 4, spade King]
地主是玩家A
玩家A的手牌是:
【 heart Queen, heart Jack, club Ace, heart 6, heart 9, heart 5,
club Queen, diamond King, diamond 6, diamond 8, club 3, club 6,
club 8, club Jack, heart 10, spade 8, spade 9, spade 6,
diamond 4, spade King 】
玩家B的手牌是:
【 diamond Jack, spade Queen, spade Ace, heart 7, heart 8, heart 2,
club 10, diamond 2, diamond 5, diamond 9, club 4, heart Ace,
club 9, spade 2, spade 3, spade 10, RED JOKER 】
玩家C的手牌是:
【 BLACK JOKER, diamond Queen, diamond 10, club King, heart 3, heart 4,
diamond 3, diamond 7, diamond Ace, club 2, club 5, club 7,
heart King, spade 4, spade 5, spade 7, spade Jack 】
Poker类部分
package com.qianfeng.test;
/**
*
* @author Lichangkun
*
*/
public class Poker {
private String sizes;
private String number;
public Poker() {
super();
}
public Poker(String sizes, String number) {
super();
this.sizes = sizes;
this.number = number;
}
public String getSizes() {
return sizes;
}
public void setSizes(int num) {
switch (num) {
case 1:
sizes="heart";
break;
case 2:
sizes="spade";
break;
case 3:
sizes="club";
break;
case 4:
sizes="diamond";
break;
}
}
public String getNumber() {
return number;
}
public void setNumber(int num) {
switch (num) {
case 1:
number = "Ace";
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
number = String.valueOf(num);
break;
case 11:
number = "Jack";
break;
case 12:
number = "Queen";
break;
case 13:
number = "King";
break;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((number == null) ? 0 : number.hashCode());
result = prime * result + ((sizes == null) ? 0 : sizes.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Poker other = (Poker) obj;
if (number == null) {
if (other.number != null)
return false;
} else if (!number.equals(other.number))
return false;
if (sizes == null) {
if (other.sizes != null)
return false;
} else if (!sizes.equals(other.sizes))
return false;
return true;
}
@Override
public String toString() {
return sizes + " " + number;
}
}
实现部分:
package com.qianfeng.test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
/**
*
* @author Lichangkun
*
*/
public class Test1 {
public static void main(String[] args) {
HashSet set = new HashSet<>();
ArrayList arrayList1 = new ArrayList<>();
ArrayList arrayList2 = new ArrayList<>();
Random random = new Random();
while (set.size() <= 51) {
int num1 = random.nextInt(13) + 1;
int num2 = random.nextInt(4) + 1;
Poker poker = new Poker();
poker.setNumber(num1);
poker.setSizes(num2);
if (numberCheck(arrayList1, num1) && sizesCheck(arrayList2, num2)) {
set.add(poker);
}
}
set.add(new Poker("BLACK", "JOKER"));
set.add(new Poker("RED", "JOKER"));
HashSet setHost = new HashSet<>();
ArrayList set1 = new ArrayList<>();
ArrayList set2 = new ArrayList<>();
ArrayList set3 = new ArrayList<>();
setHost.add(handPoker(set, 54));
setHost.add(handPoker(set, 53));
setHost.add(handPoker(set, 52));
Poker host = hostPoker(set, 51);
Iterator iterator = set.iterator();
int sum = 0, flag = 0;
while (iterator.hasNext()) {
Poker poker = (Poker) iterator.next();
sum++;
switch (sum) {
case 1:
set1.add(poker);
break;
case 2:
set2.add(poker);
break;
case 3:
set3.add(poker);
break;
}
if (poker.equals(host)) {
flag = sum;
}
if (sum == 3) {
sum = 0;
}
}
Iterator iterator2 = setHost.iterator();
while (iterator2.hasNext()) {
Poker poker = (Poker) iterator2.next();
switch (flag) {
case 1:
set1.add(poker);
break;
case 2:
set2.add(poker);
break;
case 3:
set3.add(poker);
break;
}
}
index(setHost, set1, set2, set3, host, flag);
}
public static Boolean numberCheck(ArrayList arrayList, int num) {
int count = 0;
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
Integer obj = (Integer) iterator.next();
if (obj == num) {
count++;
}
}
if (count <= 4) {
return true;
} else {
return false;
}
}
public static Boolean sizesCheck(ArrayList arrayList, int num) {
int count = 0;
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
Integer obj = (Integer) iterator.next();
if (obj == num) {
count++;
}
}
if (count <= 4) {
return true;
} else {
return false;
}
}
public static Poker handPoker(HashSet hashSet, int num) {
int count = 0;
Random random = new Random();
int x = random.nextInt(num);
Iterator iterator = hashSet.iterator();
while (iterator.hasNext()) {
Poker poker = (Poker) iterator.next();
count++;
if (count == x) {
hashSet.remove(poker);
return poker;
}
}
return null;
}
public static Poker hostPoker(HashSet hashSet, int num) {
int count = 0;
Random random = new Random();
int x = random.nextInt(num);
Iterator iterator = hashSet.iterator();
while (iterator.hasNext()) {
Poker poker = (Poker) iterator.next();
count++;
if (count == x) {
return poker;
}
}
return null;
}
public static void index(HashSet setHost, ArrayList set1, ArrayList set2,
ArrayList set3, Poker poker, int x) {
System.out.println("地主牌是:" + poker);
System.out.println("派牌开始!");
System.out.println("...");
System.out.println("派牌结束!");
if (x == 1) {
System.out.println("地主是:玩家A");
} else if (x == 2) {
System.out.println("地主是:玩家B");
} else {
System.out.println("地主是:玩家C");
}
int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
System.out.print("底牌是:[");
Iterator iterator1 = setHost.iterator();
while (iterator1.hasNext()) {
Poker poker2 = (Poker) iterator1.next();
x1++;
if (x1 == setHost.size()) {
System.out.print(poker2);
} else {
System.out.print(poker2 + ",");
}
}
System.out.println("]");
System.out.print("玩家A的手牌是:【");
Iterator iterator2 = set1.iterator();
while (iterator2.hasNext()) {
Poker poker2 = (Poker) iterator2.next();
x2++;
if (x2 == set1.size()) {
System.out.print(poker2);
} else {
System.out.print(poker2 + ",");
}
}
System.out.println("】");
System.out.print("玩家B的手牌是:【");
Iterator iterator3 = set2.iterator();
while (iterator3.hasNext()) {
Poker poker2 = (Poker) iterator3.next();
x3++;
if (x3 == set2.size()) {
System.out.print(poker2);
} else {
System.out.print(poker2 + ",");
}
}
System.out.println("】");
System.out.print("玩家C的手牌是:【");
Iterator iterator4 = set3.iterator();
while (iterator4.hasNext()) {
Poker poker2 = (Poker) iterator4.next();
x4++;
if (x4 == set3.size()) {
System.out.print(poker2);
} else {
System.out.print(poker2 + ",");
}
}
System.out.println("】");
}
}
输出实现:
地主牌是:spade King
派牌开始!
...
派牌结束!
地主是:玩家A
底牌是:[BLACK JOKER,heart 5,club 2]
玩家A的手牌是:【diamond 2,club 3,spade Jack,club 8,spade 3,diamond 5,club Queen,diamond Queen,club Ace,heart Jack,diamond 4,spade King,spade 5,diamond 3,diamond 10,diamond 8,heart Queen,BLACK JOKER,heart 5,club 2】
玩家B的手牌是:【heart 9,heart King,club 7,club 10,club King,spade 8,spade 10,club Jack,spade 4,spade 9,spade Ace,diamond Ace,diamond 9,club 4,spade 6,RED JOKER,spade Queen】
玩家C的手牌是:【spade 7,heart Ace,diamond Jack,diamond 7,heart 10,heart 8,club 6,diamond 6,heart 4,diamond King,club 5,heart 7,heart 3,club 9,heart 6,spade 2,heart 2】