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