Skip to content

Instantly share code, notes, and snippets.

@kaplan
Created August 14, 2013 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaplan/6227692 to your computer and use it in GitHub Desktop.
Save kaplan/6227692 to your computer and use it in GitHub Desktop.
A revised, non-recursive version of the scary_craps.rb file. This needs to run in the terminal b/c there's a chomp for the rolling again after a point is set. Before I was just hitting command + b to run it in Sublime Text2.
class Die
def roll_die
rand(1..6)
end
end
class CrapsShooter
attr_accessor :point, :dice_roll, :game_over
def crap_out?
(@point.nil? && [2, 3, 12].any? { |num| num == @dice_roll }) || @dice_roll == 7
end
def win?
(@point.nil? && @dice_roll == 7 || @dice_roll == 11) || @point == @dice_roll
end
def roll
@game_over = false
die1 = Die.new
die2 = Die.new
self.dice_roll = die1.roll_die + die2.roll_die
puts ".:.:.:ROLLING:.:.:."
puts "Shooter rolls a #{@dice_roll}!!"
if win?
puts "'natural' roll, you WIN!"
elsif crap_out?
puts "'craps!' you LOSE!"
else
self.point = dice_roll
puts "Roll a #{@point} to win!"
while !game_over
puts "Roll again... hit enter NOW!"
gets.chomp
puts "rolling..."
@dice_roll = die1.roll_die + die2.roll_die
puts "Shooter rolls a #{@dice_roll}!!"
if win?
puts "WIN!"
@game_over = true
break
elsif crap_out?
puts "Sorry, you crapped out!!"
@game_over = true
break
end
end
end
end
end
CrapsShooter.new.roll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment