Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6066013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:27:19+00:00 2026-05-23T09:27:19+00:00

I’m trying to write a program that simulates the dice game bones. The idea

  • 0

I’m trying to write a program that simulates the dice game bones. The idea is to roll five dice and get the lowest score possible–threes have a value of 0. Once the five dice are rolled the player/bot MUST select at least one die (or more) from the five and roll the rest. This is what’s posing a problem to me. If there’s no threes then the “keeper” array which I push the dice the bot keeps to ends up being empty, which necessitates an embedded loop. Since I’m pretty new to coding I really can’t figure out a way to create an embedded loop that will ensure that at least one dice is designated as a keeper. For your sanity’s sake I’ll say that the “beta” version of this program I’ll present now is intended to do the following: Create a bot that tries to get the lowest score on one round of bones. Ie. The dice are rolled once. Then he tries to pick the lowest score possible. If there are no threes (equivalent to 0) he picks ones. The problem I’m trying to solve is creating an embedded loop that ensures at least one die is selected for the keeper array. The code is mainly to demonstrate how ugly my solution is and give an idea for a better solution.

#rolls dice
srand
dice = []
5.times do 
  dice.push(rand(6)+1)
end
puts dice
puts " "

#initialize keeper and roll again arrays
i = 0
keeper = []
roll_again = []


#select any 3s from the dice roll and put them in keeper
dice.each do |d| 
  if d == 3 
    keeper.push(d)
  else
    i +=1  #dummy operation to keep if statement functioning, tragically ugly code
  end
end

#in the case that no threes were rolled, ones are selected
if keeper.length == 0
  dice.each do |f|
    if f == 1
      keeper.push(f)
    else
      i+= 1
    end
  end
else 
  i += 1
end

puts "Keeper:"
puts keeper
puts "Roll Again:"
puts roll_again
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:27:20+00:00Added an answer on May 23, 2026 at 9:27 am

    For a beginner you are doing fine 🙂 I’ll just show you some things that can help you with this exercise, you learn best by trying out different things and trying to understand them.

    >> dice = [5,1,2,2,6,4] #=> [5, 1, 2, 2, 6, 4]
    >> if (threes = dice.select { |d| d == 3 }).empty?
    ..   ones = dice.select { |d| d == 1 }
    ..   end #=> [1]
    >> threes #=> []
    >> ones #=> [1]
    

    Another thing you can do is group the dice array:

    >> dice.group_by { |d| d } #=> {5=>[5], 1=>[1], 2=>[2, 2], 6=>[6], 4=>[4]}
    

    That way you can instantly see if the array for a given number is empty or not. Note that it’s easy to do array differences in Ruby, which might help you with removing dice:

    >> [3,1,3,5,1,7] - [3]  #=> [1, 5, 1, 7]
    

    In fact array intersection also is a good way to see if a certain element is part of an array:

    >> ([3,1,3,5,1,7] & [3]).empty? #=> false
    >> ([4,1,2,5,1,7] & [3]).empty? #=> true
    

    Some more tips: You don’t need dummy statements for your else branches, you can just leave them out. I also strongly recommend going through the docs of Array and Enumerable, there’s lot of useful methods in there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.