java斗地主发牌代码_java代码实现斗地主发牌功能

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

实现斗地主发牌功能

共54张牌,地主比其他两名玩家多三张牌。

有一个card牌类和player玩家类,还有一个发牌类用于实现发牌的方法。

为了模拟每个玩家的牌都是随机的,我是这样想的:

1)初始化方法:用于将54张牌存到一个数组里,每张牌都一个唯一的序号。

2) 利用随机数,将每个序号打乱存到一个新数组里。

3)再根据序号取到初始化牌库数组内的牌,存到每个玩家的牌集合内。

附一个在老师指导下写的:斗地主发牌功能,自己还是有些没考虑周到。/_ \

代码如下:

牌类

public class Card {

/**花色*/

private String HuaSe;

/**点数*/

private String DianShu;

/**序号*/

private int XuHao;

public Card(String huaSe, String dianShu, int xuHao) {

super();

HuaSe = huaSe;

DianShu = dianShu;

XuHao = xuHao;

}

public String getHuaSe() {

return HuaSe;

}

public void setHuaSe(String huaSe) {

HuaSe = huaSe;

}

public String getDianShu() {

return DianShu;

}

public void setDianShu(String dianShu) {

DianShu = dianShu;

}

public int getXuHao() {

return XuHao;

}

public void setXuHao(int xuHao) {

XuHao = xuHao;

}

@Override

public String toString() {

return "[" + HuaSe + DianShu + "]";

}

}

玩家类

public class Player {

/**玩家id*/

private int id;

/**玩家姓名*/

private String name;

/**是否是地主*/

private boolean dizhu;

/**牌的集合*/

private ArrayList list;

public Player(int id, String name, boolean dizhu) {

super();

this.id = id;

this.name = name;

this.dizhu = dizhu;

}

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 boolean isDizhu() {

return dizhu;

}

public void setDizhu(boolean dizhu) {

this.dizhu = dizhu;

}

public ArrayList getList() {

return list;

}

public void setList(ArrayList list) {

this.list = list;

}

@Override

public String toString() {

return name + ", 牌:" + list ;

}

}

发牌类: 这里还有许多缺陷 :例如地主是需要自己指定而不是随机的,在给每个人发牌时,可以利用remove()方法将已经发过的牌移除,这样可以节省很多重复代码。

public class SendCard {

static ArrayList arrayList = new ArrayList();

Random r = new Random();

/**

* 初始话牌库

*/

public void init() {

for (int i = 1; i < 14; i++) {

arrayList.add(new Card("梅花", Integer.toString(i), i));

arrayList.add(new Card("方块",Integer.toString(i),13 + i));

arrayList.add(new Card("红心", Integer.toString(i), 26 + i));

arrayList.add(new Card("黑桃", Integer.toString(i), 39 + i));

}

arrayList.add(new Card("","大王",53));

arrayList.add(new Card("", "小王", 54));

}

/**

* 发牌(默认p3为地主)

* @param p1

* @param p2

* @param p3

*/

public void send(Player p1,Player p2,Player p3) {

ArrayList intList = new ArrayList();

intList = fenpei(intList);

//给p1发牌

ArrayList clist = new ArrayList();

for (int i = 0; i < 17; i++) {

clist.add(arrayList.get(intList.get(i)));

}

p1.setList(clist);

//给p2发牌

clist = new ArrayList();

for (int i = 17; i < 34; i++) {

clist.add(arrayList.get(intList.get(i)));

}

p2.setList(clist);

//给p3发牌

clist = new ArrayList();

for (int i = 34; i < 54; i++) {

clist.add(arrayList.get(intList.get(i)));

}

p3.setList(clist);

}

/**

* 将初始化牌库打乱后存入新数组

* @param list

* @return

*/

public ArrayList fenpei(ArrayList list) {

int index = 0;

while (true) {

int i = r.nextInt(54);

for (Integer integer : list) {

if (integer == i) {

index = 1;

break;

}

index = 0;

}

if(index == 0)

list.add(i);

if(list.size() == 54)

break;

}

return list;

}

}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值