这篇文章主要介绍了Java如何实现斗地主的发牌功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。斗地主是全国范围内的一种桌面游戏,尽管全国各种类型,但大同小异,今天我们先来实
这篇文章主要介绍了Java如何实现斗地主的发牌功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
斗地主是全国范围内的一种桌面游戏,尽管全国各种类型,但大同小异,今天我们先来实现一下斗地主中的发牌功能,为了程序的可读性以及可拓展性,此次我们使用面向对象的方法来实现此功能。
首先,斗地主要求有扑克和玩家才能开始游戏,所以这里我们首先要创建一个扑克牌实体类和一个玩家实体类,具体代码如下:
首先是Poker类,含点数,花色和排序属性,大小王之后额外添加,这里我们必须实现一个Comparable接口,方便之后进行排序:
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,玩家名字以及玩家手牌和是否地主:
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张,发完随机地主,将剩余的三张牌给地主,并将三名玩家的牌均按大小显示出来。
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.shuffle(list); } public void FaPai(){ 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(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()); } }}
下面我们来看看具体实现:
public class Test { public static void main(String[] args) { new PokerGame().start(); } }
结果如下:
感谢你能够认真阅读完这篇文章,希望小编分享的“Java如何实现斗地主的发牌功能”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!
--结束END--
本文标题: Java如何实现斗地主的发牌功能
本文链接: https://www.lsjlt.com/news/296593.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0