Skip to content

Commit 6801e04

Browse files
committed
init
1 parent 7c27759 commit 6801e04

18 files changed

+745
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*.iml
3+
target

README.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
# 德州扑克牌
3+
本程序是德州扑克牌大小裁判程序,来源阿里83行code比赛。
4+
https://college.devops.aliyun.com/
5+
6+
# 使用说明
7+
8+
```java
9+
//直接调用裁判程序即可。
10+
String result = JudgeManager.getInstance().judge(cards1, cards2);
11+
```
12+
13+
# 以下是源内容说明:
14+
15+
哇再次恭喜你完成83行代码重构大师赛第2关密封舱重启,来到了第3关能量舱!
16+
17+
能量舱会有一些小挑战。它的性能通过德州扑克判大小的系统控制。
18+
19+
你的任务:重构德州扑克判大小的系统,以实现能量舱重启。
20+
完成该任务即可重启能量舱,拿到歼星舰长能量水晶,还有机会抽取CHERRY机械键盘(限量1份)+CHERRY创意释压指尖轴(限量500份)。
21+
22+
德州扑克规则:
23+
1. 使用52张扑克(去掉大小王),分为四个花色(红桃H,方块D,黑桃S,梅花C),每个花色13张牌,依次从 2 到 A。J,Q,K,A分别对应为11,12,13,14(A只作为14)。游戏过程玩家各随机抽取5张牌,比大小。

24+
2. 所有牌型如下:
25+
* 同花顺(StraightFlush):同一花色的顺子。(最大牌:A-K-Q-J-10 最小牌:2-3-4-5-6)
26+
* 四条(FourOfAKind):四同张加单张。(最大牌:A-A-A-A-K 最小牌:2-2-2-2-3)
27+
* 葫芦(FullHouse):三同张加对子。(最大牌:A-A-A-K-K 最小牌:2-2-2-3-3)
28+
* 同花(Flush):同一花色。(最大牌:A-K-Q-J-9 最小牌:2-3-4-5-7)
29+
* 顺子(Straight):花色不一样的顺子。(最大牌:A-K-Q-J-10 最小牌:2-3-4-5-6)
30+
* 三条(ThreeOfAKind):三同张加两单张。(最大牌:A-A-A-K-Q 最小牌:2-2-2-3-4)
31+
* 两对(TwoPair):(最大牌:A-A-K-K-Q 最小牌:2-2-3-3-4)
32+
* 一对(OnePair):(最大牌:A-A-K-Q-J 最小牌:2-2-3-4-5)
33+
* 单牌(HighCard):(最大牌:A-K-Q-J-9 最小牌:2-3-4-5-7)

34+
3. 牌型大小:
35+
* 同花顺(StraightFlush) > 四条(FourOfAKind) > 葫芦(FullHouse) > 同花(Flush) > 顺子(Straight) > 三条(ThreeOfAKind) > 两对(TwoPair) > 一对(OnePair) > 单牌(HighCard)
36+
* 牌点从大到小为:A、K、Q、J、10、9、8、7、6、5、4、3、2,各花色不分大小。   
37+
* 同种牌型,对子时比对子的大小,其它牌型比最大的牌张,如最大牌张相同则比第二大的牌张,以此类推,都相同时为平手。
38+
* 例:
39+
"3H 6H 2H 4H 5H" > "8S 8D 8H JH 8C"
40+
"2S 2D 2H 4H 4C" > "3S 2D 6C 4H 5H"
41+
"2S 2D 4C 4H 6C" = "2H 2C 4D 4S 6D" 

42+
4. 比较程序输入2个玩家的牌,并返回比较结果。
43+
* 会以字符串形式输入玩家的牌,如 "3H 6H TH 4H AH"。每张牌由:牌点+花色 组成。10、11、12、13、14 分别用 T、J、Q、K、A 表示。
44+
* 输出格式:(请保证输出的内容符合格式要求,会用于单元测试做比较)
45+
* 牌型不同时:[player1/player2] wins - [牌型];例:player1:"3H 6H 2H 4H 5H",player2:"8S 8D 8H JH 8C"。输出:player1 wins - StraightFlush
46+
* 牌型相同时:[player1/player2] wins - high card:[牌点];例:player1:"3H 6H 2H 4H 5H",player2:"8D 9D TD JD QD"。输出:player2 wins - high card:Q
47+
* 平手时:输出:tie
48+
49+
### 注意:
50+
后台会运行若干单测来验证程序的正确性,请不要修改提示了不允许修改的类或者方法的签名,以保证测试的正常运行。例如以下提示:
51+
// Please don't modify the signature of this method.
52+
// Please don't modify the class name.

