Java简单实现斗地主洗牌、发牌

本文摘自:https://funyan.cn/p/403.html

需求分析

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

具体规则:

使用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<String> 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<String> zhangsan=new ArrayList<>();
      ArrayList<String> lisi=new ArrayList<>();
      ArrayList<String> wangwu=new ArrayList<>();
      ArrayList<String> 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]

本文摘自:https://funyan.cn/p/403.html

  • 8
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值