斗地主案例
Posted timon777
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了斗地主案例相关的知识,希望对你有一定的参考价值。
package com.ahclg.casePoker;
import java.awt.font.FontRenderContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class PorkerDemo {
public static void main(String[] args) {
//创建HashMap,键是编号,值是牌
HashMap<Integer, String> hm = new HashMap<>();
//创建ArrayList,存储编号
ArrayList<Integer> al = new ArrayList<>();
//定义花色
String[] colors = {"♥", "♠", "♣", "♦"};
//定义点数
String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "k", "A", "2",};
//从0开始往HashMap里面存储编号,并存储对应的牌。同时往al里面存储编号
int index = 0;
for (String color : colors) {
for (String number : numbers) {
hm.put(index, color + number);
al.add(index);
index++;
}
}
hm.put(index, "小王");
al.add(index);
index++;
hm.put(index,"大王");
al.add(index);
//洗牌
Collections.shuffle(al);
//发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收)
TreeSet<Integer> MaYunSet = new TreeSet<>();
TreeSet<Integer> LiYanhongSet = new TreeSet<>();
TreeSet<Integer> MaHuatengSet = new TreeSet<>();
TreeSet<Integer> alDiPaiSet = new TreeSet<>();
for (int i = 0; i < al.size(); i++) {
Integer x = al.get(i);//al集合内存储的是0,1,2,3...53与x是一一对应的
if (i >= al.size() - 3) {
alDiPaiSet.add(x);
} else if (i % 3 == 0) {
MaYunSet.add(x);
} else if (i % 3 == 1) {
LiYanhongSet.add(x);
} else if (i % 3 == 2) {
MaHuatengSet.add(x);
}
}
lookPoker("马云:", MaYunSet, hm);
lookPoker("李彦宏:", LiYanhongSet,hm);
lookPoker("马化腾:", MaHuatengSet,hm);
lookPoker("底牌:", alDiPaiSet,hm);
}
//看牌的方法
public static void lookPoker(String name, TreeSet<Integer> treeSet,HashMap<Integer, String> hm) {
System.out.print(name);
for (Integer key : treeSet){
String poker = hm.get(key);
System.out.print(poker + " ");
}
System.out.println();
}
}
结果:
马云:♥7 ♥10 ♥A ♥2 ♠4 ♠9 ♠k ♠2 ♣3 ♣7 ♣9 ♣A ♦3 ♦5 ♦10 ♦Q ♦2
李彦宏:♥5 ♥6 ♥8 ♥Q ♠5 ♠6 ♠8 ♠A ♣k ♣2 ♦4 ♦6 ♦7 ♦8 ♦k ♦A 小王
马化腾:♥3 ♥J ♥k ♠3 ♠7 ♠10 ♠J ♠Q ♣4 ♣5 ♣6 ♣8 ♣10 ♣J ♦9 ♦J 大王
底牌:♥4 ♥9 ♣Q
以上是关于斗地主案例的主要内容,如果未能解决你的问题,请参考以下文章