Created
April 24, 2017 18:45
-
-
Save danielpclark/63dbf528186c1e1d0fe0e8a65c39e6dc to your computer and use it in GitHub Desktop.
Rock Paper Scissors Black Bytes blog version
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results