本文实例为大家分享了java简单实现斗地主发牌的具体代码,供大家参考,具体内容如下
问题:
参考斗地主的游戏规则,完成一个发牌的功能(54张牌,考虑点数,花色;三名玩家,其中地主比其他玩家多3张牌)
要求实现以下功能:
1、洗牌
2、发牌(发牌按照常规方式A-B-C-A-B-C-A…,不允许一次性随机17张给某个玩家)
3、玩家手中牌的展示(要求按点数实现排序)
分析
运用面向对象的程序设计思维分为扑克、玩家和游戏三部分,扑克类属性有花色,点数,次序;玩家类属性有玩家id,昵称,是否地主,牌的集合;游戏过程分为洗牌,随机产生地主,发牌,完成牌的排序,玩家手里的牌展示功能
程序实现
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
package com.lesson17.homework;
/**
* @author HUC
*
*/
/**扑克类*/
public class Poker {
/** 扑克花色 */
private String flower;
/** 扑克点数 */
private String pointer;
/** 扑克顺序 */
private int sort;
public Poker(String flower, String pointer, int sort) {
super ();
this .flower = flower;
this .pointer = pointer;
this .sort = sort;
}
public String getFlower() {
return flower;
}
public void setFlower(String flower) {
this .flower = flower;
}
public String getPointer() {
return pointer;
}
public void setPointer(String pointer) {
this .pointer = pointer;
}
public int getSort() {
return sort;
}
public void setSort( int sort) {
this .sort = sort;
}
@Override
public String toString() {
return flower + pointer;
}
package com.lesson17.homework;
public class Player {
/** 玩家id */
private int id;
/** 玩家昵称 */
private String name;
/** 玩家拿到的牌 */
private ArrayList<Poker> pokers = new ArrayList<>();
/** 玩家是否地主 */
private boolean boss;
public Player( int id, String name) {
super ();
this .id = id;
this .name = name;
}
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public ArrayList<Poker> getPokers() {
return pokers;
}
public void setPokers(ArrayList<Poker> pokers) {
this .pokers = pokers;
}
public boolean isBoss() {
return boss;
}
public void setBoss( boolean boss) {
this .boss = boss;
}
package com.lesson17.homework;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class PokerGame {
static List<Player> players;
static ArrayList<Poker> list = new ArrayList<>();
static String[] flowers = { "方块" , "梅花" , "黑桃" , "红心" };
static String[] pointers = { "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" };
int bossIndex; //地主索引
static {
//初始化扑克
for ( int i = 0 ; i < flowers.length; i++) {
for ( int j = 0 ; j < pointers.length; j++) {
//生成一张扑克对象
Poker p = new Poker(flowers[i], pointers[j], j);
list.add(p);
}
}
//单独将大小王加入集合
list.add( new Poker( "小" , "王" , 13 ));
list.add( new Poker( "大" , "王" , 14 ));
//初始化玩家
players = Arrays.asList(
new Player( 1 , "刘备" ),
new Player( 2 , "关羽" ),
new Player( 3 , "张飞" ));
}
/**洗牌*/
public void washPoker(){
Collections.shuffle(list);
}
/**随机地主*/
public void pushBoss(){
Random r = new Random();
bossIndex = r.nextInt(players.size());
//设置被随机到的玩家为地主
players.get(bossIndex).setBoss( true );
}
/**依次发牌*/
public void pushPoker(){
for ( int i = 0 ; i < 51 ; i+= 3 ) {
players.get( 0 ).getPokers().add(list.get(i));
players.get( 1 ).getPokers().add(list.get(i+ 1 ));
players.get( 2 ).getPokers().add(list.get(i+ 2 ));
}
for ( int i = 51 ;i < 54 ;i++){
//将最后剩余的牌(3张)全部给地主
players.get(bossIndex).getPokers().add(list.get(i));
}
}
/**排序*/
public void sortPoker() {
Collections.sort(players.get( 0 ).getPokers(), (a,b)->a.getSort()-b.getSort());
Collections.sort(players.get( 1 ).getPokers(), (a,b)->a.getSort()-b.getSort());
Collections.sort(players.get( 2 ).getPokers(), (a,b)->a.getSort()-b.getSort());
}
public void start(){
//随机地主
pushBoss();
//洗牌
washPoker();
//发牌
pushPoker();
//排序
sortPoker();
//展示牌
for (Player p : players){
System.out.println(p.getPokers());
}
}
public static void main(String[] args) {
new PokerGame().start();
}
}
|
结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_35937045/article/details/97620335