Created
October 14, 2015 04:31
-
-
Save bendoane/38e40d6feddb49583e79 to your computer and use it in GitHub Desktop.
Homework: day 7
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 Dice | |
attr_accessor :sides | |
def initialize | |
self.sides = (1..6).to_a | |
end | |
def roll | |
sides.sample | |
end | |
end |
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
require_relative "dice" | |
class Dice_Game | |
attr_accessor :player1, :computer | |
def initialize | |
puts "We shall roll 6 dice at a time, and see who can achieve a total of 100 first." | |
end | |
def reset(player1_total=0,computer_total=0) | |
play(player1_total,computer_total) | |
end | |
def play(player1_total=0,computer_total=0) | |
puts "We both roll the dice!" | |
until player1_total >= 100 || computer_total >= 100 | |
player1_rolls = [] | |
computer_rolls = [] | |
6.times do | |
result = Dice.new.roll | |
player1_rolls << result | |
end | |
6.times do | |
results = Dice.new.roll | |
computer_rolls << results | |
end | |
player1_total = player1_total + player1_rolls.inject(0){|sum, x| sum = sum + x} | |
computer_total = computer_total + computer_rolls.inject(0){|sum, x| sum = sum + x} | |
puts "You rolled #{player1_rolls}, for a total of #{player1_total}" | |
puts "Jarvis rolled #{computer_rolls}, for a total of #{computer_total}" | |
puts " " | |
puts "Let's roll again, shall we?" | |
end | |
if player1_total >= 100 | |
puts "You win!" | |
elsif computer_total >= 100 | |
puts "I'm sorry sir, but I seem to have defeated you." | |
else | |
play(player1_total,computer_total) | |
end | |
end | |
end | |
game = Dice_Game.new | |
game.play |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment