2017-08-11 82 views
0

我試圖做一個循環,當隨機數匹配輸入的相同索引時,值被更改並保持每個後續循環的方式。更改循環中隨機數組的數組中的值

input = gets.chomp 
tries_left = 12 
while(tries_left > 0) 
tries_left -= 1 
computer = 4.times.map do rand(0..6) end.join 

    if computer[0] == input[0] 
    computer[0] = input[0] 
end 
end 

在上面的代碼之後的第一個循環中存儲到輸入[0]的值重置。

computer = 4.times.map do rand(0..6) end.join 
input = gets.chomp 
    tries_left = 12 
    while(tries_left > 0) 
    tries_left -= 1 

     if computer[0] == input[0] 
     computer[0] = input[0] 
    end 

如果我把計算機從這樣的循環中取出來,每次都會產生相同的隨機數。除了已經匹配的內容外,我還需要每次都生成新的數字。

+0

這似乎是一個猜謎遊戲。上面的代碼應該做什麼?什麼可行,什麼不行?你可以用用戶輸入和期望的輸出顯示示例會話嗎? – Stefan

回答

1

如果使computer字符串數組,可以freeze,以防止進一步的修改,然後replace在計算機中的內容時,它不匹配索引:

input = gets.chomp 
tries_left = 12 

computer = Array.new(4) { '' } 
# setting the srand to 1234, the next 48 calls to 'rand(0..6)' will always 
# result in the following sequence: 
# 3, 6, 5, 4, 4, 0, 1, 1, 1, 2, 6, 3, 6, 4, 4, 2, 6, 2, 0, 0, 4, 5, 0, 1, 
# 6, 6, 2, 0, 3, 4, 5, 2, 6, 2, 3, 3, 0, 1, 3, 0, 3, 2, 3, 4, 1, 3, 3, 3 
# this is useful for testing things are working correctly, 
# but take it out for 'live' code 
srand 1234 
while tries_left > 0 
    # no need to keep iterating if we've generated all the correct values 
    if computer.all?(&:frozen?) 
    puts "won #{computer.inspect} in #{12 - tries_left} tries" 
    break 
    end 

    tries_left -= 1 
    computer.each.with_index do |random, index| 
    # generate a new random number here unless they guessed correctly previously 
    random.replace(rand(0..6).to_s) unless random.frozen? 

    # if they've guessed the new random number, mark the string so they we 
    # don't update it 
    random.freeze if random == input[index] 
    end 

    puts "#{computer.inspect} has #{computer.count(&:frozen?)} correct numbers" 
end 

,然後當你運行腳本時:

$ echo 3654 | ruby example.rb 
# ["3", "6", "5", "4"] has 4 correct numbers 
# won ["3", "6", "5", "4"] in 1 tries 
$ echo 3644 | ruby example.rb 
# ["3", "6", "5", "4"] has 3 correct numbers 
# ["3", "6", "4", "4"] has 4 correct numbers 
# won ["3", "6", "4", "4"] in 2 tries 
$ echo 3555 | ruby example.rb 
# ["3", "6", "5", "4"] has 2 correct numbers 
# ["3", "4", "5", "0"] has 2 correct numbers 
# ["3", "1", "5", "1"] has 2 correct numbers 
# ["3", "1", "5", "2"] has 2 correct numbers 
# ["3", "6", "5", "3"] has 2 correct numbers 
# ["3", "6", "5", "4"] has 2 correct numbers 
# ["3", "4", "5", "2"] has 2 correct numbers 
# ["3", "6", "5", "2"] has 2 correct numbers 
# ["3", "0", "5", "0"] has 2 correct numbers 
# ["3", "4", "5", "5"] has 3 correct numbers 
# ["3", "0", "5", "5"] has 3 correct numbers 
# ["3", "1", "5", "5"] has 3 correct numbers 
+0

真棒,正是我在找的東西。在你有機會回答之前,我可能會想清楚,但是現在我正在爲每個凍結一次的元素添加一個計數器+ 1。我也可以在這種情況下使用凍結嗎?換句話說,我試圖讓「正確的數字:2」或每次迭代後的數量等等。 –

+0

您可以['count'](https://ruby-doc.org/core-2.4.0/Array.html#method-i-count)凍結元素的數量('computer.count(&: ?)'),或者只是將'if random == input [index]'移動到一個完整的塊而不是一行,如果語句並在該塊內增加一個計數器 –

+0

最後一個問題。爲什麼(&:frozen)有「:」? :是一個不變的權利?我不明白它在做什麼。 –

1

這不是很清楚你所要完成的是什麼,但這:

if computer[0] == input[0] 
    computer[0] = input[0] 
end 

顯然是一個空操作。沒有更新,因爲computer[0],無論它是什麼,都被設置爲它的相同值。我相信你想在陣列中以某種方式使用索引:

4.times.map do |index| 
    value = rand(0..6) 
    # somehow check the similarity, e.g.: 
    if input[index] == value 
    # do something 
    end 
end 

不好意思了很模糊的答案,但它確實很難理解你正在努力實現的目標。