Created
September 4, 2013 00:53
-
-
Save steven-ferguson/6431531 to your computer and use it in GitHub Desktop.
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 initialize(number_of_sides) | |
@number_of_sides = number_of_sides | |
end | |
def sides | |
@number_of_sides | |
end | |
def roll | |
rand(@number_of_sides - 1) + 1 | |
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 './lib/die' | |
def main_menu | |
puts "Press 'c' to create a new die, 'r' to roll your die or 'e' to exit" | |
main_choice = gets.chomp | |
if main_choice == 'c' | |
create_die | |
elsif main_choice == 'r' | |
roll_die | |
elsif main_choice == 'e' | |
puts "Good-bye!" | |
else | |
puts "Sorry, that wasn't a valid option." | |
main_menu | |
end | |
end | |
def create_die | |
puts "How many sides do you want in your die?" | |
@sides = gets.to_i | |
@die = Die.new(@sides) | |
puts "\n" | |
puts "You created a die with #{@die.sides} sides." | |
main_menu | |
end | |
def roll_die | |
if @die.nil? | |
puts "You have not created a die yet. Please create a die before rolling" | |
else | |
puts "You rolled a #{@die.roll}." | |
end | |
main_menu | |
end | |
main_menu | |
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 'rspec' | |
require 'die' | |
describe Die do | |
it 'initialized with specified number of sides' do | |
die = Die.new(6) | |
die.should be_an_instance_of Die | |
end | |
it 'lets you read the number of sides out' do | |
test_die = Die.new(12) | |
test_die.sides.should eq 12 | |
end | |
it 'rolls the die with a random number (1 to the number of sides)' do | |
test_die = Die.new(12) | |
test_die.stub(:rand) {11} | |
test_die.roll.should eq 12 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment