利用for循环模拟斗地主洗牌及发牌过程

斗地主游戏发牌模拟
本篇博客介绍了一个简单的斗地主游戏发牌过程模拟程序。通过Java代码实现了扑克牌的创建、洗牌和发牌流程,并展示了具体的实现细节。
package Day02

import java.util.Random;

//模拟斗地主洗牌,发牌过程
public class Demo03 {
    public static void main(String[] args) {
        //创建一幅扑克牌
        String[] cards = new String[54];
        String[] rank = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        for (int i = 0; i < rank.length; i++) {
            cards[i] = "红桃" + rank[i];
            cards[i + 13] = "黑桃" + rank[i];
            cards[i + 13 + 13] = "方块" + rank[i];
            cards[i + 13 + 13 + 13] = "梅花" + rank[i];
        }
        cards[52] = "King";
        cards[53] = "smallKing";
        for (int i = 0; i < cards.length; i++) {
            System.out.print(cards[i] + " ");
            if ((i + 1) % 13 == 0) {
                System.out.println();
            }
        }
        System.out.println();
        System.out.println();
        //洗牌
        Random random = new Random();
        for (int i = cards.length - 1; i >= 1; i--) {
            int j = random.nextInt(i);
            String t = cards[i];
            cards[i] = cards[j];
            cards[j] = t;
        }
        for (int i = 0; i < cards.length; i++) {
            System.out.print(cards[i] + " ");
            if ((i + 1) % 13 == 0) {
                System.out.println();
            }
        }
        System.out.println();
        System.out.println();
        //发牌
        String[] lee=new String[17];
        String[] wang=new String[17];
        String[] zhao=new String[17];
        for (int i=0;i<17;i++){
            lee[i]=cards[i*3];
            wang[i]=cards[i*3+1];
            zhao[i]=cards[i*3+2];
        }
        for (int i=0;i<lee.length;i++){
            System.out.print(lee[i]+" ");
        }  System.out.println();
        for (int i=0;i<wang.length;i++){
            System.out.print(wang[i]+" ");
        } System.out.println();
        for (int i=0;i<zhao.length;i++){
            System.out.print(zhao[i]+" ");
        }
        System.out.println();
        System.out.println(cards[51]+" "+cards[52]+" "+cards[53]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值