栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > .Net

java斗地主发牌代码

.Net 更新时间:发布时间: 百科书网 趣学号

本文实例为大家分享了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 {

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));

}

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);

}

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;

}

}

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

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/985022.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号