public class Card {
private int value;
private String color;
public int getValue() {
return value;
}
public String getColor() {
return color;
}
public Card(int value, String color) {
this.value = value;
this.color = color;
}
@Override
public String toString() {
String _value = "";
switch (this.value){
case 14:
_value="A";
break;
case 11:
_value="J";
break;
case 12:
_value="Q";
break;
case 13:
_value="K";
break;
default:
_value = String.valueOf(this.value);
break;
}
return color + _value;
}
}
import java.util.*;
public class Poker {
String[] colors = {"红桃","方片","黑桃","草花"};
Card[] cards = new Card[20];
public Poker() {
int count = 0;
for(int i = 0; i < colors.length; i++){
for(int j = 10; j < 15; j++){
cards[count++] = new Card(j,colors[i]);
}
}
}
public void output(){
int count = 0;
for(int i = 0; i < colors.length; i++){
for(int j = 10; j < 15; j++){
System.out.print(cards[count++]+" ");
}
System.out.println();
}
}
public void shuffle(){
Random random = new Random();
for(int i = 0; i < cards.length; i++){
int idx = random.nextInt(cards.length);
Card tmp = cards[i];
cards[i] = cards[idx];
cards[idx] = cards[i];
}
}
public List<Card> getOneHand(){
List<Card> cardLst = new ArrayList<>();
for(int i = 0; i < 5; i++){
cardLst.add(cards[i]);
}
return cardLst;
}
public String checkCardType(List<Card> hands){
Set<String> colorSet = new HashSet<>();
Set<Integer> valueSet = new HashSet<>();
List<Integer> valueLst = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
for(Card card : hands){
colorSet.add(card.getColor());
valueSet.add(card.getValue());
valueLst.add(card.getValue());
if(map.containsKey(card.getValue())){
Integer count = map.get(card.getValue());
map.put(card.getValue(), count+1);
}
else{
map.put(card.getValue(), 1);
}
}
Collections.sort(valueLst);
if(valueLst.get(4) - valueLst.get(0) == 4 && colorSet.size() == 1){
return "同花顺";
}
else if(colorSet.size() == 1){
return "同花";
}
else if(valueLst.get(4) - valueLst.get(0) == 4 && valueSet.size() == 5){
return "顺子";
}
else if(valueSet.size() == 4 ){
return "一对";
}
else if(valueSet.size() == 5 ){
return "杂牌";
}
else if(valueSet.size() == 2 ){
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
if(entry.getValue() == 1){
return "4带1";
}
else if(entry.getValue() == 2) {
return "3带2";
}
}
}
else if(valueSet.size() == 3 ){
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
if(entry.getValue() == 3){
return "311";
}
else if(entry.getValue() == 2) {
return "221";
}
}
}
return "error";
}
}
stream版
public String getHandType(List<Card> hand){
Set<String> colorSet = hand.stream().map(Card::getColor).collect(Collectors.toSet());
List<Integer> valueLst = hand.stream().map(Card::getValue).collect(Collectors.toList());
Set<Integer> valueSet = hand.stream().map(Card::getValue).collect(Collectors.toSet());
Map<Integer, List<Card>> mapCard = hand.stream().collect(Collectors.groupingBy(Card::getValue));
long countFour = mapCard.entrySet().stream().filter(entry -> entry.getValue().size() == 4).count();
long countThree = mapCard.entrySet().stream().filter(entry -> entry.getValue().size() == 3).count();
Collections.sort(valueLst);
boolean bIsSameColor = false;
boolean bIsStraight = false;
if(valueLst.get(4) - valueLst.get(0) == 4
&& valueSet.size() == 5){
bIsStraight = true;
}
if(colorSet.size() == 1){
bIsSameColor = true;
}
if(bIsStraight && bIsSameColor){
return "同花顺";
}
if(bIsSameColor){
return "同花";
}
if(bIsStraight){
return "顺子";
}
if(valueSet.size() == 4){
return "一对";
}
if(valueSet.size() == 5){
return "杂牌";
}
if(valueSet.size() == 2){
if(countFour>0){
return "四条";
}
else{
return "满堂红";
}
}
if(valueSet.size() == 3){
if(countThree>0){
return "三带一";
}
else{
return "两对";
}
}
return "";
}
import java.util.List;
public class Main {
public static void main(String[] args) {
Card card = new Card(11,"红桃");
System.out.println(card);
Poker poker = new Poker();
poker.output();
poker.shuffle();
System.out.println();
poker.output();
List<Card> oneHand = poker.getOneHand();
System.out.println(oneHand);
System.out.println(poker.checkCardType(oneHand));
}
}