You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

优化R语言老虎机score函数:支持DD作为通配符并简化代码

Simplified Score Function with DD Wildcards

Let's clean up your code by breaking down the logic into clearer, reusable parts while maintaining all the wildcard functionality you implemented. Here's a streamlined version:

score <- function(symbols) {
  # Base payout table for three-of-a-kind wins
  base_payouts <- c(DD = 100, '7' = 80, BBB = 40, BB = 25, B = 10, C = 10, '0' = 0)
  
  # Count wildcards (DD) and isolate non-wildcard symbols
  num_dd <- sum(symbols == 'DD')
  non_dd_symbols <- symbols[symbols != 'DD']
  
  # Check for three-of-a-kind (including wildcard matches)
  three_of_a_kind <- FALSE
  winning_symbol <- NULL
  
  if (num_dd == 3) {
    # All three are wildcards
    three_of_a_kind <- TRUE
    winning_symbol <- 'DD'
  } else if (length(unique(non_dd_symbols)) == 1) {
    # Two matching non-wildcards plus a wildcard, or three matching non-wildcards
    three_of_a_kind <- TRUE
    winning_symbol <- non_dd_symbols[1]
  }
  
  # Calculate base prize
  if (three_of_a_kind) {
    prize <- base_payouts[winning_symbol]
  } else if (all(symbols %in% c('BBB', 'BB', 'B'))) {
    # All bars (no wildcards, since wildcards aren't bars)
    prize <- 5
  } else {
    # Cherry payouts: 0 for 0 cherries, 2 for 1, 5 for 2
    cherry_count <- sum(symbols == 'C')
    prize <- 2 * cherry_count + 1 * (cherry_count == 2)
  }
  
  # Apply wildcard multiplier (2^number of DDs)
  prize * (2 ^ num_dd)
}

Key Simplifications:

  • Single Payout Table: We define base_payouts once instead of duplicating it, making the code easier to update if payout values change.
  • Explicit Wildcard Handling: We separate wildcard counting from non-wildcard checks, making it obvious when a three-of-a-kind is triggered by wildcards.
  • Concise Cherry Calculation: Reverted to the original cherry payout formula which is both compact and easy to understand.
  • Modular Logic: Each section handles one specific task (wildcard check, prize calculation, multiplier application) so the code is easier to debug and modify later.

Test Cases to Verify Correctness:

  • score(c('DD', 'DD', 'DD')) → 800 (100 * 2^3)
  • score(c('7', '7', 'DD')) → 160 (80 * 2^1)
  • score(c('C', 'C', '0')) → 5 (cherry case for two cherries)
  • score(c('B', 'BB', 'BBB')) → 5 (all bars)
  • score(c('C', 'C', 'DD')) → 20 (three-of-a-kind cherry * 2^1)

内容的提问来源于stack exchange,提问作者BKO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 22:53:12