# Help With Homework Code

Another user posted a question about a dice game earlier. I have this
same assignment. The goal of the assignment is the create a program to
roll two dice, add the number of those dice to the score, and if you get
a 1 you lose. After each roll the program asks the user to roll or pass.

I’ve accomplished this…but I’m pretty sure the way I set up the
scoring isn’t the most efficient way to do it, but I don’t know how else
to do it. Can anyone help me?

# Die Pieces

frame = “±----+\n”
blank = “| |\n”
center ="| * |\n"
right = “|* |\n”
left = “| |\n"
both = "|
*|\n”
#############################################

# Dice

one = frame + blank + center + blank + frame
two = frame + right + blank + left + frame
three = frame + right + center + left + frame
four = frame + both + blank + both + frame
five = frame + both + center + both + frame
six = frame + (both * 3) + frame
#############################################

score = 0
turns = 0
faces = [one, two, three, four, five, six]

puts “Would you like to Roll or Pass?”
input = gets().chomp().downcase()

if (input == “pass”)
puts "\nFinal Score is: " + score.to_s() + “\nBye!”
end

if (input != “pass”)
roll_1 = faces[rand(6)]
roll_2 = faces[rand(6)]
puts roll_1 + roll_2

From here if (roll_1 == faces[1])
score = score + 2
elsif (roll_1 == faces[2])
score = score + 3
elsif (roll_1 == faces[3])
score = score + 4
elsif (roll_1 == faces[4])
score = score + 5
elsif (roll_1 == faces[5])
score = score + 6
end

``````  if (roll_2 == faces[1])
score = score + 2
elsif (roll_2 == faces[2])
score = score + 3
elsif (roll_2 == faces[3])
score = score + 4
elsif (roll_2 == faces[4])
score = score + 5
elsif (roll_2 == faces[5])
score = score + 6
``````

To here end

``````puts "Current Score is: " + score.to_s()
turns = turns + 1
``````

elsif ((roll_1 == faces[0])||(roll_2 == faces[0]))
puts “You rolled a 1. You lose!”
end
end

puts "Turns taken: " + turns.to_s()

Rick B. wrote:

I’ve accomplished this…but I’m pretty sure the way I set up the
scoring isn’t the most efficient way to do it, but I don’t know how else
to do it. Can anyone help me?

I suggest that you separate the “model” from the “view”

That is, if you do the rolling like this:

roll_1 = rand(6)
roll_2 = rand(6)

then roll_1 and roll_2 have a numeric value between 0 and 5. This is a
natural representation (model) of the state of the roll, making
calculation easy:

score = score + (roll_1 + 1) + (roll_2 + 1)

The “view” part is when you want to translate this into something to
display:

puts faces[roll_1]
puts faces[roll_2]

Actually, the logic would probably be clearer if you make roll_1 and
roll_2 be a random value between 1 and 6 rather than 0 and 5. You can
adjust your faces array so that the zero’th element is blank. As a
convenient side-effect, you can use your faces array for playing dominos
too

Thank you Candlerb.

The only thing is that somehow created another small problem (or maybe
it was something else)

``````    if ((roll_1) && (roll_2) != 0)
puts faces[roll_1] + faces[roll_2]
score = score + (roll_1 + 1) + (roll_2 +1)
puts "Current Score is: " + score.to_s()
``````

elsif ((roll_1 == 0)||(roll_2 == 0))
puts faces[roll_1] + faces[roll_2]
puts “You rolled a 1! You lose!”
puts “Final Score is: 0”
end

It won’t count roll_1 into the “elsif” at all and I have no idea why.
ex. If roll_1 is a One it will stay in the “if”. If roll_2 is a One it
will go to “elsif”.

In this bit

if ((roll_1) && (roll_2) != 0)

did you mean

if (roll_1 != 0 && roll_2 != 0)

?

In the first case it’s effectively evaluating whether roll_1 exists (ie
is not nil), and that will always be true. Each side of the && is a
seperate logic test. So, the if/else test is effectively dependent
solely on the value of roll_2.

I’m not sure I understand what you’re saying. I tried the “if”
statements you gave me, but they didn’t do anything(or I didn’t
implement them right).

Basically I have

roll_1 = rand(6)

and then further down

if (roll_1 == 0)
puts “you lose”

…but it won’t do the “if” statement even when roll_1 does equal 0

if (input != “pass”)

