【问题标题】:testing multiple variables on picaxe在 picaxe 上测试多个变量
【发布时间】:2018-01-23 20:35:33
【问题描述】:

我正在尝试在 picaxe 上制作一个 yahtzee 得分手,除了有这么多不同的组合之外,一切都很好。如果有一种方法可以测试我的 5 个变量中的 4 个是否相同(不多也不少),而不必经过所有不同的组合,我正在徘徊,例如: 如果 b1=b2 and b1=b3 and b1=b4 and b1!=b5 then ... 如果 b1=b2 and b1=b3 and b1=b5 and b1!=b4 then ...

总而言之,有一种方法可以让我查看 5 个变量中是否只有 4 个相同。

【问题讨论】:

    标签: variables picaxe


    【解决方案1】:

    因为您告诉我们这是针对 Yahtzee 得分手的,所以我假设我们需要比较的五个变量代表五个骰子的投掷,因此它们的值只会在 1 到 6 之间。

    在这种情况下,函数式解决方案是计算有多少变量等于一个测试值,并对 1 到 6 之间的测试值重复此操作:

    ; define symbols for the two variables we will use
    symbol same_test = b6
    symbol same_count = b7
    
    b1 = 3: b2 = 3: b3 = 3: b4 = 3: b5 = 1 ; test data
    gosub test4same
    if same_count = 4 then found_4_same ; do something
    ; else program flow continues here
    end
    
    found_4_same:
    sertxd("found 4 the same")
    end
    
    test4same: ; test if any four of the variables are equal
    same_count = 0
    for same_test = 1 to 6
        if b1 = same_test then gosub found_one
        if b2 = same_test then gosub found_one
        if b3 = same_test then gosub found_one
        if b4 = same_test then gosub found_one
        if b5 = same_test then gosub found_one
        if same_count = 4 then exit ; 4 variables were equal to same_test
        same_count = 0
    next
    return
    
    found_one:
    inc same_count
    return
    

    gosub test4same 将检查 b1b5 的五个变量中的四个是否等于相同的数字,对于介于 1 和 6 之间的数字。如果是,则变量 same_count 将为 4,而四个变量相等的数字将在same_test 中。

    在将same_count 重置为零之前使用if ... then exit 结构是我能想到的判断我们是否找到四个相同的最有效方法。

    两个symbol 语句之后和标签test4same 之前的代码只是为了证明它有效;用你的实际程序替换它。

    原则上,您可以在任何值范围内使用相同的技术,但如果您需要测试字节变量的所有 256 个可能值,显然会有点慢。

    【讨论】:

      猜你喜欢
      • 2016-12-20
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多