gpt4 book ai didi

ruby - 如何在 ruby​​ 中创建哈希的哈希

转载 作者:太空宇宙 更新时间:2023-11-03 17:36:57 26 4
gpt4 key购买 nike

<分区>

更新:答案在我下面,一个简单的 .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"}}
}

我该如何实现?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com