js使用队列实现击鼓传花小游戏

文章结构

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>
<h1>题目描述</h1>
<h4>
    击鼓传花游戏介绍,循环队列模拟一个圆,队列前面的人出列再入列,倒计时结束,此时在队列第一位的被淘汰,当队伍中,只剩下一人时,游戏结束
</h4>
<h1>解题思路</h1>
<h4>循环队列长度大于1时,一直将队列头的人移到队列末尾,用number模拟时间,假设(number为2,则当执行2次队列头移到队列尾操作时,将队列头的人淘汰)</h4>
<h1>尝试使用</h1>
请输入参数的人员名字(用逗号隔开,如小红,小蓝,小美)
<input type="text" id="input1" />
<br /> 请输入时间(大于0的整数)
<input type="text" id="input2" />
<br>
<button id="btn">游戏开始</button>
<br /> 最终胜利的玩家
<input type="text" id="output" />
<br />
<h1>代码实现</h1>
<pre>
    <code>
        const main = function(names, number) {
            // 时间 number
            // 游戏参与的人员 names
            const queue = new Queue();
            // 人员入队
            for (let k in names) {
                queue.queueIn(names[k]);
            }
            // 游戏开始
            while (queue.size() > 1) {
                for (let i = 0; i < number; i++) {
                    queue.queueIn(queue.queueOut());
                }
                alert(queue.queueOut() + '被淘汰了')
            }
            alert(queue.peek() + '胜利了')
            return queue.peek();
        }
        document.getElementById("btn").addEventListener("click", function() {
            // 字符串转数组
            const inputValue1 = document.getElementById("input1").value.split(",");
            // 字符型转数值型
            const inputValue2 = document.getElementById("input2").value - 0;
            const outputValue = main(inputValue1, inputValue2);
            document.getElementById("output").value = outputValue;
        });
    </code>
</pre>

<body>
    <script src="../../Queue.js"></script>
    <script>
        const main = function(names, number) {
            // 时间 number
            // 游戏参与的人员 names
            const queue = new Queue();
            // 人员入队
            for (let k in names) {
                queue.queueIn(names[k]);
            }
            // 游戏开始
            while (queue.size() > 1) {
                for (let i = 0; i < number; i++) {
                    queue.queueIn(queue.queueOut());
                }
                alert(queue.queueOut() + '被淘汰了')
            }
            alert(queue.peek() + '胜利了')
            return queue.peek();
        }
        document.getElementById("btn").addEventListener("click", function() {
            // 字符串转数组
            const inputValue1 = document.getElementById("input1").value.split(",");
            // 字符型转数值型
            const inputValue2 = document.getElementById("input2").value - 0;
            const outputValue = main(inputValue1, inputValue2);
            document.getElementById("output").value = outputValue;
        });
    </script>
</body>

</html>

Queue.js

// 队列
const Queue =  function(item) {
    this.item = item ? item : [];

    // 入列
    this.__proto__.queueIn = function(element) {
        this.item.push(element);
    };

    // 出列
    this.__proto__.queueOut = function() {
        return this.item.shift();
    };

    // 查看队列头
    this.__proto__.peek = function() {
        return this.item[0];
    };

    // 检查队列是否为空
    this.__proto__.isEmpty = function() {
        return this.item.length === 0;
    };

    // 返回队列大小
    this.__proto__.size = function() {
        return this.item.length;
    };

    // 查看整个队列
    this.__proto__.getQueue = function() {
        return this.item;
    };
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LiuJie_Boom

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值