Last active
December 20, 2015 19:08
-
-
Save kaplan/6180526 to your computer and use it in GitHub Desktop.
Scary Craps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Die | |
def roll_die | |
rand(1..6) | |
end | |
end | |
class CrapsGame | |
attr_accessor :point, :dice_roll | |
def pass | |
(@dice_roll == 7 || @dice_roll == 11) | |
end | |
def no_pass | |
@dice_roll == 2 || @dice_roll == 3 || @dice_roll == 12 | |
end | |
def craps | |
@dice_roll == 7 | |
end | |
def win | |
@dice_roll == @point | |
end | |
def roll (die1, die2, first_roll) | |
self.dice_roll = die1.roll_die + die2.roll_die | |
if first_roll | |
puts "your first roll was a #{@dice_roll}" | |
if pass | |
puts dice_roll | |
puts "'natural' roll, you WIN!" | |
elsif no_pass | |
puts dice_roll | |
puts "'craps!' you LOSE!" | |
else | |
self.point = dice_roll | |
puts "set point is #{@point}" | |
roll(die1, die2, false) | |
end | |
else | |
puts "rolling again and you rolled a #{@dice_roll}" | |
if craps | |
puts "you crapped out with a #{@dice_roll}" | |
elsif win | |
puts "you win with #{@point}" | |
else | |
roll(die1, die2, false) | |
end | |
end | |
end | |
end | |
CrapsGame.new.roll(Die.new, Die.new, true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was looking at the @dice_roll and the @point v. using self.dice_roll and self.point. I thought maybe using self. for setting seemed ok and @ for retrieving, but I'm not sure about that and if there is a big difference other than style??