roll_1 = rand(6)
roll_2 = rand(6)
puts faces[roll_1] + faces[roll_2]
score = score + (roll_1 + 1) + (roll_2 +1)
turns = turns + 1
puts "Current Score is: " + score.to_s()

elsif (roll_1 == 0 || roll_2 == 0)
puts “You lose!\nFinal Score is: 0”

end

Here’s my whole code so far if it helps you help me

score = 0
turns = 0
faces = [one, two, three, four, five, six]

puts “Would you like to Roll or Pass?”
input = gets().chomp().downcase()

if (input == “pass”)
puts "\nFinal Score is: " + score.to_s()
end

if (input != “pass”)

roll_1 = rand(6)
roll_2 = rand(6)
puts faces[roll_1] + faces[roll_2]
score = score + (roll_1 + 1) + (roll_2 +1)
turns = turns + 1
puts "Current Score is: " + score.to_s()

elsif (roll_1 == 0 || roll_2 == 0)
puts “You lose!\nFinal Score is: 0”
end
end

puts "Turns taken: " + turns.to_s()

Max W. wrote:

In this bit

if ((roll_1) && (roll_2) != 0)

did you mean

if (roll_1 != 0 && roll_2 != 0)

?

In the first case it’s effectively evaluating whether roll_1 exists (ie
is not nil), and that will always be true. Each side of the && is a
seperate logic test. So, the if/else test is effectively dependent
solely on the value of roll_2.

Oh and if you did mean that, the logic could be more simply expressed as

if (roll_1 + roll_2) > 0

note this is a mathematical plus rather than a logical and.

Thank you so much. That was driving me crazy.

I don’t really understand what you’re trying to do, but i can see one
problem.

roll_1 = rand(6) happens inside the if block, right? (if (input !=
“pass”))

So it will be done if the if test is true. And if the first if test is
true, then the elsif will never be tested - when it finishes the if
block then it will exit the whole if/elsif section.

Without getting into any other details, and just addressing the if/else
stuff, this might be better - it uses a nested if instead of an elsif
which i think is what you wanted to do.

score = 0
turns = 0
faces = [one, two, three, four, five, six]

puts “Would you like to Roll or Pass?”
input = gets().chomp().downcase()

if (input == “pass”)
puts "\nFinal Score is: " + score.to_s()
else
roll_1 = rand(6)
roll_2 = rand(6)
puts faces[roll_1] + faces[roll_2]
score = score + (roll_1 + 1) + (roll_2 +1)
turns = turns + 1
puts "Current Score is: " + score.to_s()

``````if (roll_1 == 0 || roll_2 == 0)
puts "You lose!\nFinal Score is: 0"
end
``````

end
end

puts "Turns taken: " + turns.to_s()

Rick B. wrote:

Thank you so much. That was driving me crazy.

cool

btw, another tip - instead of this

puts "Turns taken: " + turns.to_s()

do this

puts “Turns taken: #{turns}”

the to_s (no need for braces btw) is done automatically and it’s
generally cleaner, safer and easier to read.

Rick,

I’m going to sprinkle a little commentary in here…

On Oct 28, 2009, at 10:35 AM, Max W. wrote:

Why call this variable “answered”? It seems like you want the next
line (the start of the loop) to read like “until finished” so why not
make it so.

finished = false # or you could use nil

You don’t need to have a “do” here. A while (or an until) implicitly
begins a block.

until finished

puts “Would you like to Roll or Pass?”

You can leave the cursor after the question by using print instead of
puts:

print "Would you like to Roll or Pass? "

input = gets().chomp().downcase()

The empty parentheses are typically omitted.

input = gets.chomp.downcase

if (input == “pass”)
puts "\nFinal Score is: " + score.to_s()

Again, you have optional parentheses that are idiomatically dropped.
Also, it’s much more likely that you’d see interpolation that string
concatenation.

if input == “pass”
puts “\nFinal Score is: #{score}”
finished = true

Of course, you could also simply call ‘break’ here and use the ‘loop’
keyword rather than keep track of a variable to control the loop.
(Although the ‘loop’ does take a block so you’d have: ‘loop do …
end’ in place of ‘until finished; … end’)

`````` answered = true
``````

end
end
end

## puts "Turns taken: " + turns.to_s()

Posted via http://www.ruby-forum.com/.

I hope this helps you.

-Rob