This blog no longer exists.
You will be redirected to something cool.

Tuesday, November 2, 2010

Dice Game: Ifs & Loops

I had a little fun with some 'ifs' and 'while loops' with this program. When I first started the program I became somewhat frustrated. I stopped and slept on it. When I came back to the code, it was sooo easy to piece together. The break was a really good idea. The code:

count = 0
count2 = 3
guess = 0

while guess <2 || guess >12
  puts( 'Pick a number 2-12: ')
  guess = gets.chomp.to_i
end

dieTotal = 0

while dieTotal != guess && count != 3
  count = count + 1
  count2 = count2 - 1
  die1 = rand(6)
  die1 = die1 + 1
  die2 = rand(6)
  die2 = die2 + 1
  dieTotal = die1 + die2
  input = ''
  puts( 'Type roll to roll the dice: ')
  input = gets.chomp

  while input != 'roll'
    puts( 'Type roll to roll the dice: ')
    input = gets.chomp
    puts( 'I don\'t understand. ')
  end
  puts  
  puts( "Your guess is #{guess}.")
  puts( "You rolled #{die1} and #{die2}. For a total of #{dieTotal}.")
  puts

  if dieTotal != guess && count2 > 1
  puts( "You have #{count2} rolls left.") 
  puts
  elsif dieTotal != guess && count2 == 1
  puts( "You have #{count2} roll left.")
  puts
  elsif dieTotal != guess && count2 == 0
  puts( "You have no rolls left.")
  puts( "Sorry, you lose.")
  else
  puts( "Congrats! You win!")
  end
end

0 comments: