一 思路
1.1 分析
一副扑克牌共有大小王,红桃、黑桃、梅花、方块(A---K)共54张,那么就先生成一副扑克牌,然后调用方法,先后5次,以牌数量为随机点,先获取牌的索引,然后获取牌,将获取的牌放入集合,然后将这个字符串集合转为数字集合,然后将数字集合进行排序,然后进行是否为顺子的判断;
二 实现
package com.thread.puke;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class pukepai {
public static void main(String[] args) {
String[] huase = new String[]{"♠️","♥️","♣️","♦️"};
String[] dianshu = new String[]{"A","2","3","4","5","6","7","8","9",
"10","J","Q","K"};
ArrayList<String> puke = createPuke(huase,dianshu);
for (String s : puke) {
System.out.print(s+",");
}
System.out.println();
System.out.println("===========");
ArrayList<String> getPai = randomRowMethod(puke);
for (String s : getPai) {
System.out.print(s+",");
if ("大王".equals(s)||"小王".equals(s)){
System.out.print("当前得到的牌包含大小王不是顺子!");
return;
}
}
ArrayList<Integer> shuzi = new ArrayList<>();
for (String s : getPai) {
String[] split = s.split(" ");
String getNum = split[1];
if ("J".equals(getNum)){
getNum =String.valueOf(11);
}
if ("Q".equals(getNum)){
getNum =String.valueOf(12);
}
if ("K".equals(getNum)){
getNum =String.valueOf(13);
}
if ("A".equals(getNum)){
getNum =String.valueOf(1);
}
shuzi.add(Integer.parseInt(getNum));
}
System.out.println();
System.out.println("========");
for (Integer integer : shuzi) {
System.out.print(integer + ",");
}
Collections.sort(shuzi);
System.out.println();
System.out.println("========");
JudgingStraight(shuzi);
}
private static void JudgingStraight(ArrayList<Integer> shuzi){
for (int i = 1; i < shuzi.size(); i++) {
if(shuzi.get(i)-shuzi.get(i-1) != 1){
System.out.print("当前得到的牌不是顺子!");
return;
}
}
System.out.print("当前得到的牌是顺子!");
}
private static ArrayList<String> randomRowMethod(ArrayList<String> puke) {
ArrayList<String> pai = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 5; i++) {
int j = random.nextInt(puke.size());
pai.add(puke.get(j));
}
return pai;
}
private static ArrayList<String> createPuke(String[] huase, String[] dianshu) {
String[] kingAndQu = new String[]{"大王","小王"};
ArrayList<String> puke = new ArrayList<String >();
puke.add(kingAndQu[0]);
puke.add(kingAndQu[1]);
for (int i = 0; i < huase.length; i++) {
for (int j = 0; j < dianshu.length; j++) {
String pai = huase[i]+" "+dianshu[j];
puke.add(pai);
}
}
return puke;
}
}