更新:答案在我下面,需要一个简单的 .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"}}
}
我该如何做到这一点?