本帖最后由 黑夜中那颗星 于 2015-12-30 14:07 编辑
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.TreeMap;
- public class Ddz {
- static TreeMap<Integer,String> t1 = new TreeMap<Integer,String>(new Copara()); //玩家一
- static TreeMap<Integer,String> t2 = new TreeMap<Integer,String>(new Copara()); //玩家二
- static TreeMap<Integer,String> t3 = new TreeMap<Integer,String>(new Copara()); //玩家三
- static ArrayList<String> al = new ArrayList<String>(); //存单个牌的容器
- static ArrayList<Pk> al1 = new ArrayList<Pk>(); //存一副牌的容器
- public static void main(String[] args) {
- addBoard(); //所有牌,没色的
- addColour();//添加所有牌,有色的
- Collections.shuffle(al1); //洗牌
- Fp(); //发牌
- showt(t1,"玩家一");
- showt(t2,"玩家二");
- showt(t3,"玩家三");
- System.out.print("底牌:");
- for(int x = al1.size()-3;x<al1.size();x++){
- Pk p = al1.get(x);
- System.out.print(x==al1.size()-1?p.getName():p.getName()+",");
- }
- }
- private static void Fp() { //发牌
- for(int x = 0;x<al1.size()-3;){
- Pk p = al1.get(x);
- Pk p1 = al1.get(x+1);
- Pk p2 = al1.get(x+2);
- t1.put(p.getId(), p.getName());
- t2.put(p1.getId(), p1.getName());
- t3.put(p2.getId(), p2.getName());
- x+=3;
- }
- }
- private static void showt(TreeMap<Integer, String> t,String name) { //看牌
- StringBuilder sb = new StringBuilder();
- for(Iterator<Map.Entry<Integer,String>> it = t.entrySet().iterator();it.hasNext();){
- Map.Entry<Integer, String> map = it.next();
- sb.append(map.getValue()+",");
- }
- System.out.println(name+":"+sb.toString().substring(0,sb.length()-1));
- }
- private static void addColour() { //添加所有牌
- int count = 1;
- String[] string = {"红桃","黑桃","方块","梅花"};
- for(String str:al){
- for(int x = 0;x<4;x++){
- al1.add(new Pk(count,string[x]+str)); //给每个牌设定一个id,3是最小的所以他的id是1,然后依次升序
- }
- count++;
- }
- al1.add(new Pk(14,"小王"));
- al1.add(new Pk(15,"大王"));
- }
- private static void addBoard() { //添加单个有色的牌,按照牌的大小顺序添加,除大小王
- for(int x = 3;x<=10;x++){
- al.add(x+"");
- }
- al.add("J");
- al.add("Q");
- al.add("K");
- al.add("A");
- al.add("2");
- }
- }
- class Pk{ //把牌封装成一个对象,属性有id和name,id是为了方便牌的排序
- private int id;
- private String name;
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + id;
- result = prime * result + ((name == null) ? 0 : name.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;
- Pk other = (Pk) obj;
- if (id != other.id)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- public int getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public Pk(int id, String name) {
- super();
- this.id = id;
- this.name = name;
- }
- }
- class Copara implements Comparator<Integer>{ //定义一个比较器,因为TreeMap键是唯一性的,所以要稍作修改,不然的话一个牌添加不了四张
- public int compare(Integer o1, Integer o2) {
- int num = o2.compareTo(o1);
- if(num==0)
- return 1;
- return num;
- }
- }
复制代码 |
|