0

The method below randomly generates the letters for the 6 sides of each of 16 dice meant to be used for Boggle based off the letter_frequencies hash. When the code is run, it iterates through the first element of boggle_dice, an empty array, filling it with random letters.

However, it proceeds to use that first iteration to fill the other 15 elements in boggle_dice with the same letters it has randomly chosen for the first element. What is causing this to happen, and how should the iterative process be altered to work as intended?

At the end of iteration, each of the keys should appear in boggle_dice the number of times equal to the value associated with it in the hash.

def create_board
  letter_frequencies = {
    'e' => 10,
    'a' => 8,
    'i' => 7,
    'o' => 6,
    'l' => 5,
    'n' => 5,
    's' => 5,
    't' => 5,
    'd' => 4,
    'r' => 4,
    'u' => 4,
    'b' => 3,
    'c' => 3,
    'g' => 3,
    'h' => 3,
    'm' => 3,
    'p' => 3,
    'y' => 3,
    'f' => 2,
    'k' => 2,
    'v' => 2,
    'w' => 2,
    'j' => 1,
    'q' => 1,
    'x' => 1,
    'z' => 1
  }

  boggle_dice = Array.new(16, [])
  boggle_dice.each do |die| 
    while die.length < 6 do 
      random_letter = letter_frequencies.keys.sample
      letter_frequencies[random_letter] <=0 ? next : die << random_letter
      letter_frequencies[random_letter] -= 1
    end
  end
end

1 Answer 1

1

That is a common beginner's mistake. It is because you did Array.new(16, []), the same array is modified repeatedly. It can be fixed by changing it to Array.new(16){[]}.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.