斗地主是全国范围内的一种桌面游戏,尽管全国各种类型,但大同小异,今天我们先来实现一下斗地主中的发牌功能,为了程序的可读性以及可拓展性,此次我们使用面向对象的方法来实现此功能。
首先,斗地主要求有扑克和玩家才能开始游戏,所以这里我们首先要创建一个扑克牌实体类和一个玩家实体类,具体代码如下:
首先是Poker类,含点数,花色和排序属性,大小王之后额外添加,这里我们必须实现一个Comparable接口,方便之后进行排序:
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
|
public class Poker implements Comparable<Poker>{ private String flower; private String pointer; private int sort; public Poker() { } 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; } @Override public int compareTo(Poker a) { return this .getSort() - a.getSort(); } } |
然后就是Player类,这里我们定义属性有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
|
import java.util.ArrayList; public class Player { private int id; private String name; private ArrayList<Poker> pokers = new ArrayList<>(); private boolean boss; public Player() { } 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; } } |
实体类建完之后,我们就要开始实现功能了,首先我们将要实现的功能简单叙述一下,首先我们要求有一副54张的完整牌,然后洗牌(此处我们用Collections里的静态shuffle方法实现),然后开始按洗好的牌发牌51张,发完随机地主,将剩余的三张牌给地主,并将三名玩家的牌均按大小显示出来。
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
|
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 pushBoss(){ /**随机一个索引,随到的为地主*/ Random r = new Random(); bossIndex = r.nextInt(players.size()); players.get(bossIndex).setBoss( true ); } /**洗牌功能*/ public void XiPai(){ /**用Collections中的一个静态方法实现随机顺序集合*/ Collections.shuffle(list); } /**发牌功能*/ public void FaPai(){ /**每次按顺序给每人发一张牌直到51张牌发完*/ 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 j = 51 ;j < 54 ;j++){ players.get(bossIndex).getPokers().add(list.get(j)); } } /**排序功能*/ public void PaiXu(){ /**用Collections的sort方法实现排序功能*/ Collections.sort(players.get( 0 ).getPokers()); Collections.sort(players.get( 1 ).getPokers()); Collections.sort(players.get( 2 ).getPokers()); } /**开始游戏*/ public void start(){ pushBoss(); XiPai(); FaPai(); PaiXu(); for (Player p:players){ System.out.println(p.getPokers()); } } } |
下面我们来看看具体实现:
1
2
3
4
5
6
7
|
public class Test { public static void main(String[] args) { new PokerGame().start(); } } |
结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_45416065/article/details/97431378