package ArrayList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/*
* 模拟斗地主发牌程序
* 分析: 1.创建扑克牌
* 1.1先创建一个集合,用来存储1-10 ,J,Q,K,A
* 1.2创建一个用来存储"♠","♥","♦","♣"
* 1.3然后在创建一个集合用来存储扑克
* 1.4将花色和数字组合在一起
* 1.5最后添加大小王
* 2.发牌
* 3.发牌以及看牌
*
*
*/
public class Poker {
public static void main(String[] args){
//创建一个用来存储数字的集合
ArrayList<String> number = new ArrayList<String>();
//创建一个用来存储花色的集合
ArrayList<String> color = new ArrayList<String>();
//创建一个用来存储扑克的集合
Map<Integer,String> poker = new HashMap<Integer,String>();
//使用集合工具类向number集合中添加字符
Collections.addAll(number,"3","4","5","6","7","8","9","10","J","Q","K","A","2");
Collections.addAll(color, "♠","♥","♦","♣");
int i = 0;
//遍历集合,将
for(String n : number){
for(String c : color){
poker.put(i,c+n);
i++;
}
}
//添加大小王到集合
poker.put(i++,"小王");
poker.put(i++,"大王");
//System.out.println(poker);
//洗牌
ArrayList<Integer> r = new ArrayList<Integer>();
for (int j = 0; j < 54; j++) {
r.add(j);
}
Collections.shuffle(r);
//发牌
//创建4个集合,分别用来存储玩家1.玩家2,玩家3,和底牌
ArrayList<Integer> nplay1 = new ArrayList<Integer>();
ArrayList<Integer> nplay2 = new ArrayList<Integer>();
ArrayList<Integer> nplay3 = new ArrayList<Integer>();
ArrayList<Integer> nlastpoker = new ArrayList<Integer>();
for (int j = 0; j < r.size(); j++) {
if(j >= 51){
nlastpoker.add(r.get(j));
}else{
if(j%3==0){
nplay1.add(r.get(j));
}else if(j%3==1){
nplay2.add(r.get(j));
}else{
nplay3.add(r.get(j));
}
}
}
/*System.out.println(nplay1);
System.out.println(nplay2);
System.out.println(nplay3);
System.out.println(nlastpoker);*/
Collections.sort(nplay1);
Collections.sort(nplay2);
Collections.sort(nplay3);
Collections.sort(nlastpoker);
ArrayList<String> play1 = new ArrayList<String>();
ArrayList<String> play2 = new ArrayList<String>();
ArrayList<String> play3 = new ArrayList<String>();
ArrayList<String> lastpoker = new ArrayList<String>();
//通过数字获取想要的牌
for(Integer j : nplay1){
play1.add(poker.get(j));
}
for(Integer j : nplay2){
play2.add(poker.get(j));
}
for(Integer j : nplay3){
play3.add(poker.get(j));
}
for(Integer j : nlastpoker){
lastpoker.add(poker.get(j));
}
System.out.println(play1);
System.out.println(play2);
System.out.println(play3);
System.out.println(lastpoker);
}
}
|
|