休闲题==》从扑克牌中任意取5张牌判断是否为顺子

判断是否为顺子

  • 一 思路
    • 1.1 分析
  • 二 实现

一 思路

1.1 分析

一副扑克牌共有大小王,红桃、黑桃、梅花、方块(A---K)共54张,那么就先生成一副扑克牌,然后调用方法,先后5次,以牌数量为随机点,先获取牌的索引,然后获取牌,将获取的牌放入集合,然后将这个字符串集合转为数字集合,然后将数字集合进行排序,然后进行是否为顺子的判断;

二 实现

package com.thread.puke;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/**
 * @description:
 * @author: Sw_Ljb
 * @PACKAGE_NAME:com.thread.puke
 * @time: 2022/8/19 下午3:22
 * @version: 1.0
 */

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);

        //判断是否是顺子
        //1、判断是否有大小王 有 不是
        for (String s : getPai) {
            System.out.print(s+",");
            if ("大王".equals(s)||"小王".equals(s)){
                System.out.print("当前得到的牌包含大小王不是顺子!");
                return;
            }
        }
        //2、先将其进行从小到大的排序
        //先得到一个 数字集合
        ArrayList<Integer> shuzi = new ArrayList<>();
        for (String s : getPai) {
            String[] split = s.split(" ");
            String getNum = split[1];
            //J,♠️ Q,♠️ K A
            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("========");
        //3、从第二位比较梯度差 看是不是顺子
        //ArrayList demo = new ArrayList<>();
        //demo.add(1);
        //demo.add(2);
        //demo.add(3);
        //demo.add(4);
        //demo.add(5);
        JudgingStraight(shuzi);





    }

    /**
     * 判断是不是顺子
     * @param 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("当前得到的牌是顺子!");
    }


    /**
     * 随机拿5张牌
     * @param puke
     * @return
     */
    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());
            //System.out.println(j);
            pai.add(puke.get(j));
        }

        return pai;
    }

    /**
     * 得到所有的牌
     * @param huase:花色
     * @param dianshu:点数
     * @return
     */
    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;

    }
}

你可能感兴趣的:(数据结构,java,集合,扑克牌问题)