简易斗地主发牌器

/**
 * 1、生成54张牌(数字、花色)
 * 2、打乱54张牌
 *    随机1个下标、获取牌
 * 3、发牌
 * 4、排序
 */
//获取随机数
function getNum(min, max = 0) {
    min > max ? [min, max] = [max, min] : "";
    return parseInt(Math.random() * (max - min + 1)) + min;
}

//获取标签样式
function getNodeStyle(node, temp = null) {
    if (node.currentStyle) {
        return node.currentStyle;
    } else {
        return window.getComputedStyle(node, temp);
    }
}

//生成54张牌
function genPoker() {
    //基础数据
    let pokerNum = [3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A", 2];
    let pokerType = ["♦️", "♣️", "♥️", "♠️"];
    let pokerKing = ["小王", "大王"];
    let poker = [];
    //遍历数字与花色生成54张牌
    pokerNum.map((item1) => {
        pokerType.map((item2) => {
            poker.push(item1 + item2);
        })
    });
    poker.push(...pokerKing);
    return poker;
}

//生成乱序的1-54的数字(打乱54张牌)
function xipai() {
    let set = new Set();
    while (true) {
        set.add(getNum(0, 53));
        if (set.size == 54) {
            return [...set];
        }
    }
}

//给用户发牌(已排序)
function fapai(poker, pokerIndex) {
    let arr = [];
    //排序
    pokerIndex.sort((a, b) => a - b);
    pokerIndex.map(item => {
        arr.push(poker[item]);
    });
    return arr;
}

//主程序
function main() {
    let poker = genPoker();
    let pokerIndex = xipai();
    //获取玩家和底牌节点
    let divEles = document.querySelectorAll("div");
    let pEle = document.querySelector("p");
    //给节点添加内容
    [...divEles].map((item, index) => {
        item.innerHTML = `Play${index+1}:` + fapai(poker, pokerIndex.splice(0, 17)).join(",");
    });
    pEle.innerHTML = "底牌:" + fapai(poker, pokerIndex).join(",");
}

main();
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>斗地主发牌器</title>
</head>

<body>
    <div class="play1"></div>
    <div class="play2"></div>
    <div class="play3"></div>
    <p class="dipai"></p>

    <script src="../tools.js"></script>
    <script src="./index.js"></script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值