java使用HashMap实现斗地主(有序版)

2025-05-29 0 63

本文实例为大家分享了java使用HashMap实现斗地主的具体代码,供大家参考,具体内容如下

案例介绍

按照斗地主的规则,完成洗牌发牌的动作。 具体规则:

使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。

案例分析

1、准备牌:

每张扑克牌牌由花色和数字两部分组成。可以使用花色集合与数字集合嵌套迭代完成扑克牌的组装。

2、发牌

扑克牌组转完毕后由Collections类的shuffle方法打乱重排,最后3张当作底牌,剩余牌通过对3取模依次发牌。

3、看牌

打印集合。

代码演示

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118
import java.util.*;

/**

* @author layman

*/

public class Poker2 {

// 牌堆

private static Map<Integer, String> pokerMap = new HashMap<>();

// 花色

private static ArrayList<String> colors = new ArrayList<>();

// 数字

private static ArrayList<String> numbers = new ArrayList<>();

// 扑克牌的编号集合

private static ArrayList<Integer> numberList = new ArrayList<>();

// 玩家编号集合

private static ArrayList<Integer> noP1 = new ArrayList<>();

private static ArrayList<Integer> noP2 = new ArrayList<>();

private static ArrayList<Integer> noP3 = new ArrayList<>();

// 底牌编号集合

private static ArrayList<Integer> diPaiNo = new ArrayList<>();

// 三个玩家

private static ArrayList<String> playerOne = new ArrayList<String>();

private static ArrayList<String> playerTwo = new ArrayList<String>();

private static ArrayList<String> playerThree = new ArrayList<String>();

// 底牌

private static ArrayList<String> diPai = new ArrayList<String>();

/**

* 创建扑克牌并洗牌

*/

public static void createPoker(){

Collections.addAll(colors, "♦", "♣", "♥", "♠");

Collections.addAll(numbers, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");

// 设置存储编号

int count = 1;

pokerMap.put(count++, "大王");

pokerMap.put(count++, "小王");

// 创建扑克牌

for (String number : numbers) {

for (String color : colors) {

String card = color + number;

pokerMap.put(count++, card);

}

}

// 先取编号

Set<Integer> numberSet = pokerMap.keySet();

numberList.addAll(numberSet);

// 然后随机洗牌

Collections.shuffle(numberList);

}

/**

* 发牌

*/

public static void faPai(){

for (int i = 0; i < numberList.size(); i++) {

Integer no = numberList.get(i);

// 留出底牌

if (i >= 51) {

diPaiNo.add(no);

} else {

if (i % 3 == 0) {

noP1.add(no);

} else if (i % 3 == 1) {

noP2.add(no);

} else {

noP3.add(no);

}

}

}

}

/**

* 发牌并排序

*/

public static void sortCards(){

// 对编号进行排序

Collections.sort(noP1);

Collections.sort(noP2);

Collections.sort(noP3);

Collections.sort(diPaiNo);

// 进行牌面的转换

for (Integer i : noP1) {

// 根据编号获取牌面,并发给对应的玩家

String card = pokerMap.get(i);

playerOne.add(card);

}

for (Integer i : noP2) {

String card = pokerMap.get(i);

playerTwo.add(card);

}

for (Integer i : noP3) {

String card = pokerMap.get(i);

playerThree.add(card);

}

for (Integer i : diPaiNo) {

String card = pokerMap.get(i);

diPai.add(card);

}

}

/**

* 看牌

*/

public static void showCards(){

System.out.println("赌圣:" + playerOne);

System.out.println("赌侠:" + playerTwo);

System.out.println("赌王:" + playerThree);

System.out.println("底牌:" + diPai);

}

public static void main(String[] args) {

createPoker();

faPai();

sortCards();

showCards();

}

}

补充:

使用ArrayList实现斗地主案例(无序版):传送门

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/single_0910/article/details/114242124

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java使用HashMap实现斗地主(有序版) https://www.kuaiidc.com/108475.html

相关文章

猜你喜欢
发表评论
暂无评论