52張撲克牌分法__java

來源:互聯網
上載者:User
package com.company;public class Main {    public static void main(String[] args) {   //1.先產生52張撲克        Poke poke = new Poke();        poke.makeCards();        //2.將產生的撲克列印出來        poke.showCards();        //3.洗牌        poke.shuffle();        //4.列印洗好之後的撲克牌        System.out.println();        System.out.println("\n"+"洗牌之後");        poke.showCards();        System.out.println();        //獲得一手牌        Card[] handCards = poke.getHandCards();        for(Card c :handCards){            System.out.println(c);        }        //判斷牌型        poke.judgeCardType(handCards);    }}
 
package com.company;/** * 定義一個撲克牌的類,有花色屬性,還有牌面值的屬性 * Created by ttc on 2017/7/1. */public class Card {    private String color;    private Integer value;//考慮到之後牌面值會有j q k 會用到toString方法,所以定義Integer類型的變數    public String getColor() {        return color;    }    public void setColor(String color) {        this.color = color;    }    public Integer getValue() {        return value;    }    public void setValue(Integer value) {        this.value = value;    }    //重寫Card類的toString方法,能列印"J","Q","K","A"    public String toString(){        String strValue = value.toString();        switch(strValue){            case "1":                strValue = "A";                break;            case "11":                strValue = "J";                break;            case "12":                strValue = "Q";                break;            case "13":                strValue = "K";                break;            default:                break;        }        return color+strValue;    }}
package com.company;import java.util.*;/** * 撲克的功能類 * Created by ttc on 2017/7/1. */public class Poke {    String[]colors = {"紅桃","黑桃","方片","草花"};    Integer[]values = {1,2,3,4,5,6,7,8,9,10,11,12,13};    Card [] cards = new Card[52];    //定義生產撲克的方法,雙For迴圈    public void makeCards(){        int index = 0;        for(int i = 0;i<colors.length;i++){            for(int j = 0;j<values.length;j++){                cards[index] = new Card();                cards[index].setColor(colors[i]);                cards[index].setValue(values[j]);                index++;            }        }        return;    }    //顯示撲克的方法    public void showCards(){        int index2 = 0;        for(Card c : cards){            if(index2%13==0){                System.out.println();            }            System.out.print(c.toString());            index2++;        }    }    //定義洗牌的方法    public void shuffle(){        Random r = new Random();        for(int i = 0 ;i<cards.length;i++){            int index = r.nextInt(i+1);            Card temp = cards[i];            cards[i] = cards[index];            cards[index] = temp;        }    }    //定義取一手牌的方法    public Card[] getHandCards(){        Card[] handCards = new Card[5];        for(int i = 0;i<5;i++){           handCards[i] = cards[i];        }        return handCards;    }    //判斷牌型的方法    public void judgeCardType(Card [] handCards){        //先判斷這手牌是不是同花        boolean bIsSameColor = false;        boolean bIsShunZi = false;        Set<String> colorSet = new HashSet<>();        for(int i = 0;i<handCards.length;i++){            colorSet.add(handCards[i].getColor());        }        if(colorSet.size()==1){            bIsSameColor = true;        }        //在判斷是不是順子        Set<Integer> valueSet = new HashSet<>();//用於判斷這手牌中是否有重複的牌        List<Integer> valueList = new ArrayList<>();        for(int i = 0;i<handCards.length;i++){            valueSet.add(handCards[i].getValue());            valueList.add(handCards[i].getValue());        }        Collections.sort(valueList);        int diff = valueList.get(4)-valueList.get(0);        if(diff == 4 && valueSet.size()==5) {            bIsShunZi = true;        }        if(bIsSameColor&&bIsShunZi){            System.out.println("同花順");        }else if(bIsSameColor){            System.out.println("同花");        }else if(bIsShunZi){            System.out.println("順子");        }else if (valueSet.size()==5){            System.out.println("雜牌");        }else if (valueSet.size()==4){            System.out.println("一對");        }        else{            //map的key儲存的是牌的值,map的值儲存的是同樣值的牌的列表            Map<Integer,List<Card>> map = new HashMap<>();            for(int i = 0;i<handCards.length;i++){                Card card = handCards[i];                if(!map.containsKey(card.getValue())){                    //不包括的情況,需要添加                    List<Card> listCard = new ArrayList<>();                    listCard.add(card);                    map.put(card.getValue(),listCard);                }else{                    //已經存在,就只在LIST中添加                    List<Card> listCard = map.get(card.getValue());                    listCard.add(card);                }            }            if(map.size()==2){                //有4帶1和3帶2兩種情況                boolean bIsFourWithOne = false;                for(Map.Entry<Integer,List<Card>> m :map.entrySet()){                    if(m.getValue().size()==4){                        bIsFourWithOne = true;                        break;                    }                }                if(bIsFourWithOne==true){                    System.out.println("4帶1");                }else{                    System.out.println("3帶2");                }            }else if (map.size()==3){                //311,221兩種可能                boolean bIsThreeWithOneOne = false;                for(Map.Entry<Integer,List<Card>> m : map.entrySet()){                    if(m.getValue().size()==3){                        bIsThreeWithOneOne = true;                        break;                    }                }                if (bIsThreeWithOneOne==true){                    System.out.println("311");                }else{                    System.out.println("221");                }            }        }        return;    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.