0

更新:答案在我下面,需要一个简单的 .merge 。

我需要从我的 build_virtual_boards_hash 方法中获取一个哈希值作为输出我无法弄清楚如何将哈希值添加到哈希值堆栈中。或者甚至如何创建这样的东西......首先我将列出方法,然后向您展示我得到的输出。

这是使用的两种方法...问题位于'build_virtual_boards_hash'内部...

def build_virtual_boards_hash(board, player)
  virtual_board = board.dup

  virtual_board_hash = {}
  new_board_hash = {}

  empty_spaces_on_board = virtual_board.grid.select{ |k, v| v == " " }.keys
  index_mark = 'VB'+empty_spaces_on_board.length.to_s

  #THIS IS WHERE I THINK THE PROBLEM IS
  #HOW DO I ADD EACH LOOP RESULT TO -SOMETHING- 
  #THAT CAN BE CALLED ON LATER AFTER THE LOOP ENDS
  #SO I GET THE DESIRED RESULTING OUTPUT?
  # 
  empty_spaces_on_board.each do |empty_space_symbol|
    # create a hash
    new_board_hash = {index_mark => move_as_somebody(board, player, empty_space_symbol).grid} #value
    p new_board_hash  
  end

end


#takes board....returns new board
def move_as_somebody(board, player, empty_space)
  new_board = board
  if player == 'X'
    new_board.grid[empty_space] = player
  else
    player == 'O'
    new_board.grid[empty_space] = player
  end
  return new_board
end

我目前从这两种方法中得到这个输出......

{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>" ", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>"X", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>"X", :c3=>"X"}}

我需要的是这个...

virtual_boards = {
    {"VB7"=>{:a1=>"X", :a2=>"X", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}},
    {"VB6"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}},
    {"VB5"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}},
    {"VB4"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>" "}},
    {"VB3"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>" ", :c3=>" "}},
    {"VB2"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>"X", :c3=>" "}},
    {"VB1"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>"X", :c3=>"X"}}
}

我该如何做到这一点?

4

2 回答 2

4

通常哈希哈希的问题是如何初始化深度哈希。

这个问题已经解决并提交到Ruby Facets

h = Hash.autonew

现在您可以通过这种方式轻松初始化哈希:

h[:h1][:key] = 'value'
h[:h2][:key] = 'value'
...
于 2012-12-09T20:41:47.327 回答
1

而答案是......

#builds hash of hash of fake boards
def build_virtual_boards_hash(board, player)
  virtual_board = board.dup
  i = 0
  virtual_board_hash = {}
  new_board_hash = {}

  empty_spaces_on_board = virtual_board.grid.select{ |k, v| v == " " }.keys


  while i < empty_spaces_on_board.length do
    p empty_space_symbol = empty_spaces_on_board[i]
    p index_mark = 'VB'+i.to_s

    new_board_hash = {index_mark => move_as_somebody(board, player, empty_space_symbol).grid}

    virtual_board_hash = virtual_board_hash.merge(new_board_hash)        

    i += 1
  end
  p virtual_board_hash
end
于 2012-12-10T03:53:57.927 回答