-
-
Save bjhaid/6181419 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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