案例实现斗地过程中的洗牌发牌和看牌。
package java20;
import java.util.ArrayList;
import java.util.Collections;
public class demo {
public static void main(String[] args) {
//创建牌盒,即定义集合对象
ArrayList<String> array = new ArrayList<String>();
//往牌盒里面装牌
//定义花色组
String[] colors = {"♥","♠","♣","♦"};
//定义数组
String[] numbers = {"2","3","4","5","6","7","8","9","10","J","K","A"};
for (String color:colors){
for(String number:numbers){
array.add(color+number);
}
}
array.add("小王");
array.add("大王");
// System.out.println(array);
//洗牌,Collections中的shuffle方法实现
Collections.shuffle(array);
// System.out.println(array);
//发牌,定义三个集合对应三个玩家,定义一个集合对应三张底牌
ArrayList<String> player1 = new ArrayList<String>();
ArrayList<String> player2 = new ArrayList<String>();
ArrayList<String> player3 = new ArrayList<String>();
ArrayList<String> dipai = new ArrayList<String>();
for(int i=0;i<array.size();i++){
String poker = array.get(i);
if(i>=array.size()-3){
dipai.add(poker);
}else if(i%3==0){
player1.add(poker);
}else if (i%3==1){
player2.add(poker);
}else if (i%3==2){
player3.add(poker);
}
}
//看牌
lookPoker("玩家1",player1);
lookPoker("玩家2",player2);
lookPoker("玩家3",player3);
lookPoker("底牌",dipai);
}
//看牌的方法
public static void lookPoker(String name,ArrayList<String> array){
System.out.print(name+"的牌是:");
for(String poker:array){
System.out.print(poker+" ");
}
System.out.println();
}
}
添加功能对玩家的牌进行排序
思路:
- 创建HashMap集合存储序号和牌,一个编号对应一张牌
- 创建ArrayList集合存储编号
- 创建花色数组和数字数组用于组合每一张牌,并按顺序往HashMap存储编号和牌,同时ArrayList存入编号
- 对ArrayList进行随机排序
- 创建TreeSet集合存储玩家手牌及底牌,遍历ArrayList的顺序对玩家进行发牌,然后TreeSet会对发到的编号进行排序,根据编号找到HashMap中对应的牌实现手牌排序。
package java20;
import Java18.Fan.fanxing1.Teacher;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class demo2 {
public static void main(String[] args) {
//键是编号,值是牌
HashMap<Integer,String> hm = new HashMap<Integer,String>();
//存储编号
ArrayList<Integer> array = new ArrayList<Integer>();
//创建花色数组
String[] colors = {"♥","♠","♣","♦"};
//创建点数数组
String[] numbers = {"3","4","5","6","7","8","9","10","J","K","A","2"};
//从0 开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号
int index = 0;
for (String number:numbers){
for (String color:colors){
hm.put(index,color+number);
array.add(index);
index++;
}
}
hm.put(index,"小王");
array.add(index);
index++;
hm.put(index,"大王");
array.add(index);
//洗牌,洗的是编号
Collections.shuffle(array);
//发牌,发的也是编号;创建TreeSet集合接受保证排序
TreeSet<Integer> player1 = new TreeSet<Integer>();
TreeSet<Integer> player2 = new TreeSet<Integer>();
TreeSet<Integer> player3 = new TreeSet<Integer>();
TreeSet<Integer> dipai = new TreeSet<Integer>();
for (int i=0;i<array.size();i++) {
Integer x = array.get(i);
if (i >= array.size() - 3) {
dipai.add(x);
} else if (i % 3 == 0){
player1.add(x);
}else if (i%3==1){
player2.add(x);
}else if (i%3==2){
player3.add(x);
}
}
//看牌
lookPoker("玩家1",player1,hm);
lookPoker("玩家2",player2,hm);
lookPoker("玩家3",player3,hm);
lookPoker("底牌",dipai,hm);
}
//定义方法看牌
public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){
System.out.print(name+"的牌是:");
for (Integer key:ts){
String poker = hm.get(key);
System.out.print(poker+" ");
}
System.out.println();
}
}