Last active
February 2, 2025 04:19
-
-
Save mkaschenko/8cc814cd78468a1c1515 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 GameElementsFactory | |
| def self.sample(type = nil) | |
| selected_type = if type.nil? | |
| self.types.values.sample | |
| else | |
| self.types.fetch(type) | |
| end | |
| selected_type.new | |
| end | |
| def self.types | |
| { 'rock' => Rock, | |
| 'scissors' => Scissors, | |
| 'paper' => Paper } | |
| end | |
| end | |
| class GameElement | |
| include Comparable | |
| def <=>(other) | |
| rules[other.class.name] | |
| end | |
| def rules | |
| raise 'Redefine me in subclasses' | |
| end | |
| def to_s | |
| self.class.name | |
| end | |
| end | |
| class Rock < GameElement | |
| def rules | |
| { 'Scissors' => 1, 'Paper' => -1, 'Rock' => 0 } | |
| end | |
| end | |
| class Scissors < GameElement | |
| def rules | |
| { 'Paper' => 1, 'Rock' => -1, 'Scissors' => 0 } | |
| end | |
| end | |
| class Paper < GameElement | |
| def rules | |
| { 'Rock' => 1, 'Scissors' => -1, 'Paper' => 0 } | |
| end | |
| end |
This file contains hidden or 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
| describe GameElementsFactory do | |
| subject(:game_elements_factory) { described_class } | |
| let(:rock) { Rock.new } | |
| let(:scissors) { Scissors.new } | |
| let(:paper) { Paper.new } | |
| describe '.sample' do | |
| it 'returns a rock' do | |
| expect(game_elements_factory.sample('rock')).to eq(rock) | |
| end | |
| it 'returns a scissors' do | |
| expect(game_elements_factory.sample('scissors')).to eq(scissors) | |
| end | |
| it 'returns a paper' do | |
| expect(game_elements_factory.sample('paper')).to eq(paper) | |
| end | |
| it 'returns any game element' do | |
| expect(game_elements_factory.sample).to eq(rock).or eq(scissors).or eq(paper) | |
| end | |
| end | |
| end | |
| describe Rock do | |
| subject(:rock) { GameElementsFactory.sample('rock') } | |
| describe '#<=>' do | |
| it 'crashes scissors' do | |
| scissors = GameElementsFactory.sample('scissors') | |
| expect(rock <=> scissors).to eq(1) | |
| end | |
| it 'is covered by paper' do | |
| paper = GameElementsFactory.sample('paper') | |
| expect(rock <=> paper).to eq(-1) | |
| end | |
| it 'is equal to rock' do | |
| another_rock = GameElementsFactory.sample('rock') | |
| expect(rock <=> another_rock).to eq(0) | |
| end | |
| end | |
| end | |
| describe Scissors do | |
| subject(:scissors) { GameElementsFactory.sample('scissors') } | |
| describe '#<=>' do | |
| it 'cuts paper' do | |
| paper = GameElementsFactory.sample('paper') | |
| expect(scissors <=> paper).to eq(1) | |
| end | |
| it 'is crashed by rock' do | |
| rock = GameElementsFactory.sample('rock') | |
| expect(scissors <=> rock).to eq(-1) | |
| end | |
| it 'is equal to sissors' do | |
| another_scissors = GameElementsFactory.sample('scissors') | |
| expect(scissors <=> another_scissors).to eq(0) | |
| end | |
| end | |
| end | |
| describe Paper do | |
| subject(:paper) { GameElementsFactory.sample('paper') } | |
| describe '#<=>' do | |
| it 'covers rock' do | |
| rock = GameElementsFactory.sample('rock') | |
| expect(paper <=> rock).to eq(1) | |
| end | |
| it 'is cut by scissors' do | |
| scissors = GameElementsFactory.sample('scissors') | |
| expect(paper <=> scissors).to eq(-1) | |
| end | |
| it 'is equal to paper' do | |
| another_paper = GameElementsFactory.sample('paper') | |
| expect(paper <=> another_paper).to eq(0) | |
| end | |
| end | |
| end |
This file contains hidden or 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 RockScissorsPaper | |
| def self.start | |
| the_first_element = GameElementsFactory.sample | |
| the_second_element = GameElementsFactory.sample | |
| case | |
| when the_first_element > the_second_element | |
| puts "#{the_first_element} wins #{the_second_element}" | |
| when the_first_element == the_second_element | |
| puts "A draw between #{the_first_element} and #{the_second_element}. Another turn..." | |
| start | |
| when the_first_element < the_second_element | |
| puts "#{the_first_element} loses #{the_second_element}" | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment