码迷,mamicode.com
首页 > 编程语言 > 详细

扑克模拟,牌型判断java版

时间:2017-05-28 10:01:30      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:exti   ++   img   str   void   style   cte   try   switch   

Card类

技术分享
package com.company;




public class Card {
    private String     color;
    private Integer    value;
    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;
    }
    public String ToString()
    {
        String strValue = "";
        switch(value)
        {
            case 1:
            {
                strValue = "A";
                break;
            }
            case 11:
            {
                strValue = "J";
                break;
            }
            case 12:
            {
                strValue = "Q";
                break;
            }
            case 13:
            {
                strValue = "K";
                break;
            }
            default:
                strValue = value.toString();
                break;
        }
        return color+strValue;
    }

}
View Code

主类

技术分享
package com.company;

import java.util.ArrayList;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;


public class Main {

    public static void Judge(Card[] hands)
    {
        boolean bIsSameColor = false;
        boolean bIsShunzi = false;

        Set<String> setColors = new HashSet<String>();

        for(int i=0; i < hands.length; i++)
        {
            setColors.add(hands[i].getColor());
        }

        if(setColors.size()==1)
        {
            bIsSameColor=true;
            //System.out.println("同花");
        }

        List<Integer> lstValues = new ArrayList<Integer>();
        Set<Integer> setValues = new HashSet<Integer>();

        for(int i=0; i < hands.length; i++)
        {
            lstValues.add(hands[i].getValue());
            setValues.add(hands[i].getValue());
        }
        //顺子,必须满足3个条件
        //1.这手牌先排好序(由大到小)sortHands[5]
        //2.这手牌里面牌值不能有重复项
        //3.如果sortHands[0]-sortHands[4]==4
        Collections.sort(lstValues);
        int nDiff = lstValues.get(4)-lstValues.get(0);
        if(setValues.size()==5&&nDiff==4)
        {
            bIsShunzi = true;
            //System.out.println("顺子");
        }

        if(bIsSameColor&&bIsShunzi)
        {
            System.out.println("同花顺");
        }
        else if(bIsSameColor)
        {
            System.out.println("同花");
        }
        else if(bIsShunzi)
        {
            System.out.println("顺子");
        }
        else if(setValues.size()==5)
        {
            System.out.println("杂牌");
        }

        Map<Integer,List<Card>> map = new HashMap<Integer,List<Card>>();

        for(int i = 0; i < hands.length; i++)
        {

            int nValue = hands[i].getValue();//红桃9
            if(map.containsKey(nValue))
            {
                List<Card> lstCards = map.get(nValue);
                lstCards.add(hands[i]);

            }
            else//不包含
            {
                List<Card> lstCards = new ArrayList<Card>();
                lstCards.add(hands[i]);
                map.put(nValue, lstCards);
            }
        }

        if(map.size()==4)
        {
            System.out.println("一对");
        }
        else if(map.size() == 2)//4带1,3带2
        {
            boolean bIsFourWithOne = false;
            for(Map.Entry<Integer, List<Card>> e : map.entrySet() )
            {
                if(e.getValue().size() == 4)
                {
                    bIsFourWithOne = true;
                }
            }
            if(bIsFourWithOne == true)
            {
                System.out.println("四带一");
            }
            else
            {
                System.out.println("三带二");
            }

//            Map<String,String> map2;
//            map2.add("apple","苹果");
//            map2.add("orange","桔子");
//            map2.values();
        }
        else if(map.size() == 3)//311,221
        {
            boolean bIsThreeOneOne = false;
            for(Map.Entry<Integer, List<Card>> e : map.entrySet() )
            {
                if(e.getValue().size() == 3)
                {
                    bIsThreeOneOne = true;
                }
            }
            if(bIsThreeOneOne == true)
            {
                System.out.println("311");
            }
            else
            {
                System.out.println("221");
            }
        }
//        int i = 7;
//        Card c = card1;

//        (3,[方块3])
//        (5,[红桃5])
//        (9,[黑桃9,草花9,红桃9])
//
//
//
//        (13,[红桃13,方块13])
//        (5,[草花5,黑桃5])
//        (2,[红桃2])
//
//        (4,[黑桃4,草花4,红桃4,方块4])
//        (9,[草花9])
//        //4带1,3带2,311,221,2111
//        map.size()==4//2111
//        if(map.size()==2)
//            abs(map.value[0].size()-map.value[1].size())==3
//            //4带1
//            abs(map.value[0].size()-map.value[1].size())==1
//            //,3带2
//        if(map.size()==3)
//
//            //311,221
    }
    public static void generateHands(Card[] pokes,Card[] hands)
    {
        for(int i = 0; i < hands.length; i++)
        {
            hands[i] = pokes[i];
        }
    }
    public static void outputCards(Card[] pokes)
    {
        for(int i = 0; i < pokes.length; i++)
        {
            if(i%13==0)
                System.out.println();
            System.out.print(pokes[i].ToString()+" ");

        }
    }
    public static void shuffle(Card[] pokes)
    {
        Random r = new Random();

        for(int i = 0; i < pokes.length; i++)
        {
            int n = r.nextInt(i+1);
            Card cTemp;
            cTemp = pokes[i];
            pokes[i] = pokes[n];
            pokes[n] = cTemp;
        }
    }
    public static void main(String[] args) {
        //System.out.println("helloworld");

        String[] Colors = {"红桃","方片","黑桃","草花"};
        Integer[] Values = {1,2,3,4,5,6,7,8,9,10,11,12,13};

        Card[] pokes = new Card[52];
        Card[] hands = new Card[5];

        int nIndex = 0;
        for(int i = 0; i < Colors.length; i++)
        {
            for(int j = 0; j < Values.length; j++)
            {
                pokes[nIndex] = new Card();
                pokes[nIndex].setColor(Colors[i]);
                pokes[nIndex].setValue(Values[j]);
                nIndex++;
            }
        }

        outputCards(pokes);

        shuffle(pokes);

        generateHands(pokes,hands);

        outputCards(hands);

        Judge(hands);
    }
}
View Code

 

扑克模拟,牌型判断java版

标签:exti   ++   img   str   void   style   cte   try   switch   

原文地址:http://www.cnblogs.com/ice-river/p/6914937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!