Skip to content

Instantly share code, notes, and snippets.

@bjhaid
Forked from kaplan/scary_craps.rb
Last active December 20, 2015 19:09
Show Gist options
  • Save bjhaid/6181419 to your computer and use it in GitHub Desktop.
Save bjhaid/6181419 to your computer and use it in GitHub Desktop.
class Die
def roll
rand(1..6)
end
end
class Player
attr_writer :die1, :die2
def initialize
@dice_roll_from_first_turn = nil
@roll_count = 0
end
def pass dice_roll
(dice_roll == 7 || dice_roll == 11)
end
def no_pass dice_roll
dice_roll == 2 || dice_roll == 3 || dice_roll == 12
end
def first_roll dice_roll
@dice_roll_from_first_turn ||= dice_roll
if pass dice_roll
"Pass"
elsif no_pass dice_roll
"Failed"
else
""
end
end
def subsequent_roll dice_roll
if dice_roll == 7
"Failed"
elsif @dice_roll_from_first_turn == dice_roll
"Pass"
else
""
end
end
def roll
dice_roll = @die1.roll + @die2.roll
@roll_count += 1
if (first_roll dice_roll) == "Pass" && @roll_count == 1
puts "You win on roll no 1 with #{dice_roll} points"
true
elsif (first_roll dice_roll) == "Failed" && @roll_count == 1
puts "'craps!' you LOSE!"
true
elsif (subsequent_roll dice_roll) == "Failed" && @roll_count > 1
puts "Craps you loose with #{dice_roll} points at turn no #{@roll_count}"
true
elsif (subsequent_roll dice_roll) == "Pass" && @roll_count > 1
puts "You win with #{dice_roll} points on turn #{@roll_count}"
true
else
puts "Retry your points are #{dice_roll} at turn no #{@roll_count}"
false
end
end
def play
while !roll
end
end
end
player = Player.new
player.die1 = Die.new
player.die2 = Die.new
player.play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment