Skip to content

Instantly share code, notes, and snippets.

@bendoane bendoane/dice. rb
Created Oct 14, 2015

Embed
What would you like to do?
Homework: day 7
class Dice
attr_accessor :sides
def initialize
self.sides = (1..6).to_a
end
def roll
sides.sample
end
end
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
You can’t perform that action at this time.