Skip to content

Instantly share code, notes, and snippets.

@BaylorRae
Created February 8, 2012 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaylorRae/1770891 to your computer and use it in GitHub Desktop.
Save BaylorRae/1770891 to your computer and use it in GitHub Desktop.
my first spec
class RockPaperScissors
attr_reader :result, :choice
def initialize(choice)
@choice = choice
if is_valid_choice?
@result = play
else
@result = instructions
end
end
private
def is_valid_choice?
if @choice.is_a?(Integer) && (@choice > 0 && @choice < 4)
@choice = number_to_choice(@choice)
return true
elsif @choice.is_a?(String) && ['rock', 'paper', 'scissors'].include?(@choice.downcase)
@choice = @choice.downcase
return true
end
false
end
def play
@computer_choice = random_choice
if @choice == @computer_choice
return "tie!"
elsif (@choice == "rock" && @computer_choice == "scissors") ||
(@choice == "paper" && @computer_choice == "rock") ||
(@choice == "scissors" && @computer_choice == "paper")
return "won!"
else
return "lost!"
end
end
def random_choice
number_to_choice(Random.new.rand(1...4))
end
def instructions
"please use one of the valid answers below"
end
def number_to_choice(number)
case number
when 1
number = "rock"
when 2
number = "paper"
when 3
number = "scissors"
end
return number
end
end
require 'rock_paper_scissors'
# game = RockPaperScissors.new('rock')
# game.result => ['win!', 'lose!', 'tie!']
#
# 1 == rock
# 2 == paper
# 3 == scissors
describe "Rock Paper Scissors" do
let(:answers_snippet) { /valid answers/ }
context "when a number is passed" do
it "fails when invalid" do
game = RockPaperScissors.new(0)
game.result.should match(answers_snippet)
end
it "passes when valid and convert to string value" do
game = RockPaperScissors.new(1)
game.result.should_not match(answers_snippet)
game.choice.should == "rock"
end
end
context "when a string is passed" do
it "fails when invalid" do
game = RockPaperScissors.new('cheese')
game.result.should match(answers_snippet)
end
it "passes when valid" do
game = RockPaperScissors.new('rock')
game.result.should_not match(answers_snippet)
end
end
it "should tie when rock vs rock" do
RockPaperScissors.any_instance.stub(:random_choice).and_return('rock')
game = RockPaperScissors.new('rock')
game.result.should == "tie!"
end
it "rock should beat scissors" do
RockPaperScissors.any_instance.stub(:random_choice).and_return('scissors')
game = RockPaperScissors.new('rock')
game.result.should == "won!"
end
it "paper should beat rock" do
RockPaperScissors.any_instance.stub(:random_choice).and_return('rock')
game = RockPaperScissors.new('paper')
game.result.should == "won!"
end
it "scissors should beat paper" do
RockPaperScissors.any_instance.stub(:random_choice).and_return('paper')
game = RockPaperScissors.new('scissors')
game.result.should == "won!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment