# Hey I need help with my code

**URL:** <https://www.ruby-forum.com/t/hey-i-need-help-with-my-code/178021>\
**Category:** Ruby\
**Created:** [October 28, 2009, 4:25am UTC](https://www.ruby-forum.com/t/hey-i-need-help-with-my-code/178021 "2009-10-28T04:25:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bguan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8edcca/32.png) [@bguan](https://www.ruby-forum.com/u/bguan)\
**Post date:** [October 28, 2009, 4:25am UTC](https://www.ruby-forum.com/t/hey-i-need-help-with-my-code/178021/1 "2009-10-28T04:25:38Z")

</div>

Hello guys I am doing a little something for class but I need help  
Okay the point of the game is to roll to random dice and if the dice  
lands on [one] you lose or if you dont you get to add up the two sums of  
what you did roll. or you get the option to choose to roll again or  
pass. heres that my code looks like…it will do the while, but when it  
gets to the if(and lands on 1) it will stay in the while for some  
reason. Here is what my code looks like

# roll or pass

# 

# A Ruby dice game. Each turn you can play it safe and keep your score,

# or roll again to rack up points. But watch out! A roll of 1 could

cost

# you everything!

# dice face rows

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

# dice faces

one = frame + blank + center + blank + frame  
two = frame + left + blank + right + frame  
three = frame + left + center + right + frame  
four = frame + both + blank + both + frame  
five = frame + both + center + both + frame  
six = frame + both_3 + frame  
weird = frame + blank_3 + frame  
faces = [one, two, three, four, five, six]  
points = 0  
puts ‘Would you like the roll or pass?’  
user\_turn = gets.chomp()

while (user\_turn != ‘pass’) do  
result1= faces[(rand(6))]  
result2= faces[(rand(6))]  
puts result1  
puts result2  
puts ‘current score is’  
if (result1 result2 != faces[0])  
puts ‘Would you like to roll again?’  
user\_turn = gets.chomp  
end  
end

---

<div class="post-metadata">

**Author:** ![bguan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8edcca/32.png) [@bguan](https://www.ruby-forum.com/u/bguan)\
**Post date:** [October 28, 2009, 5:24am UTC](https://www.ruby-forum.com/t/hey-i-need-help-with-my-code/178021/2 "2009-10-28T05:24:50Z")

</div>

You need a break in your loop. Also the conditional syntax is wrong

it should be something like:

unless result1 == faces[0] or results2 == faces[0]  
puts ‘Would you like to roll again?’  
else  
break  
end

---

<div class="post-metadata">

**Author:** ![bguan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8edcca/32.png) [@bguan](https://www.ruby-forum.com/u/bguan)\
**Post date:** [October 28, 2009, 5:44am UTC](https://www.ruby-forum.com/t/hey-i-need-help-with-my-code/178021/3 "2009-10-28T05:44:47Z")

</div>

Hey I got it working now I need help with one last thing. The game is  
suppose to take the number of the dice and put them into points, and  
this gives you, your score. the numbers are suppose to be like this one  
= \*0, two = 2, and so oh. How can I do this?

# roll or pass

# 

# A Ruby dice game. Each turn you can play it safe and keep your score,

# or roll again to rack up points. But watch out! A roll of 1 could

cost

# you everything!

# dice face rows

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

# dice faces

one = frame + blank + center + blank + frame  
two = frame + left + blank + right + frame  
three = frame + left + center + right + frame  
four = frame + both + blank + both + frame  
five = frame + both + center + both + frame  
six = frame + both_3 + frame  
weird = frame + blank_3 + frame  
faces = [one, two, three, four, five, six]  
points = 0  
puts ‘Would you like the roll or pass?’  
user\_turn = gets.chomp()  
while (user\_turn != ‘pass’) do  
result1= faces[(rand(6))]  
result2= faces[(rand(6))]  
puts result1  
puts result2  
if (result1 != faces[0]) and (result2 != faces[0])  
puts ‘current score is’  
puts ‘Would you like to roll again?’  
user\_turn = gets.chomp  
else  
break  
end  
end

---

<div class="post-metadata">

**Author:** ![bguan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8edcca/32.png) [@bguan](https://www.ruby-forum.com/u/bguan)\
**Post date:** [October 28, 2009, 5:57am UTC](https://www.ruby-forum.com/t/hey-i-need-help-with-my-code/178021/4 "2009-10-28T05:57:02Z")

</div>

Hi,

do yourself the favour and make your code more readable.

UNTESTED.

Am Mittwoch, 28. Okt 2009, 12:25:41 +0900 schrieb Brian G.:

> # dice face rows
> 
> frame = “±----+\n”  
> blank = “| |\n”  
> center = “| \* |\n”  
> left = “|\* |\n”  
> right = “| _|\n"  
> both = "|_ \*|\n”

FRAME = “±----+”  
BLANK = “| |”  
CENTER = “| \* |”  
LEFT = “|\* |”  
RIGHT = “| _|"  
BOTH = "|_ \*|”

> # dice faces
> 
> one = frame + blank + center + blank + frame  
> two = frame + left + blank + right + frame  
> three = frame + left + center + right + frame  
> four = frame + both + blank + both + frame  
> five = frame + both + center + both + frame  
> six = frame + both_3 + frame  
> weird = frame + blank_3 + frame  
> faces = [one, two, three, four, five, six]

FACES = [  
[BLANK, CENTER, BLANK],  
[LEFT , BLANK , RIGHT],  
[LEFT , CENTER, RIGHT],  
[BOTH , BLANK , BOTH],  
[BOTH , CENTER, BOTH],  
[BOTH , BOTH , BOTH],  
]

FACES.map! { |a| ([FACES] + a + [FACES]).map { |x| x + $/ }.join “”  
}

# As it’s a constant it is only evaluated once.

> points = 0  
> puts ‘Would you like the roll or pass?’  
> user\_turn = gets.chomp()

`gets’ may return nil (EOF == Ctrl-D).

> end  
> Argh. Indent the `if' correctly. Just call`gets’ in one place.

You should store and compare the _number_ of resulting eyes but  
not the whole image. That’s just for displaying.

Then the problem will be obvious, I bet.

Bertram

---

<div class="post-metadata">

**Author:** ![bguan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8edcca/32.png) [@bguan](https://www.ruby-forum.com/u/bguan)\
**Post date:** [October 28, 2009, 5:49am UTC](https://www.ruby-forum.com/t/hey-i-need-help-with-my-code/178021/5 "2009-10-28T05:49:25Z")

</div>

The integer you want is the random number + 1. Just save the rand  
value.
