一.存牌
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","q","k","a","2"};
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);
index++;
二.洗牌
Collections.shuffle(array);
三.发牌
TreeSet<Integer> ydjSet= new TreeSet<Integer>();
TreeSet<Integer> hyjSet= new TreeSet<Integer>();
TreeSet<Integer> bzhSet= new TreeSet<Integer>();
TreeSet<Integer> dpSet= new TreeSet<Integer>();
for (int i =0; i<array.size();i++){
int x = array.get(i);
if (i>=array.get(i)){
dpSet.add(x);
}else if (i%3 == 0){
ydjSet.add(x);
}else if (i%3==1){
hyjSet.add(x);
}else if (i%3==2){
bzhSet.add(x);
}
}
//调用看牌方法
lookpoker("妖刀姬",ydjSet,hm);
lookpoker("辉夜姬",hyjSet,hm);
lookpoker("不知火",bzhSet,hm);
lookpoker("底牌",dpSet,hm);
}
//看牌方法
public static void lookpoker(String name, TreeSet<Integer> ts ,HashMap<Integer,String>hm){
System.out.println(name + "的牌是:");
for (Integer key :ts){
String poker = hm.get(key);
System.out.println(poker+" ");
}
System.out.println();
}
完整代码
import java.util.*;
public class PokerDemo {
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","q","k","a","2"};
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);
index++;
//洗牌
Collections.shuffle(array);
//发牌
TreeSet<Integer> ydjSet= new TreeSet<Integer>();
TreeSet<Integer> hyjSet= new TreeSet<Integer>();
TreeSet<Integer> bzhSet= new TreeSet<Integer>();
TreeSet<Integer> dpSet= new TreeSet<Integer>();
for (int i =0; i<array.size();i++){
int x = array.get(i);
if (i>=array.get(i)){
dpSet.add(x);
}else if (i%3 == 0){
ydjSet.add(x);
}else if (i%3==1){
hyjSet.add(x);
}else if (i%3==2){
bzhSet.add(x);
}
}
//调用看牌方法
lookpoker("妖刀姬",ydjSet,hm);
lookpoker("辉夜姬",hyjSet,hm);
lookpoker("不知火",bzhSet,hm);
lookpoker("底牌",dpSet,hm);
}
//看牌方法
public static void lookpoker(String name, TreeSet<Integer> ts ,HashMap<Integer,String>hm){
System.out.println(name + "的牌是:");
for (Integer key :ts){
String poker = hm.get(key);
System.out.println(poker+" ");
}
System.out.println();
}
}