JAVA扑克游戏控制流量思路
生成扑克数据以字节形式存放到MAP容器
public static void pai() {
int tmp = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
tmp = (byte) (i) << 4 | (byte) j;
cards.put(new Integer(i * 13 + j), new Byte((byte) tmp));
}
}
cards.put(new Integer(53), new Byte((byte) 0x4d));
cards.put(new Integer(54), new Byte((byte) 0x4e));
}
//从服务器获取扑克,获取的是字节形式的数据可减少流量消耗
public static void sendp(Map cards) {
int n = 0;
while (n < 8) {
Integer key = new Long(Math.round(Math.random() * 87) % 55).intValue();
if (cards.containsKey(key)) {
System.out.println(getPm(cards.get(key).toString()));
cards.remove(key);
++n;
}
}
}
//解析扑克
public static String getPm(String n) {
int psIndex;
long psNum;
String pm = null;
String[] ps = {“黑”, “红”, “樱”, “方”, “王”};
String[] psm = {“A”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”};
String p = Integer.toHexString(Integer.valueOf(n));
System.out.println§;
if (p.length() == 1) {
psIndex = 0;
psNum = Long.parseLong(p, 16);
pm = ps[psIndex] + psm[(int)psNum];
} else {
psIndex = Integer.valueOf(String.valueOf(p.charAt(0)));
psNum = Long.parseLong(String.valueOf(p.charAt(1)), 16);
if (psIndex == 4) {
if (psNum == 0) {
pm = “大王”;
} else {
pm = “小王”;
}
} else {
pm = ps[psIndex] + psm[(int)psNum];
}
}
return pm;
}
}
主方法测式
private static HashMap<Integer, Byte> cards = new HashMap<>();
public static void main(String[] args) throws UnsupportedEncodingException {
pai();
System.out.println(“总牌数:” + cards.size());
for (int i = 0; i < 4; i++) {
System.out.println(“玩家”+i+1);
sendp(cards);
System.out.println(“剩余牌数”+cards.size());
}
}