扑克牌

package com.company;
package com.company;

/**
 * Created by ttc on 2017/12/28.
 */
public class Card {
    private int value;//牌值
    private String color;//花色

    public Card(int value, String color) {
        this.value = value;
        this.color = color;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String toString()
    {
        String str = "";
        if(value == 11)
        {
            str = "J";
        }
        else if(value == 12)
        {
            str = "Q";
        }
        else if(value == 13)
        {
            str = "K";
        }
        else if(value == 1)
        {
            str = "A";
        }
        else
        {
            str = value+"";
        }
        return color+str;
    }
}
package com.company;
import java.util.*;
/**
 * Created by ttc on 2017/12/28.
 */
public class Poker {
    List<Card> cards = new ArrayList<Card>();
    private String[] colors = {"红桃", "方片", "黑桃","草花"};
    private int[] values = {1,2,3,4,5,6,7,8,9,10,11,12,13};

    public Poker()
    {
        //初始化,创建52张扑克牌
        //红桃13张 1,2,3...
        //方片13张
        //外层循环生成花色
        //里层循环生成牌值
        for(int i = 0; i < colors.length; i++)
        {
            for(int j = 0; j < values.length; j++)
            {
                Card  card = new Card(values[j],colors[i]);
//                card.setColor(colors[i]);
//                card.setValue(values[j]);
                cards.add(card);
            }
        }
    }
    //洗牌方法
    public void shuffle()
    {
        Collections.shuffle(cards);
    }
    //打印扑克中的每一张牌
    public void output()
    {
        System.out.println(cards);
    }
    //发一手牌
    public List<Card> takeHands()
    {
        List<Card> cardList = new ArrayList<>();
        //把cards的前5张牌放到cardList里
        int count = 0;
        for(Card card : cards)
        {
            cardList.add(card);
            count++;
            if(count == 5)
            {
                break;
            }
        }
        return cardList;
    }
    //判断牌型
    public String judgeType(List<Card> cardList)
    {
        boolean isSameColor = false;//是否同花
        boolean isShunZi = false;//是否顺子
        String strType = "";
        Set<String> setColors = new HashSet<>();
        for(Card card : cardList)
        {
            setColors.add(card.getColor());
        }
        if(setColors.size() == 1)
        {
            isSameColor = true;
//            System.out.println("同花");
        }
        //5张牌,排好序,(最后一张减去第一张 == 4 && 把5张牌放到set中,set.size()== 5)
        Set<Integer> setValues = new HashSet<>();
        List<Integer> listValues = new ArrayList<>();
        for(Card card : cardList)
        {
            int value = card.getValue();
            setValues.add(value);
            listValues.add(value);
        }
        Collections.sort(listValues);
        int between = listValues.get(4) - listValues.get(0);
        if(between == 4 && setValues.size() == 5)
        {
            isShunZi = true;
//            System.out.println("顺子");
        }

        if(isSameColor == true && isShunZi == true)
        {
            strType = "同花顺";
//            System.out.println("同花顺");
        }
        else if(isSameColor == true)
        {
            strType = "同花";
//            System.out.println("同花");
        }
        else if(isShunZi == true)
        {
            strType = "顺子";
//            System.out.println("顺子");
        }
        else if(setValues.size() == 5)
        {
            strType = "杂牌";
//            System.out.println("杂牌");
        }
        else if(setValues.size() == 4)
        {
            strType = "一对";
//            System.out.println("一对");
        }
        else if(setValues.size() == 3)
        {
//            System.out.println("两对或3条");
//            System.out.println("四带一或三带二");
            Map<Integer, Integer> mapCardValue2Counts = new HashMap<>();
            //将5张牌从list结构转化到map中
            for(Card card: cardList)
            {
                //map的key中是否包含当前牌的值,如果包含,拿出当前牌值已经出现的次数
                if(mapCardValue2Counts.containsKey(card.getValue()))
                {
                    int count = mapCardValue2Counts.get(card.getValue());
                    count++;
                    mapCardValue2Counts.put(card.getValue(),count);
                }
                else//不包含
                {
                    mapCardValue2Counts.put(card.getValue(),1);
                }
            }
//            (9,3),(5,1),(3,1)
//            (13,2),(5,2),(2,1)
            for(Map.Entry<Integer, Integer> entry : mapCardValue2Counts.entrySet())
            {
                if(entry.getValue() == 3)
                {
                    strType = "三带一";
//                    System.out.println("三带一");
                    break;
                }
                else if(entry.getValue() == 2)
                {
                    strType = "两对";
//                    System.out.println("两对");
                    break;
                }
            }
        }
        else if(setValues.size() == 2)
        {
//            System.out.println("四带一或三带二");
            Map<Integer, Integer> mapCardValue2Counts = new HashMap<>();
            //将5张牌从list结构转化到map中
            for(Card card: cardList)
            {
                //map的key中是否包含当前牌的值,如果包含,拿出当前牌值已经出现的次数
                if(mapCardValue2Counts.containsKey(card.getValue()))
                {
                   int count = mapCardValue2Counts.get(card.getValue());
                   count++;
                   mapCardValue2Counts.put(card.getValue(),count);
                }
                else//不包含
                {
                    mapCardValue2Counts.put(card.getValue(),1);
                }
            }
//            (4,4),(9,1)
//            (3,3),(10,2)
            for(Map.Entry<Integer, Integer> entry : mapCardValue2Counts.entrySet())
            {
                if(entry.getValue() == 4)
                {
                    strType = "四带一";
//                    System.out.println("四带一");
                    break;
                }
                else if(entry.getValue() == 3)
                {
                    strType = "三带二";
//                    System.out.println("三带二");
                    break;
                }
            }
        }
        return strType;
    }
}
package com.company;
import java.util.ArrayList;
import java.util.List;
public class Main {
    public static void main(String[] args) {
    // write your code here
        Poker poker = new Poker();
        poker.output();
//        poker.shuffle();//洗牌
        System.out.println();
        poker.output();

//       List<Card> cardList = poker.takeHands();
        List<Card> cardList = new ArrayList<>();
        Card card = new Card(5,"黑桃");
        cardList.add(card);
        card = new Card(5,"红桃");
        cardList.add(card);
        card = new Card(9,"草花");
        cardList.add(card);
        card = new Card(8,"草花");
        cardList.add(card);
        card = new Card(8,"方片");
        cardList.add(card);

        System.out.println(cardList);

        String type = poker.judgeType(cardList);
        System.out.println(type);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 随机抽出5张牌,判断抽出的5张牌是不是连续的扑克牌,不许使用排序算法。 程序输出如下:
    FredricZhu阅读 4,003评论 0 0
  • 导读: 今晚我终于拆开了萌姐的<<人生效率手册>>,内心有点激动,我被萌姐十九年五点起床学习的精神感动,也意识到,...
    甜甜姐Sweety阅读 5,154评论 0 1
  • 当你放下面子赚钱的时候, 说明你已经懂事了。 当你用钱赚回面子的时候, 说明你已经成功了。 当你用面子可以赚钱的时...
    郑州三千茶农生活舘阅读 2,481评论 0 0
  • 但凡与车有关的,无论大巴、火车、飞机或是公交,上车后在有限的选择下,总会选择一个靠窗的稍后排的位置缓缓坐下,心...
    berrys阅读 2,615评论 0 3
  • 我认识木心先生,是从陈丹青先生的《文学回忆录》开始的。从此深深的沉迷。我不敢轻易提起,因为,先生说过,他内心敬仰的...
    柳汀雪阅读 3,480评论 0 2