Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rock Paper Scissors Black Bytes blog version
module BlackBytesExample
class Game
def self.play(move1, move2)
return :tie if move1.class == move2.class
move2.wins_against?(move1)
end
end
class Rock
def wins_against?(other_move)
other_move.do_you_beat_rock?
end
def do_you_beat_paper?
false
end
def do_you_beat_scissors?
true
end
end
class Paper
def wins_against?(other_move)
other_move.do_you_beat_paper?
end
def do_you_beat_rock?
true
end
def do_you_beat_scissors?
false
end
end
class Scissors
def wins_against?(other_move)
other_move.do_you_beat_scissors?
end
def do_you_beat_paper?
true
end
def do_you_beat_rock?
false
end
end
end
include BlackBytesExample
require "minitest/autorun"
describe "Rock Paper Scissors Game" do
describe "Rock" do
it "Ties against Rock" do
assert_equal :tie, Game.play(Rock.new, Rock.new)
end
it "Loses against Paper" do
refute Game.play(Rock.new, Paper.new)
end
it " Wins against Scissors" do
assert Game.play(Rock.new, Scissors.new)
end
end
describe "Paper" do
it "Wins against Rock" do
assert Game.play(Paper.new, Rock.new)
end
it "Ties against Paper" do
assert_equal :tie, Game.play(Paper.new, Paper.new)
end
it "Loses against Scissors" do
refute Game.play(Paper.new, Scissors.new)
end
end
describe "Scirssors" do
it "Loses against Rock" do
refute Game.play(Scissors.new, Rock.new)
end
it "Wins against Paper" do
assert Game.play(Scissors.new, Paper.new)
end
it "Ties against Scissors" do
assert_equal :tie, Game.play(Scissors.new, Scissors.new)
end
end
end
require "benchmark/ips"
Benchmark.ips do |x|
x.report("BlackBytesExample") do
Game.play(Rock.new, Rock.new)
Game.play(Rock.new, Paper.new)
Game.play(Rock.new, Scissors.new)
Game.play(Paper.new, Rock.new)
Game.play(Paper.new, Paper.new)
Game.play(Paper.new, Scissors.new)
Game.play(Scissors.new, Rock.new)
Game.play(Scissors.new, Paper.new)
Game.play(Scissors.new, Scissors.new)
end
end
@danielpclark

This comment has been minimized.

Copy link
Owner Author

@danielpclark danielpclark commented Apr 24, 2017

Results

Warming up --------------------------------------
   BlackBytesExample    14.104k i/100ms
Calculating -------------------------------------
   BlackBytesExample    155.594k (± 5.6%) i/s -    775.720k in   5.003031s
Run options: --seed 31728

# Running:

.........

Finished in 0.002388s, 3768.2667 runs/s, 3768.2667 assertions/s.

9 runs, 9 assertions, 0 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment