Skip to content

Commit

Permalink
protect the secret sauce
Browse files Browse the repository at this point in the history
  • Loading branch information
saragibby committed Feb 24, 2015
1 parent 7e4344e commit 37b510d
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gem "rspec"
23 changes: 23 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,23 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
rspec (3.2.0)
rspec-core (~> 3.2.0)
rspec-expectations (~> 3.2.0)
rspec-mocks (~> 3.2.0)
rspec-core (3.2.0)
rspec-support (~> 3.2.0)
rspec-expectations (3.2.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-mocks (3.2.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-support (3.2.1)

PLATFORMS
ruby

DEPENDENCIES
rspec
4 changes: 4 additions & 0 deletions README.md
@@ -0,0 +1,4 @@
rock, paper, scissors, lizard, spock
================

Exercise in showing the application lifecycle in the land of start-ups
1 change: 0 additions & 1 deletion models/game.rb
Expand Up @@ -10,7 +10,6 @@ class Game
def initialize(player_name)
@player1 = Player.new(player_name)
@player2 = ComputerPlayer.new
@round = 1
end

def result
Expand Down
1 change: 1 addition & 0 deletions play_a_game.rb
Expand Up @@ -47,6 +47,7 @@ def print_3_dots(sleep_time)
puts game.result
end

# Display final score
puts "\n--------- FINAL SCORE ---------"
puts " #{game.player1.name}: #{game.player1.win_count}"
puts " #{game.player2.name}: #{game.player2.win_count}"
Expand Down
39 changes: 39 additions & 0 deletions spec/computer_player_spec.rb
@@ -0,0 +1,39 @@
require_relative '../models/computer_player'
require_relative '../models/game'

describe ComputerPlayer do
subject { ComputerPlayer.new }

context '#initialize' do
it { expect(subject.win_count).to eq(0) }
end

context '#name' do
it { expect(subject.name).to eq('Computer') }
end

context '#pick' do
before { subject.pick }

it 'sets selection' do
expect(subject.selection).not_to be_nil
end

it 'gets random value from Game::CHOICES' do
expect(Game::CHOICES).to include(subject.selection.to_sym)
end
end

context '#selection_detail' do
before{ subject.pick }

it { expect(subject.selection_detail).to eq(Game::CHOICES[subject.selection.to_sym]) }
end

context '#selection_name' do
before { subject.pick }

it { expect(subject.selection_name).to eq(Game::CHOICES[subject.selection.to_sym][:name]) }
end

end
62 changes: 62 additions & 0 deletions spec/game_spec.rb
@@ -0,0 +1,62 @@
require_relative '../models/game'

describe Game do
subject { Game.new('Kirk') }

context '#initialize' do
it { expect(subject.player1.name).to eq('Kirk') }
it { expect(subject.player2.class.name).to eq('ComputerPlayer')}
end

context '#result' do
it 'returns tie if both players picked same choice' do
subject.player1.selection = 'r'
allow(subject.player2).to receive(:selection) { 'r' }
expect(subject.result).to eq('It was a tie! No winner')
end

it 'returns player1 name if he/she beats player2 choice' do
subject.player2.pick
subject.player1.selection = get_choice_that_beats(subject.player2.selection)
expect(subject.result).to eq("WINNER is: #{subject.player1.name} - #{subject.player1.selection_name} #{winner_choice_action(subject.player1.selection, subject.player2.selection)} #{subject.player2.selection_name}")
end

it 'returns player2 name if he/she beats player1 choice' do
subject.player2.pick
subject.player1.selection = get_choice_that_loses(subject.player2.selection)
expect(subject.result).to eq("WINNER is: #{subject.player2.name} - #{subject.player2.selection_name} #{winner_choice_action(subject.player2.selection, subject.player1.selection)} #{subject.player1.selection_name}")
end
end

context '#champion' do
it 'returns tie if matching win counts' do
subject.player1.win_count = 1
subject.player2.win_count = 1
expect(subject.champion).to eq('TIE!!!')
end

it 'returns player1 name if he/she has more wins' do
subject.player1.win_count = 1
subject.player2.win_count = 0
expect(subject.champion).to eq(subject.player1.name)
end

it 'returns player2 name if he/she has more wins' do
subject.player1.win_count = 0
subject.player2.win_count = 1
expect(subject.champion).to eq(subject.player2.name)
end
end
end

def get_choice_that_beats(choice)
Game::CHOICES.select{ |k,v| v[:beats].include?(choice.to_sym) }.first[0].to_s
end

def get_choice_that_loses(choice)
Game::CHOICES[choice.to_sym][:beats].first[0].to_s
end

def winner_choice_action(choice, losing_action)
Game::CHOICES[choice.to_sym][:beats][losing_action.to_sym]
end
51 changes: 51 additions & 0 deletions spec/player_spec.rb
@@ -0,0 +1,51 @@
require_relative '../models/player'
require_relative '../models/game'

describe Player do
subject { Player.new('Kirk') }

context '#initialize' do
it { expect(subject.win_count).to eq(0) }
end

context '#name' do
it { expect(subject.name).to eq('Kirk') }
end

context '#valid?' do
it 'true for valid selection' do
subject.selection = 'r'
expect(subject).to be_valid
end

it 'false for invalid selection' do
subject.selection = 'f'
expect(subject).not_to be_valid
end
end

context '#selection_detail' do
before{ subject.selection = 'r' }

it { expect(subject.selection_detail).to eq(Game::CHOICES[subject.selection.to_sym]) }
end

context '#selection_name' do
before { subject.selection = 'r' }

it { expect(subject.selection_name).to eq(Game::CHOICES[subject.selection.to_sym][:name]) }
end

context '#beat?' do
before { subject.selection = 'r' }

it 'true if selection does not beat opponent' do
expect(subject).to be_beat('l')
end

it 'false if selection does beat opponent' do
expect(subject).not_to be_beat('p')
end
end

end

0 comments on commit 37b510d

Please sign in to comment.