fruit machine

本文介绍了一种简洁的水果机得分算法实现。该算法通过处理三个相同的数组(每组包含10种不同的项),根据旋转后的结果计算得分。特别地,相同项越多得分越高,若有“Wild”项则按特定规则加倍得分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目很水,不过看到了很简洁的写法学习一下.

题目描述

水果机器有三列,出现的结果分别可能是十种情况. 得分标准如下:

1. There are always exactly three reels
2. Each reel has 10 different items.
3. The three reel inputs may be different.
4. The spin array represents the index of where the reels finish.
5. The three spin inputs may be different
6. Three of the same is worth more than two of the same
7. Two of the same plus one "Wild" is double the score.
8. No matching items returns 0.

这里写图片描述

Sample

reel1 = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"];
reel2 = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"];
reel3 = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"];
spin = [5,5,5];
result = fruit([reel1,reel2,reel3],spin);

代码实现

function fruit(reels, spins) {
    let list = ['Jack', 'Queen', 'King', 'Bar', 'Cherry', 'Seven', 'Shell', 'Bell', 'Star', 'Wild'];
    let [c,b,a] = reels
    .map((reel,i)=>list.indexOf(reel[spins[i]])+1)
    .sort((a,b)=>a-b);

    if(a===b && a===c) return a*10;

    if(a===b) return c===10 ? a * 2 : a;

    return b===c?b:0;
}

感觉有点皮

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值