java实现斗地主小案例

2025-05-29 0 105

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

斗地主案例

按照斗地主的规则,完成洗牌发牌的动作。
具体规则: 使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,后三张留作底牌

具体操作如下

1、准备牌:

完成数字与纸牌的映射关系:
使用双列map(hashmap)集合,完成一个数字与字符串纸牌的对应关系(相当于一个字典)。

2、洗牌:

通过数字完成洗牌发牌

3、发牌:

将每个人以及底牌设计为arraylist,将后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌。
存放的过程中要求数字大小与斗地主规则的大小对应。
将代表不同纸牌的数字分配给不同的玩家与底牌。

4、看牌: 通过map集合找到对应字符展示。

通过查询纸牌与数字的对应关系,由数字转成纸牌字符串再进行展示。

?

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

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137
/**

*斗地主案例

* @program: practice_masaike

* @author: csl

* @create: 2021-02-23 16:02

**/

/**

*步骤如下

*1.准备牌

*2.洗牌

*3.发牌

*4.排序

*5.看牌

**/

public class poker {

public static void main(string[] args) {

//1.准备牌

//创建一个map集合,存储牌的索引和组装好的牌

hashmap<integer,string> poker= new hashmap<>();

//创建一个list集合,存储牌的的索引

arraylist<integer> pokerindex= new arraylist<>();

//定义连个集合 存储牌的花色和牌的序号

list<string> colors = new arraylist<string>();

list<string> numbers = new arraylist<string>();

//list<string> colors= array.aslist("♣","♦", "♥", "♠");

//list<string> numbers= list.of("2", "a", "k", "q", "j", "10", "9", "8", "7", "6", "5", "4", "3");

/**

* collections集合的方法

* public static <t> boolean addall(collection<t> c, t... elements) `:往集合中添加一些元素。

**/

collections.addall(colors,"♣","♦", "♥", "♠");

collections.addall(numbers,"2", "a", "k", "q", "j", "10", "9", "8", "7", "6", "5", "4", "3");

//把大王和小王存储到集合中

//定义一个牌的索引

int index=0;

poker.put(index,"大王");

pokerindex.add(index);

index++; //1

poker.put(index,"小王");

pokerindex.add(index);

index++; //2

//循环嵌套遍历两个集合,组合52张牌,存储到集合中

for(string number : numbers){

for(string color : colors){

//重点注意 map集合poker的key为index

poker.put(index,color+number);

pokerindex.add(index);

index++; //3

}

}

// system.out.println(poker);

// system.out.println(pokerindex);

/**

* 2.洗牌

* 使用collections中的方法shuffle(list)

**/

collections.shuffle(pokerindex);

//system.out.println(pokerindex);

/**

* 进行发牌

**/

//需要定义四个集合,存储玩家牌的索引和底牌的索引

arraylist<integer> play01 = new arraylist<>();

arraylist<integer> play02 = new arraylist<>();

arraylist<integer> play03 = new arraylist<>();

//底牌集合

arraylist<integer> dipai = new arraylist<>();

/**

* 遍历存储牌索引的list集合,获取每一个牌的索引

**/

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

integer in=pokerindex.get(i);

//先判断底牌

if (i >= 51) {

//给底牌发牌

dipai.add(in);

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

//给玩家1发牌

play01.add(in);

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

//给玩家1发牌

play02.add(in);

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

//给玩家1发牌

play03.add(in);

}

}

/**

* 4.进行牌的排序

* 使用collectiond中的方法sort(list) 默认是升序排序

**/

collections.sort(play01);

collections.sort(play02);

collections.sort(play03);

collections.sort(dipai);

/**

* 5.看牌

* 调用看牌的方法

**/

lookpoker("张三",poker,play01);

lookpoker("李四",poker,play02);

lookpoker("王五",poker,play03);

lookpoker("底牌",poker,dipai);

}

/**

* 定义一个看牌的方法,提高代码的复用性

* 参数

* string name:玩家名称

* hashmap<integer,string> poker:存储牌的poker集合

* arraylist<integer> pokerindex:存储玩家和底牌的list集合

*

* 查表发:

* 遍历玩家或者底牌集合,获取牌的索引

* 使用牌的索引,去map集合中找到对对那个的牌

**/

public static void lookpoker(string name,hashmap<integer,string> poker,arraylist<integer> list){

//输出玩家的名称

system.out.print(name+": ");

for(integer key : list){

//使用牌的索引,去map集合中找到对对那个的牌

string value=poker.get(key);

system.out.print(value+": ");

}

//打印完每一个玩家的牌后,进行换行操作

system.out.println();

}

}

第一次洗牌的结果

java实现斗地主小案例

第二次洗牌的结果

java实现斗地主小案例

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

原文链接:https://blog.csdn.net/Liamcsl/article/details/113998154

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 java实现斗地主小案例 https://www.kuaiidc.com/108459.html

相关文章

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