pom.xml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.zjiecode</groupId>
8+
<artifactId>poker</artifactId>
9+
<version>1.0.0</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
<dependencies>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.13</version>
27+
<scope>test</scope>
28+
</dependency>
29+
30+
</dependencies>
31+
32+
33+
</project>
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.zjiecode.poker;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class Card implements Comparable<Card> {
7+
8+
private static Map<String, Integer> codes;
9+
10+
static {
11+
codes = new HashMap<>();
12+
codes.put("2", 2);
13+
codes.put("3", 3);
14+
codes.put("4", 4);
15+
codes.put("5", 5);
16+
codes.put("6", 6);
17+
codes.put("7", 7);
18+
codes.put("8", 8);
19+
codes.put("9", 9);
20+
codes.put("T", 10);
21+
codes.put("J", 11);
22+
codes.put("Q", 12);
23+
codes.put("K", 13);
24+
codes.put("A", 14);
25+
}
26+
27+
28+
/**
29+
* 点数
30+
*/
31+
private String name;
32+
/**
33+
* 编码
34+
*/
35+
private int code;
36+
/**
37+
* 花色
38+
*/
39+
private String type;
40+
41+
public Card(String name, String type) {
42+
this.name = name;
43+
this.code = codes.get(name);
44+
this.type = type;
45+
}
46+
47+
public String getName() {
48+
return name;
49+
}
50+
51+
public int getCode() {
52+
return code;
53+
}
54+
55+
public String getType() {
56+
return type;
57+
}
58+
59+
@Override
60+
public int compareTo(Card o) {
61+
return code - o.code;
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.zjiecode.poker;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class CardFactory {
8+
/**
9+
* 根据输入构造一副牌
10+
*/
11+
public static List<Card> createCards(String inputStr) {
12+
List<Card> cards = new ArrayList<>();
13+
String[] s = inputStr.split(" ");
14+
for (String s1 : s) {
15+
cards.add(new Card(s1.substring(0, 1), s1.substring(1)));
16+
}
17+
//排序
18+
Collections.sort(cards);
19+
return cards;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.zjiecode.poker;
2+
3+
import com.zjiecode.poker.judge.*;
4+
import com.zjiecode.poker.judge.base.IJudge;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class JudgeManager {
10+
public static final String TIE = "tie";
11+
private List<IJudge> judges = new ArrayList<>();
12+
private static JudgeManager sInstance;
13+
14+
private JudgeManager() {
15+
judges.add(new AStraightFlushJudge());
16+
judges.add(new BFourOfAKindJudge());
17+
judges.add(new CFullHouseJudge());
18+
judges.add(new DFlushJudge());
19+
judges.add(new EStraightJudge());
20+
judges.add(new FThreeOfAKindJudge());
21+
judges.add(new GTwoPairJudge());
22+
judges.add(new HOnePairJudge());
23+
judges.add(new IHighCard());
24+
}
25+
26+
public static JudgeManager getInstance() {
27+
if (sInstance == null) {
28+
sInstance = new JudgeManager();
29+
}
30+
return sInstance;
31+
}
32+
33+
/**
34+
* 裁判2个玩家的输赢
35+
*/
36+
public String judge(String player1, String player2) {
37+
List<Card> cards1 = CardFactory.createCards(player1);
38+
List<Card> cards2 = CardFactory.createCards(player2);
39+
for (IJudge judge : judges) {
40+
String result = judge.judge(cards1, cards2);
41+
if (result != null) {
42+
return result;
43+
}
44+
}
45+
//平手
46+
return TIE;
47+
}
48+
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.zjiecode.poker.judge;
2+
3+
import com.zjiecode.poker.Card;
4+
import com.zjiecode.poker.judge.base.BaseJudge;
5+
6+
import java.util.List;
7+
8+
public class AStraightFlushJudge extends BaseJudge {
9+
public static final String TYPE_NAME = "StraightFlush";
10+
11+
@Override
12+
public List<Card> getCardType(List<Card> cards) {
13+
return isStraightFlush(cards) ? cards : null;
14+
}
15+
16+
@Override
17+
public String getTypeName() {
18+
return TYPE_NAME;
19+
}
20+
21+
22+
/**
23+
* 是不是StraightFlush牌型
24+
*/
25+
private boolean isStraightFlush(List<Card> cards) {
26+
int code = cards.get(0).getCode();
27+
String type = cards.get(0).getType();
28+
for (Card card : cards) {
29+
if (card.getCode() != code || !type.equals(card.getType())) {
30+
return false;
31+
}
32+
code++;
33+
}
34+
return true;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.zjiecode.poker.judge;
2+
3+
import com.zjiecode.poker.Card;
4+
import com.zjiecode.poker.judge.base.BaseJudge;
5+
6+
import java.util.*;
7+
8+
import static java.util.stream.Collectors.groupingBy;
9+
10+
public class BFourOfAKindJudge extends BaseJudge {
11+
public static final String TYPE_NAME = "FourOfAKind";
12+
public static final int TYPE_CARD_NAME_TYPE_COUNT = 2;
13+
public static final int TYPE_CARD_COUNT_FOUR = 4;
14+
public static final int TYPE_CARD_COUNT_ONE = 1;
15+
16+
@Override
17+
public List<Card> getCardType(List<Card> cards) {
18+
Map<String, List<Card>> collect = cards.stream().collect(groupingBy(Card::getName));
19+
if (collect.size() != TYPE_CARD_NAME_TYPE_COUNT) {
20+
return null;
21+
}
22+
List<String> names = new ArrayList<>(collect.keySet());
23+
String name1 = names.get(0);
24+
String name2 = names.get(1);
25+
//一个是4张,一个是1张
26+
if ((collect.get(name1).size() == TYPE_CARD_COUNT_FOUR && collect.get(name2).size() == TYPE_CARD_COUNT_ONE)
27+
|| (collect.get(name1).size() == TYPE_CARD_COUNT_ONE && collect.get(name2).size() == TYPE_CARD_COUNT_FOUR)) {
28+
List<Card> resultCards = new ArrayList<>();
29+
resultCards.add(collect.get(name1).get(0));
30+
resultCards.add(collect.get(name2).get(0));
31+
Collections.sort(resultCards);
32+
return resultCards;
33+
}
34+
return null;
35+
}
36+
37+
@Override
38+
public String getTypeName() {
39+
return TYPE_NAME;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.zjiecode.poker.judge;
2+
3+
import com.zjiecode.poker.Card;
4+
import com.zjiecode.poker.judge.base.BaseJudge;
5+
6+
import java.util.*;
7+
8+
import static java.util.stream.Collectors.groupingBy;
9+
10+
public class CFullHouseJudge extends BaseJudge {
11+
public static final int TYPE_CARD_TYPE_COUNT = 2;
12+
public static final int TYPE_CARD_COUNT_LESS = 2;
13+
public static final int TYPE_CARD_COUNT_MORE = 3;
14+
15+
public static final String TYPE_NAME = "FullHouse";
16+
17+
@Override
18+
public List<Card> getCardType(List<Card> cards) {
19+
Map<String, List<Card>> collect = cards.stream().collect(groupingBy(Card::getName));
20+
if (collect.size() != TYPE_CARD_TYPE_COUNT) {
21+
return null;
22+
}
23+
List<String> names = new ArrayList<>(collect.keySet());
24+
String name1 = names.get(0);
25+
String name2 = names.get(1);
26+
//一个是4张,一个是1张
27+
if ((collect.get(name1).size() == TYPE_CARD_COUNT_LESS && collect.get(name2).size() == TYPE_CARD_COUNT_MORE)
28+
|| (collect.get(name1).size() == TYPE_CARD_COUNT_MORE && collect.get(name2).size() == TYPE_CARD_COUNT_LESS)) {
29+
List<Card> resultCards = new ArrayList<>();
30+
resultCards.add(collect.get(name1).get(0));
31+
resultCards.add(collect.get(name2).get(0));
32+
Collections.sort(resultCards);
33+
return resultCards;
34+
}
35+
return null;
36+
}
37+
38+
@Override
39+
public String getTypeName() {
40+
return TYPE_NAME;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.zjiecode.poker.judge;
2+
3+
import com.zjiecode.poker.Card;
4+
import com.zjiecode.poker.judge.base.BaseJudge;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import static java.util.stream.Collectors.groupingBy;
10+
11+
public class DFlushJudge extends BaseJudge {
12+
public static final String TYPE_NAME = "Flush";
13+
private static final int TYPE_CARD_TYPE_COUNT = 1;
14+
15+
@Override
16+
public List<Card> getCardType(List<Card> cards) {
17+
Map<String, List<Card>> collect = cards.stream().collect(groupingBy(Card::getType));
18+
//只有一种花色
19+
if (collect.size() == TYPE_CARD_TYPE_COUNT) {
20+
return cards;
21+
}
22+
return null;
23+
}
24+
25+
@Override
26+
public String getTypeName() {
27+
return TYPE_NAME;
28+
}
29+
}

0 commit comments

Comments
 (0)