java 实现斗地主发牌_Java简单实现斗地主洗牌、发牌-Fun言

需求分析

按照斗地主的规则,完成洗牌发牌的动作。

具体规则:

使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。

代码实现import java.util.ArrayList;

import java.util.Collection;

import java.util.Collections;

import java.util.Iterator;

/**

*

**/

public class Poker

{

public static void main(String[] args) {

//准备牌,54张牌,其中两张大小王,其他为不同花色的牌

//一盒牌的集合

ArrayList PokerBox=new ArrayList<>();

//把大小王加进去

PokerBox.add("大王");

PokerBox.add("小王");

//将其他牌加进来

//定义两个数组

String[] colors={"♥","♠","♦","♣"};

String[] num={"A","2","K","Q","J","10","9","8","7","6","5","4","3"};

for (int i = 0; i < num.length; i++) {

for (int j = 0; j < colors.length; j++) {

PokerBox.add(colors[j]+num[i]);

}

}

//洗牌

Collections.shuffle(PokerBox);

//发牌

//发51张牌,剩余当做底牌不发

//新建四个集合,用于接收牌

ArrayList zhangsan=new ArrayList<>();

ArrayList lisi=new ArrayList<>();

ArrayList wangwu=new ArrayList<>();

ArrayList bottomPoker=new ArrayList<>();

for (int i = 0; i < PokerBox.size(); i++) {

if(i<51){

//正常发牌

switch (i%3){

case 0: zhangsan.add(PokerBox.get(i));break;

case 1: lisi.add(PokerBox.get(i));break;

case 2: wangwu.add(PokerBox.get(i));break;

}

}else{

bottomPoker.add(PokerBox.get(i));

}

}

//看牌

System.out.println("张三的牌:"+zhangsan);

System.out.println("李四的牌:"+lisi);

System.out.println("王五的牌:"+wangwu);

System.out.println("底牌:"+bottomPoker);

}

}

结果张三的牌:[♣6, ♠2, ♣10, ♥10, ♠3, ♠5, ♥7, ♥J, ♦9, ♠J, ♦10, ♥A, ♠10, ♥Q, ♣9, ♣K, ♠Q]

李四的牌:[♥9, ♦K, ♥8, ♠4, ♦Q, ♥2, ♦7, ♦2, ♦J, ♠A, ♦8, ♥4, 小王, ♥3, ♠9, ♣8, ♥6]

王五的牌:[♣Q, ♣3, ♣A, ♦A, ♠K, ♦4, ♠6, ♣J, ♦5, ♥5, ♦3, ♣4, ♦6, ♥K, 大王, ♣2, ♣5]

底牌:[♣7, ♠7, ♠8]

你还没有登录,请先使用 QQ登录 或 注册!

文章评论

发表评论

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值