【问题标题】:How to check if numbers in array are included in nested array?如何检查数组中的数字是否包含在嵌套数组中?
【发布时间】:2019-10-10 20:13:15
【问题描述】:

我正在开发井字游戏,并想检查玩家的选择是否与包含获胜组合的嵌套数组匹配。

此时,例如 [7,8,9] 或 [9,7,8] 或 [1,2,3] 或 [1,3,2] 结束游戏并宣布获胜者,如他们应该。

但是,例如,[1,7,8,6,9] 或 [1,7,3,5,6] 或 [1,4,2,3,9] 将被忽略,获胜者获胜'尽管数组包括获胜组合“7,8,9”和“3,5,7”和“1,2,3”,但不会按预期声明。

提前感谢您的任何建议或帮助。

3x3 网格中的所有获胜组合。

WINNING_COMBOS = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]

我有两个玩家数组,@@player_one 和 @@player_two 从@possible_choice 数组中选择数字。

@possible_choice = [1,2,3,4,5,6,7,8,9]
.
.
.
def player_one_turn()
  puts "Player One, make your choice:"
  p @possible_choice
  puts @grid
  @@player_one << @possible_choice.delete(gets.chomp.to_i)
  p @@player_one
end

我也有has_won?方法

def has_won?
  WINNING_COMBOS.include?(@@player_one.sort) ||
  WINNING_COMBOS.include?(@@player_two.sort)
end

完整的未完成游戏。请不要介意网格,我还在努力。


class Grid
    WINNING_COMBOS = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]
    attr_accessor :possible_choice
    attr_accessor :grid

    def initialize
        @possible_choice = [1,2,3,4,5,6,7,8,9]
        @grid = "
                |----|----|----|   
                |  1 |  2 |  3 |
                |----|----|----|
                |  4 |  5 |  6 |
                |----|----|----|
                |  7 |  8 |  9 |
                |----|----|----|
                "
    end
end

class Game < Grid
    @@player_one = Array.new
    @@player_two = Array.new

    def game
        puts
        puts "*** This is a tic-tac-toe game for two human players. ***"
        puts
        loop do
            player_one_turn()
            puts
                if has_won?
                    puts "The game has ended. Player One has won!"
                    puts
                    return
                end
            break if @@player_one.length == 5
            player_two_turn()
            puts
                if has_won?
                    puts "The game has ended. Player Two has won!"
                    puts
                    return
                end
        end
    end

    def player_one_turn()
        puts "Player One, make your choice:"
        p @possible_choice
        puts @grid
        @@player_one << @possible_choice.delete(gets.chomp.to_i)
        p @@player_one
    end

    def player_two_turn()
        puts "Player Two, make your choice:"
        p @possible_choice
        puts @grid
        @@player_two << @possible_choice.delete(gets.chomp.to_i)
        p @@player_two
    end

    def has_won?
        WINNING_COMBOS.include?(@@player_one.sort) ||
        WINNING_COMBOS.include?(@@player_two.sort)
    end
end

new_game = Game.new
new_game.game

【问题讨论】:

  • WINNING_COMBOS.any? { |e| (@@player_one.sort &amp; e).size == 3 } → 你需要确保奖金中有一个元素,它与玩家的动作完全相交。
  • 有一个 3×3 的网格,每个字段都可以是空的,XO 不是更干净吗?两个@@player_one / @@player_two 数组似乎有点多余。

标签: arrays ruby multidimensional-array


【解决方案1】:
WINNING_COMBOS = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]

def win?(player)
  WINNING_COMBOS.each do |x,z,y|
    return true if player.include?(x) && player.include?(z) && player.include?(y) 
  end
  false
end

def has_won?
  win?(@@player_one) || win?(@@player_two)
end

应该做的工作,不需要对数组进行排序。

【讨论】:

  • 我猜你打算使用return true if ... 而不是true if ...,否则win? 将始终返回false。如果您将each 替换为any?,则不需要true if,只需后面的语句,您可以在最后省略false
  • @3limin4t0r 提出了一个您认可的好建议。那么为什么不编辑你的答案呢?您可能已经注意到,一些成员在编辑过的答案中承认了贡献;正如您所做的那样,其他人在评论中承认要保持答案“干净”。 (我属于后者。)另外,在编辑时,我建议您删除之前的内容(不要提及它,也不要使用“编辑:”),就像您正在编写一个文章或书籍。
  • @Cary Swoveland 如果我知道怎么做会非常高兴:)
  • 很好的解决方案。这很简单,但我无法弄清楚!谢谢大家帮助我。
【解决方案2】:

我能想到的最简单的是:

def has_won?(player)
  WINNING_COMBOS.any? { |combo| (player & combo).size == combo.size }
end

但是,如果您在哪里使用 Set 而不是 Array,您可以执行以下操作:

def has_won?(player)
  WINNING_COMBOS.any? { |combo| combo <= player } # check for subset
end

要将WINNING_COMBOS 转换为集合,只需执行以下操作:

require 'set'

WINNING_COMBOS = [
  [1, 2, 3], [4, 5, 6], [7, 8, 9],
  [1, 4, 7], [2, 5, 8], [3, 6, 9],
  [1, 5, 9], [3, 5, 7]
].map(&:to_set)

# for players use
@@player_one = Set.new

【讨论】:

  • 错字警告:一个太多的“h”。将删除此评论。考虑WINNING_COMBOS = Set.new([[1,2,3],....])
  • 非常感谢。这真的很有帮助。我还没有了解 Sets。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 2013-08-16
相关资源
最近更新 更多