求助:用java实现的纸牌游戏
答案:2 悬赏:80 手机版
解决时间 2021-02-15 19:23
- 提问者网友:嘚啵嘚啵
- 2021-02-14 20:28
求助:用java实现的纸牌游戏
最佳答案
- 五星知识达人网友:鱼芗
- 2021-02-14 21:36
用一个list放这52个元素,每个人随机一个数作为list下标发牌,发掉的牌就从list中remove掉
小写了个来耍:
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
for (int i = 1; i <= 13; i++) {//生成牌堆
list.add("红心" + i);
list.add("黑桃" + i);
list.add("梅花" + i);
list.add("方块" + i);
}
ArrayList<String> lista = new ArrayList<String>();//生成牌友
ArrayList<String> listb = new ArrayList<String>();
ArrayList<String> listc = new ArrayList<String>();
ArrayList<String> listd = new ArrayList<String>();
while (list.size() > 1) {
lista.add(find(list));
listb.add(find(list));
listc.add(find(list));
listd.add(find(list));
}
System.out.println("甲:" + lista.toString());
System.out.println("乙:" + listb.toString());
System.out.println("丙:" + listc.toString());
System.out.println("丁:" + listd.toString());
}
public static String find(ArrayList<String> list) {
Random random = new Random();
int m = list.size() - 1;//随机数范围为0到剩余牌数量-1
if (m == 0)//对最后一张牌处理
return list.get(0);
int index = Math.abs(random.nextInt()) % m;
String str = list.get(index);
list.remove(index);
return str;
}
小写了个来耍:
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
for (int i = 1; i <= 13; i++) {//生成牌堆
list.add("红心" + i);
list.add("黑桃" + i);
list.add("梅花" + i);
list.add("方块" + i);
}
ArrayList<String> lista = new ArrayList<String>();//生成牌友
ArrayList<String> listb = new ArrayList<String>();
ArrayList<String> listc = new ArrayList<String>();
ArrayList<String> listd = new ArrayList<String>();
while (list.size() > 1) {
lista.add(find(list));
listb.add(find(list));
listc.add(find(list));
listd.add(find(list));
}
System.out.println("甲:" + lista.toString());
System.out.println("乙:" + listb.toString());
System.out.println("丙:" + listc.toString());
System.out.println("丁:" + listd.toString());
}
public static String find(ArrayList<String> list) {
Random random = new Random();
int m = list.size() - 1;//随机数范围为0到剩余牌数量-1
if (m == 0)//对最后一张牌处理
return list.get(0);
int index = Math.abs(random.nextInt()) % m;
String str = list.get(index);
list.remove(index);
return str;
}
全部回答
- 1楼网友:千杯敬自由
- 2021-02-14 21:48
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