首页 > 其他 > 详细

斗地主案例

时间:2019-12-27 23:30:43      阅读:123      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

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

斗地主案例

原文:https://www.cnblogs.com/timon777/p/12110011.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!