普通卡池a,b,c三种权重为1500,1200,1000,数量无限,稀有卡池a,b,c三种权重为400,300,300,数量为4,3,3,传说卡池a,b,c权重为100,100,100,数量 1,1,1用js语言写出抽卡方法
时间: 2024-05-16 17:16:17 浏览: 4
以下是一个简单的抽卡方法,使用 JavaScript 语言:
```javascript
// 普通卡池的权重和数量
const normalWeights = [1500, 1200, 1000];
const normalCount = Infinity;
// 稀有卡池的权重和数量
const rareWeights = [400, 300, 300];
const rareCount = [4, 3, 3];
// 传说卡池的权重和数量
const legendWeights = [100, 100, 100];
const legendCount = [1, 1, 1];
// 抽卡函数
function drawCard(poolType) {
let weights, count;
// 根据卡池类型选择权重和数量
switch (poolType) {
case 'normal':
weights = normalWeights;
count = normalCount;
break;
case 'rare':
weights = rareWeights;
count = rareCount;
break;
case 'legend':
weights = legendWeights;
count = legendCount;
break;
default:
throw new Error('Invalid pool type');
}
// 计算总权重
const totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
// 如果数量为无限,则直接返回随机结果
if (count === Infinity) {
let rand = Math.random() * totalWeight;
for (let i = 0; i < weights.length; i++) {
if (rand < weights[i]) {
return i;
}
rand -= weights[i];
}
}
// 否则,按照数量来进行抽卡
let results = [];
for (let i = 0; i < count.length; i++) {
for (let j = 0; j < count[i]; j++) {
let rand = Math.random() * totalWeight;
for (let k = 0; k < weights.length; k++) {
if (rand < weights[k]) {
results.push(k);
break;
}
rand -= weights[k];
}
}
}
return results;
}
```
使用方法:
```javascript
// 抽普通卡池的一张卡
const normalCard = drawCard('normal');
// 抽稀有卡池的三张卡
const rareCards = drawCard('rare').slice(0, 3);
// 抽传说卡池的一张卡
const legendCard = drawCard('legend')[0];
```
该抽卡方法可以根据卡池类型和数量来进行抽卡,并且支持无限数量的普通卡池。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)