击鼓传花击鼓次数相同c语言,JavaScript 实现击鼓传花游戏

大家小时候应该都玩过击鼓传花(Hot Potato)的游戏吧!

一群小孩围成一个圆圈,把花尽快的传给旁边的人。某一时刻传花停止,这时花在谁手里,谁就退出圆圈结束游戏。重复此过程,直到剩下最后一个孩子,即为胜者。

如何用 JavaScript 代码按此规则实现击鼓传花呢?

这里需要借助 JavaScript 中队列的知识,利用循环队列来实现。

6d99ffa9d44a

队列

首先,我们创建一个 queue.js 的文件存储 Queue 类,代码如下:

export default class Queue {

constructor() {

this.count = 0;

this.lowestCount = 0;

this.items = {};

}

enqueue(element) {

this.items[this.count] = element;

this.count++;

}

dequeue() {

if (this.isEmpty()) {

return undefined;

}

const result = this.items[this.lowestCount];

delete this.items[this.lowestCount];

this.lowestCount++;

return result;

}

isEmpty() {

return this.size() === 0;

}

clear() {

this.items = {};

this.count = 0;

this.lowestCount = 0;

}

size() {

return this.count - this.lowestCount;

}

toString() {

if (this.isEmpty()) {

return '';

}

let objString = `${this.items[this.lowestCount]}`;

for (let i = this.lowestCount + 1; i < this.count; i++) {

objString = `${objString},${this.items[i]}`;

}

return objString;

}

}

其次,根据击鼓传花的规则和循环队列的知识,组织代码:

/**

* @param {Array:参加游戏的人员} elementsList

* @param {number:每次击鼓的次数} num

*/

function hotPotato(elementsList, num) {

// 引入 queue.js 创建 Queue 实例

const queue = new Queue();

const elimitatedList = [];

for (let i = 0; i < elementsList.length; i++) {

// 把参加的人列入队列中

queue.enqueue(elementsList[i]);

}

while (queue.size() > 1) {

for (let i = 0; i < num; i++) {

// 把队列的第一个人放入队尾循环队列

queue.enqueue(queue.dequeue());

}

// 把淘汰的人放入 elimitatedList 数组中

elimitatedList.push(queue.dequeue());

}

return {

eliminated: elimitatedList, // 淘汰的人

winner: queue.dequeue() // 胜利者

};

}

// test

const names = ['丁一', '龙二', '张三', '李四', '王五', '赵六'];

const result = hotPotato(names, 7);

result.eliminated.forEach(name => {

console.log(`${name} was eliminated from the Hot Potato game.`);

});

console.log(`The winner is: ${result.winner}`);

// 龙二 was eliminated from the Hot Potato game.

// 王五 was eliminated from the Hot Potato game.

// 李四 was eliminated from the Hot Potato game.

// 丁一 was eliminated from the Hot Potato game.

// 赵六 was eliminated from the Hot Potato game.

// The winner is: 张三

输出的结果依次为被淘汰的选手和最后的胜利者,这个方法有个弊病:每次淘汰选手敲得鼓声都是一样,导致淘汰的选手和胜利者能预测到。一般我们游戏的时候敲的鼓声都是不确定的,那如何做到不确定,达到一定的游戏氛围呢?大家集思广益吧!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值