- 制作扑克牌盒:
定义一个集合存储扑克牌:
ArrayList<String> array=new ArrayList<>();
定义一个colors数组,存储扑克中的四种花色:
String [] colors={"♥","♠","♦","♣"};
定义一个number数组,存储扑克牌2~A的字符
String[] number={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
向牌盒中添加扑克牌:
for (String c:colors){
for (String n:number) {
array.add(c+n);
}
}
array.add("小王");
array.add("大王");
打乱牌序
Collections.shuffle(array);
- 定义四个集合,分别存储三个玩家的牌和底牌
ArrayList<String> w1=new ArrayList<>();
ArrayList<String> w2=new ArrayList<>();
ArrayList<String> w3=new ArrayList<>();
ArrayList<String> dp=new ArrayList<>();
- 发牌
for (int i = 0; i <array.size() ; i++) {
String poker = array.get(i);
if(i>array.size()-3){
dp.add(poker);
}else if(i%3==0){
w1.add(poker);
}else if(i%3==1){
w2.add(poker);
}else if (i%3==2){
w3.add(poker);
}
}
4.定义一个方法,用于看牌
public static void look(String name,ArrayList<String> aa){
System.out.println(name+"的牌是:");
for (String p:aa){
System.out.print(p+" ");
}
System.out.println();
}
5.全部代码
import java.util.ArrayList;
import java.util.Collections;
public class DO {
public static void main(String[] args) {
ArrayList<String> array=new ArrayList<>();
String [] colors={"♥","♠","♦","♣"};
String[] number={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for (String c:colors){
for (String n:number) {
array.add(c+n);
}
}
array.add("小王");
array.add("大王");
//System.out.println(array);
Collections.shuffle(array);
// System.out.println(array);
System.out.println("斗地主");
ArrayList<String> w1=new ArrayList<>();
ArrayList<String> w2=new ArrayList<>();
ArrayList<String> w3=new ArrayList<>();
ArrayList<String> dp=new ArrayList<>();
for (int i = 0; i <array.size() ; i++) {
String poker = array.get(i);
if(i>=array.size()-3){
dp.add(poker);
}else if(i%3==0){
w1.add(poker);
}else if(i%3==1){
w2.add(poker);
}else if (i%3==2){
w3.add(poker);
}
}
look("张三",w1);
look("李四",w2);
look("王五",w3);
look("底牌",dp);
}
public static void look(String name,ArrayList<String> aa){
System.out.println(name+"的牌是:");
for (String p:aa){
System.out.print(p+" ");
}
System.out.println();
}
}
- 执行结果
- 增强版代码:
HashMap<Integer,String> hm=new HashMap<>();
ArrayList<Integer> array=new ArrayList<>();
String [] colors={"♥","♠","♦","♣"};
String[] number={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int index=0;
for (String c:colors){
for (String n:number) {
hm.put(index,c+n );
array.add(index);
index++;
}
}
hm.put(index,"小王");
array.add(index);
index++;
hm.put(index,"大王");
array.add(index);
index++;
Collections.shuffle(array);
TreeSet<Integer> lqx=new TreeSet<>();
TreeSet<Integer> ly=new TreeSet<>();
TreeSet<Integer> zyz=new TreeSet<>();
TreeSet<Integer> dp=new TreeSet<>();
for (int i = 0; i <array.size() ; i++) {
int x = array.get(i);
if(i>=array.size()-3){
dp.add(x);
}else if(i%3==0){
lqx.add(x);
}else if(i%3==1){
ly.add(x);
}else if (i%3==2){
zyz.add(x);
}
}
System.out.println("斗地主");
lookPoker("刘燕",ly,hm);
lookPoker("林青霞",lqx,hm);
lookPoker("朱元璋",zyz,hm);
lookPoker("底牌",dp,hm);
}
public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){
System.out.println(name +"的牌是:");
for (Integer key:ts){
String s = hm.get(key);
System.out.print(s+" ");
}
System.out.println();
}