public static void main(String[] args) {
String[] pai = new String[] { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
// String[] hs = new String[] { "♥", "♠", "♦", "♣" };
HashMap hj = new HashMap<>();
HashMap hy = new HashMap<>();
HashMap hb = new HashMap<>();
List<String> a = new ArrayList<String>();
for (int i = 0; i < pai.length; i++) {
a.add(pai[i]);
a.add(pai[i]);
a.add(pai[i]);
a.add(pai[i]);
}
a.add("大王");
a.add("小王");
Random rand = new Random();
String temp = null;
for (int k = 0; k < 100; k++) {
int p = a.size();
int l = rand.nextInt(p);
int m = rand.nextInt(p);
if (l == m)
continue;
{
temp = a.get(l);
a.set(l, a.get(m));
a.set(m, temp);
}
}
// Collections.shuffle(a);
List<String> j = new ArrayList<String>();
List<String> y = new ArrayList<String>();
List<String> b = new ArrayList<String>();
List<String> d = new ArrayList<String>();
for (int i = 0; i < a.size(); i++) {
if (i >= a.size() - 3) {
d.add(a.get(i));
} else if (i % 3 == 0) {
j.add(a.get(i));
} else if (i % 3 == 1) {
y.add(a.get(i));
} else if (i % 3 == 2) {
b.add(a.get(i));
}
}
System.out.println(d);
System.out.println("甲" + j);
System.out.println("乙" + y);
System.out.println("丙" + b);
for (String str : j) {
if (hj.containsKey(str)) {
hj.put(str, hj.get(str) + 1);
} else
hj.put(str, 1);
}
System.out.println("j" + hj);
for (String str : y) {
if (hy.containsKey(str)) {
hy.put(str, hy.get(str) + 1);
} else
hy.put(str, 1);
}
System.out.println("y" + hy);
for (String str : b) {
if (hb.containsKey(str)) {
hb.put(str, hb.get(str) + 1);
} else
hb.put(str, 1);
}
System.out.println("b" + hb);
最好在我已经写出的代码上加代码,要显示出甲、乙、丙手牌的对子、三带、炸弹之类的牌型,求具体的代码!!!!!!!
@Test
public void testFunction4(){
/*数字*/
String[] pai = new String[] { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
/*花色*/
String[] hs = new String[] { "♥", "♠", "♦", "♣" };
HashMap hj = new HashMap<>();
HashMap hy = new HashMap<>();
HashMap hb = new HashMap<>();
List a = new ArrayList();
for (int i = 0; i < pai.length; i++) {
a.add(hs[0]+pai[i]);
a.add(hs[1]+pai[i]);
a.add(hs[2]+pai[i]);
a.add(hs[3]+pai[i]);
}
a.add("大王");
a.add("小王");
Random rand = new Random();
String temp = null;
for (int k = 0; k < 100; k++) {
int p = a.size();
int l = rand.nextInt(p);
int m = rand.nextInt(p);
if (l == m) {
continue;
}
{
temp = a.get(l);
a.set(l, a.get(m));
a.set(m, temp);
}
}
// Collections.shuffle(a);
List<String> j = new ArrayList<String>();
List<String> y = new ArrayList<String>();
List<String> b = new ArrayList<String>();
List<String> d = new ArrayList<String>();
for (int i = 0; i < a.size(); i++) {
if (i >= a.size() - 3) {
d.add(a.get(i));
} else if (i % 3 == 0) {
j.add(a.get(i));
} else if (i % 3 == 1) {
y.add(a.get(i));
} else if (i % 3 == 2) {
b.add(a.get(i));
}
}
System.out.println(d);
System.out.println("甲" + j.toString());
/*获得有三个的*/
printPai(j,3,null);
System.out.println("乙" + y.toString());
System.out.println("丙" + b.toString());
for (String str : j) {
if (hj.containsKey(str)) {
hj.put(str, hj.get(str) + 1);
} else {
hj.put(str, 1);
}
}
System.out.println("j" + hj);
for (String str : y) {
if (hy.containsKey(str)) {
hy.put(str, hy.get(str) + 1);
} else {
hy.put(str, 1);
}
}
System.out.println("y" + hy);
for (String str : b) {
if (hb.containsKey(str)) {
hb.put(str, hb.get(str) + 1);
} else {
hb.put(str, 1);
}
}
System.out.println("b" + hb);
}
public void printPai(List<String> pai, Integer count ,String name) {
if (count == null) {
/*去掉花色,输出手里面的排*/
pai.forEach(p ->p=p.substring(1,2));
System.out.println(pai.toString());
}else if (name !=null && name !=""){
/*name为大王的时候输出大王,按名字查找,忽略大小写,忽略花色*/
pai.stream().filter(p -> p.substring(1,2).equalsIgnoreCase(name)).collect(toList());
System.out.println(name+":"+ pai.size()+"个");
} else if (count!=null && name ==null || name !="" ){
/*查找指定数量的pai,2对子,3三个,4炸等,忽略花色*/
HashMap<String, Integer> paiCount = new HashMap<>();
pai.forEach(p->{
String p2 = p.substring(1,2);
if (paiCount.containsKey(p2)) {
paiCount.put(p2, paiCount.get(p2)+1);
} else {
paiCount.put(p2, 1);
}
});
/*输出指定个数的*/
ArrayList<String> resultPai = new ArrayList<String>();
paiCount.forEach((k,v)->{
if (v.equals(count)) {
resultPai.add(k);
}
});
System.out.println(count+"个的牌有:"+resultPai.toString());
} else {
/*如果count是2输出手里面的对子,3数据三个,4输出炸弹,配合name,如name=大王,输出手里面是否有大王*/
/*由于不能使用int类型变量来自增统计个数,就使用集合来统计个数一样的*/
ArrayList list = new ArrayList();
pai.stream().forEach(p ->{
if (p.substring(1, 2).equalsIgnoreCase(name)) {
list.add(p);
}
});
System.out.println("是否有"+count+"个"+name+":"+ (list.size()>=count?"是":"否"));
}
}
printPai是我写的方法,具体注释在里面,输出想要的,具体可以更具你的需求对代买进行必要的修改,我只是按照我的想法写了一个,下面给出一个输出的结果:
[♠A, ♦5, ♥9]
甲[♣3, ♥5, ♦6, ♥7, ♦9, ♠2, ♦3, ♠Q, ♣5, ♠9, ♥10, ♦10, ♣K, ♠6, 小王, ♣9, ♥J]
3个的牌有:[9]
乙[♦2, ♣10, ♠4, ♥8, ♥4, ♣J, ♣4, ♥6, ♦J, ♠10, ♣A, ♥2, ♥A, ♦7, 大王, ♦8, ♣2]
丙[♠7, ♣7, ♠5, ♠3, ♦Q, ♣6, ♦4, ♦A, ♥K, ♣Q, ♣8, ♠K, ♥3, ♦K, ♥Q, ♠J, ♠8]
j{♥J=1, ♦3=1, ♣K=1, ♠6=1, ♣9=1, ♣3=1, ♥5=1, ♦6=1, ♥10=1, ♠Q=1, ♦10=1, ♥7=1, ♠2=1, ♣5=1, ♦9=1, 小王=1, ♠9=1}
y{♦J=1, ♣J=1, ♦2=1, ♥2=1, ♥4=1, ♣2=1, ♠10=1, 大王=1, ♠4=1, ♣10=1, ♣4=1, ♥6=1, ♦7=1, ♦8=1, ♥8=1, ♥A=1, ♣A=1}
b{♦K=1, ♥K=1, ♥Q=1, ♦4=1, ♣Q=1, ♥3=1, ♠J=1, ♦Q=1, ♠K=1, ♣7=1, ♠5=1, ♣8=1, ♠7=1, ♠3=1, ♣6=1, ♠8=1, ♦A=1}
package com.paly;
import java.util.ArrayList;
import java.util.Collections;
public class SetPaly {
public static void main(String[] args) {
// 花色
String[] color = { "♠","♥","♣","♦" };
// 数字
String[] num = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
// 牌盒
ArrayList<String> box = new ArrayList<>();
for (int i = 0; i < color.length; i++) {
for (int j = 0; j < num.length; j++) {
box.add(color[i] + num[j]);
}
}
box.add("大王");
box.add("小王");
// 洗牌
Collections.shuffle(box);
//System.out.println(box);
// 找三个人来打牌
ArrayList<Object> 小波 = new ArrayList<>();
ArrayList<Object> 小苍 = new ArrayList<>();
ArrayList<Object> 小泽 = new ArrayList<>();
// 发牌
for (int i = 0; i < box.size() - 3; i++) {
if (i % 3 == 0) {
小波.add(box.get(i));
}
else if (i % 3 == 1) {
小苍.add(box.get(i));
}
else if (i % 3 == 2) {
小泽.add(box.get(i));
}
}
System.out.println("小波:"+小波);
System.out.println("小苍:"+小苍);
System.out.println("小泽:"+小泽);
//底牌
method01(box);
}
public static void method01(ArrayList<String> box) {
System.out.println("底牌:");
for (int i =box.size()-3 ; i <box.size() ; i++) {
System.out.print(box.get(i)+" ");
}
}
public static void main(String[] args) {
// 花色
String[] color = { "♠","♥","♣","♦" };
// 数字
String[] num = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
// 牌盒
ArrayList<String> box = new ArrayList<>();
for (int i = 0; i < color.length; i++) {
for (int j = 0; j < num.length; j++) {
box.add(color[i] + num[j]);
}
}
box.add("大王");
box.add("小王");
// 洗牌
Collections.shuffle(box);
//System.out.println(box);
// 找三个人来打牌
ArrayList<Object> 小波 = new ArrayList<>();
ArrayList<Object> 小苍 = new ArrayList<>();
ArrayList<Object> 小泽 = new ArrayList<>();
// 发牌
for (int i = 0; i < box.size() - 3; i++) {
if (i % 3 == 0) {
小波.add(box.get(i));
}
else if (i % 3 == 1) {
小苍.add(box.get(i));
}
else if (i % 3 == 2) {
小泽.add(box.get(i));
}
}
System.out.println("小波:"+小波);
System.out.println("小苍:"+小苍);
System.out.println("小泽:"+小泽);
//底牌
method01(box);
}
public static void method01(ArrayList box) {
System.out.println("底牌:");
for (int i =box.size()-3 ; i <box.size() ; i++) {
System.out.print(box.get(i)+" ");
}
}
/*
模拟斗地主的发牌功能(发牌完毕后发到手上的牌是有顺序的)
分析:
A:创建一个HashMap集合
B:创建一个ArrayList集合
C:创建两个字符串,一个是花色,一个是牌的数字 (发牌得有一盒牌)
为了方便以后的排序,创建这两个字符串的时候,按照大小顺序排列(斗地主中的大小顺序)
大小王除外
D:把这两个字符串放进HashMap集合中(拼接一起 花色+牌号) 同时给每个放进去牌进行编码0--52并存储
同时也给ArrayList集合中存储编码,大小王在这些编码完成后再进行编码 53 , 54
E:洗牌
F:发牌,其实发的是编号,由于要排序,所以创建TreeSet集合,斗地主是3人和底牌3张,对应4个集合,
a:利用取模的方法给3个人发牌,x%3=0;x%3=1;x%3=2
b:底牌发开始的3张
G:看牌,因为要使用3次这个功能,所以设定一个方法:
a:返回类型:void
b:参数列表:String name(玩家名字) ; TreeSet i(牌的编号) ; HashMap hm(牌)
*/
package com.htfg.callcenter.portal.common;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class w1 {
public static void main(String[] args) {
//创建一个HashMap集合
HashMap<Integer , String> poker = new HashMap<Integer , String>();
//创建一个ArrayList集合
ArrayList<Integer> index = new ArrayList<Integer>();
//创建两个字符串,一个是花色,一个是牌的数字 (发牌得有一盒牌)
String[] colors = {"♦","♣","♥","♠"};
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A", "2", };
//把这两个字符串放进HashMap集合中(拼接一起 花色+牌号)同时给每个放进去牌进行编码0--52并存储
int count = 0;
for(String number : numbers){
for(String color : colors){
//拼接
String pk = color.concat(number);
//把编码和牌存储到HashMap中
poker.put(count, pk);
//把编码存到ArrayList中
index.add(count);
count ++;
}
}
//给大小王编码并存储
poker.put(count, "小王");
index.add(count);
count ++;
poker.put(count, "大王");
index.add(count);
//洗牌
Collections.shuffle(index);
//发牌,由于要排序,所以创建TreeSet集合,斗地主是3人和底牌3张,对应4个集合,
TreeSet<Integer> player1 = new TreeSet<Integer>();
TreeSet<Integer> player2 = new TreeSet<Integer>();
TreeSet<Integer> player3 = new TreeSet<Integer>();
TreeSet<Integer> dipai = new TreeSet<Integer>();
//a:利用取模的方法给3个人发牌,x%3=0;x%3=1;x%3=2 b:底牌剩下的3张
//此时ArrayList中已经存储了0-54的编码
for(int x = 0 ;x < index.size() ; x ++){
if(x >= index.size() - 3){
dipai.add(index.get(x));//给底牌发ArrayList前3个编号,不能dipai.add(x),这样给底牌的是0,1,2
}
else if(x % 3 == 0){
player1.add(index.get(x));
}
else if(x % 3 == 1){
player2.add(index.get(x));
}
else if(x % 3 == 2){
player3.add(index.get(x));
}
}
//看牌
lookpoker("玩家1", player1, poker);
lookpoker("玩家2", player2, poker);
lookpoker("玩家3", player3, poker);
lookpoker("底牌", dipai, poker);
}
//创建一个看牌的方法,a:返回类型:void
//b:参数列表:String name(玩家名字) ; TreeSet<Integer> i(牌的编号) ; HashMap<Integer,String> hm(牌)
public static void lookpoker(String name,TreeSet<Integer> i ,HashMap<Integer,String> hm) {
//首先是名字
System.out.print(name+"的牌是:");
//其次是牌,遍历
for(Integer key : i){
//TreeSet的值就是HashMap的键,所以可以得到对应的值,也就是牌
String result = hm.get(key);
//输出看到的牌
System.out.print(result+" ");
}
System.out.println(" ");
}
}
你遍历你的map value 等于2、3、4 不就是相应的牌型
第二个人回答的答案逻辑应该是对的
看了楼主的两个相同问题。。都回答一下。。。
这种牌型问题我觉得应该将所有可能的牌型都作为一个map集合,比如34567顺子,map集合就叫34567顺子,然后value为3,4,5,6,7
遍历手中卡牌的时候,只要分别调用map的contain()方法,用&&连接,判断是否同时包含所有value,则可以判断出卡牌为顺子34567
这种方法比较复杂 需要写很多的牌型可能性的map,然后一 一判断,最后输出复合的牌型
方法比较笨,但是应该可以实现楼主的要求。。。
public static void Paixing(List shouPai) {
HashMap px1=new HashMap<>();//判断34567顺子,至于更复杂的可以3456789依次往后判断
px1.put(1, "3");
px1.put(2, "4");
px1.put(3, "5");
px1.put(4, "6");
px1.put(5, "7");
int i=1;
ArrayList<String> shouPai1=(ArrayList<String>) shouPai;
while(shouPai1.contains(px1.get(i))) {
if(i==px1.size()) {
System.out.println("顺子34567");
break;//一旦手牌中包含此map集合,则跳出循环,输出牌型
}
i++;
}
}