本案例使用了 hashMap 来存储手牌,其中 key 存储 0 到 1 作为索引,value 存储手牌。
下面来进行代码实现:
首先要准备牌:
//准备牌
String[] huase = {"♥","♠","♣","♦"};
//数字的顺序很重要,关系到后面放入哈希表时是否有序
String[] nums = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//准备存放纸牌的容器
HashMap<Integer,String> pai = new HashMap<>();
pai.put(0,"大王");
pai.put(1,"小王");
//使用循环嵌套遍历的方法来组装牌,并把牌和其对应的索引添加到容器中
for (int i = 0, j = 0; i < nums.length; i++, j++) {
for (int i1 = 0; i1 < huase.length; i1++) {
String pais = huase[i1] + nums[i];
//手牌对应的索引
int index = i * 4 + i1 + 2;
pai.put(index,pais);
}
}
把数组随机打乱
//把hashMap的索引加入到一个新建的数组中,然后把顺序进行随机打乱
ArrayList<Integer>index = new ArrayList<>();
for (int i = 0; i < 54; i++) {
index.add(i);
}
//洗牌
Collections.shuffle(index);
发牌
//创建四个数组来作为三个斗地主人员以及底牌
ArrayList<Integer> player1 = new ArrayList<>();
ArrayList<Integer> player2 = new ArrayList<>();
ArrayList<Integer> player3 = new ArrayList<>();
ArrayList<Integer> dipai = new ArrayList<>(); //底牌
for (int i = 0; i < index.size(); i++) {
if (i >= 51){
//发牌给底牌
dipai.add(index.get(i));
}
else if (i % 3 == 0){
//发牌给player1
player1.add(index.get(i));
}
else if (i % 3 == 1){
//发牌给player2
player2.add(index.get(i));
}
else if (i % 3 == 2){
//发牌给player3
player3.add(index.get(i));
}
}
由于重复的代码较多,创建一个方法来排序和看牌
//创建一个排序和看牌二和一的方法
private static void look_pai(String name,HashMap pai,ArrayList<Integer> index) {
//调整牌序
Collections.sort(index);
System.out.print(name + ": ");
for (Integer integer : index) {
System.out.print(" " + pai.get(integer));
}
//换行
System.out.println();
}
由于使用了随机打乱的方法,所以每次的结果都不相同。
结果1:
周润发: ♠2 ♦2 ♣A ♠K ♥Q ♦10 ♥9 ♠8 ♦8 ♥7 ♦7 ♥6 ♠6 ♥5 ♠5 ♣4 ♦3
周星驰: ♥A ♦A ♥K ♣Q ♠J ♣J ♥10 ♠10 ♣10 ♣9 ♦9 ♣8 ♠7 ♣5 ♠4 ♠3 ♣3
刘德华: 小王 ♥2 ♣2 ♠A ♣K ♦K ♠Q ♦Q ♦J ♠9 ♥8 ♣7 ♦6 ♦5 ♥4 ♦4 ♥3
底牌 : 大王 ♥J ♣6
结果2:
周润发: ♣2 ♦2 ♦A ♠K ♣K ♦K ♥Q ♠J ♦J ♦10 ♥9 ♦9 ♥8 ♦7 ♦6 ♥5 ♣3
周星驰: ♥2 ♠2 ♥A ♠A ♣A ♥K ♠Q ♣Q ♥10 ♠10 ♣8 ♦8 ♣6 ♣5 ♦5 ♣4 ♦4
刘德华: 大王 小王 ♥J ♣J ♣10 ♠9 ♣9 ♠8 ♠7 ♥6 ♠6 ♠5 ♥4 ♠4 ♥3 ♠3 ♦3
底牌 : ♦Q ♥7 ♣7
下面给出完整代码:
import java.util.*;
public class 斗地主_排序版 {
public static void main(String[] args) {
//准备牌
String[] huase = {"♥","♠","♣","♦"};
//数字的顺序很重要,关系到后面放入哈希表时是否有序
String[] nums = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//准备存放纸牌的容器
HashMap<Integer,String> pai = new HashMap<>();
pai.put(0,"大王");
pai.put(1,"小王");
//使用循环嵌套遍历的方法来组装牌,并把牌和其对应的索引添加到容器中
for (int i = 0, j = 0; i < nums.length; i++, j++) {
for (int i1 = 0; i1 < huase.length; i1++) {
String pais = huase[i1] + nums[i];
//手牌对应的索引
int index = i * 4 + i1 + 2;
pai.put(index,pais);
}
}
//把hashMap的索引加入到一个数组里,然后把顺序进行随机打乱
ArrayList<Integer>index = new ArrayList<>();
for (int i = 0; i < 54; i++) {
index.add(i);
}
//洗牌
Collections.shuffle(index);
//发牌
//创建四个数组来作为三个斗地主人员以及底牌
ArrayList<Integer> player1 = new ArrayList<>();
ArrayList<Integer> player2 = new ArrayList<>();
ArrayList<Integer> player3 = new ArrayList<>();
ArrayList<Integer> dipai = new ArrayList<>(); //底牌
for (int i = 0; i < index.size(); i++) {
if (i >= 51){
//发牌给底牌
dipai.add(index.get(i));
}
else if (i % 3 == 0){
//发牌给player1
player1.add(index.get(i));
}
else if (i % 3 == 1){
//发牌给player2
player2.add(index.get(i));
}
else if (i % 3 == 2){
//发牌给player3
player3.add(index.get(i));
}
}
//看牌
look_pai("周润发", pai, player1);
look_pai("周星驰", pai, player2);
look_pai("刘德华", pai, player3);
look_pai(" 底牌 ", pai, dipai);
}
//构造一个排序和看牌二和一的方法
private static void look_pai(String name,HashMap pai,ArrayList<Integer> index) {
//调整牌序
Collections.sort(index);
System.out.print(name + ": ");
for (Integer integer : index) {
System.out.print(" " + pai.get(integer));
}
//换行
System.out.println();
}
}