POKER

作者: 李哈哈_bing | 来源:发表于2018-01-07 18:31 被阅读0次

    card类

    public class card{
        private int value;
        private String 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;
        }
    
        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;
        }
    
    }
    
    
    

    Poker类

    public class poker2 {
        private List<card2> cards = new ArrayList<card2>();
        private int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,};
        private String[] colors = {"红桃", "梅花", "黑桃", "方片"};
    
        public poker2() {
            for (int i = 0; i < 52; i++) {
                card2 card2s = new card2();
                card2s.setColor(colors[i / 13]);
                card2s.setValue(values[i % 13]);
                cards.add(card2s);
            }
        }
    
        public void show() {
    
            System.out.println(cards + "\t");
        }
    
        public void shuffer() {
            Collections.shuffle(cards);
        }
    
        public List<card2> takecard() {
            int n = 0;
            List<card2> card2List = new ArrayList<card2>();
            for (card2 c : cards) {
                card2List.add(c);
                n++;
                if (n == 5) {
                    break;
                }
            }
            return card2List;
        }
    
        public String texttype(List<card2> card2List) {
            boolean isfoluwer = false;
            boolean isshunzi = false;
            String str = "h";
            Set<String> colorset = new HashSet<String>();
            for (card2 cc : card2List) {
                colorset.add(cc.getColor());
            }
            Set<Integer> valueset = new HashSet<Integer>();
            List<Integer> valuelist = new ArrayList<Integer>();
            for (card2 cc : card2List) {
                valueset.add(cc.getValue());
                valuelist.add(cc.getValue());
            }
            Collections.sort(valuelist);
    
            if (colorset.size() == 1) {
                isfoluwer = true;
            }
            if (valueset.size() == 5 && valuelist.get(4) - valuelist.get(0) == 4) {
                isshunzi = true;
            }
            if (isfoluwer == true && isshunzi == true) {
                // System.out.println("同花顺");
                str = "同花顺";
            } else if (isfoluwer == true) {
                // System.out.println("同花");
                str = "同花";
            } else if (isshunzi == true) {
                //System.out.println("顺子");
                str = "顺子";
            } else if (valueset.size() == 5) {
                // System.out.println("杂牌");
                str = "杂牌";
            } else if (valueset.size() == 4) {
                // System.out.println("一对");
                str = "一对";
            } else if (valueset.size() == 3) {//两队或三条
                Map<Integer, Integer> mapCardValue2Times = new HashMap<>();
                for (card2 card2 : card2List) {
                    if (mapCardValue2Times.containsKey(card2.getValue())) {
                        int c = mapCardValue2Times.get(card2.getValue());
                        c++;
                        mapCardValue2Times.put(card2.getValue(), c);
                    } else {
                        mapCardValue2Times.put(card2.getValue(), 1);
                    }
                }
                for (Integer integer : mapCardValue2Times.keySet()) {//(8,2)(5,2) (4,1) (5,3) (3,1)(4,1)
                    if (mapCardValue2Times.get(integer) == 2) {
                        //System.out.println("两对");
                        str = "两对";
                        break;
                    } else if (mapCardValue2Times.get(integer) == 3) {
                        // System.out.println("三条");
                        str = "三条";
                        break;
                    }
                }
            } else if (valueset.size() == 2) {//四代一或三代二
                Map<Integer, Integer> mapCardValue2Times = new HashMap<>();
                for (card2 card2 : card2List) {
                    if (mapCardValue2Times.containsKey(card2.getValue())) {
                        int c = mapCardValue2Times.get(card2.getValue());
                        c++;
                        mapCardValue2Times.put(card2.getValue(), c);
                    } else {
                        mapCardValue2Times.put(card2.getValue(), 1);
                    }
                }
                for (Integer integer : mapCardValue2Times.keySet()) {//(8,4)(5,1) (5,3) (3,2)
                    if (mapCardValue2Times.get(integer) == 4) {
                        // System.out.println("四带一");
                        str = "四带一";
                        break;
                    } else if (mapCardValue2Times.get(integer) == 3) {
                        //System.out.println("三带一对");
                        str = "三带一对";
                        break;
                    }
                }
            }
    
            return str;
        }
    
    
    }
    
    
    

    主方法

    public class pokermain {
        public static void main(String[] args) {
            poker2 p = new poker2();
            p.show();
            p.shuffer();
            System.out.println();
            p.show();
            List<card2> cl = p.takecard();
            System.out.println(cl);
            String string = p.texttype(cl);
            System.out.println(string);
    
        }
    }
    

    相关文章

      网友评论

          本文标题:POKER

          本文链接:https://www.haomeiwen.com/subject/ppjvgxtx.html