如何检查嵌套数组中是否包含数组中的数字?

时间:2019-05-24 08:49:33

标签: arrays ruby multidimensional-array

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

例如,目前[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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我能想到的最简单的方法是:

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