Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Cloud9 Ruby Project Specs: spec/rock_paper_scissors_spec.rb In a game of rock-paper-scissors, each player chooses to play Rock (R), Paper (P), or Scissors (S). The rules are:

Cloud9 Ruby Project

Specs: spec/rock_paper_scissors_spec.rb

In a game of rock-paper-scissors, each player chooses to play Rock (R), Paper (P), or Scissors (S). The rules are: Rock breaks Scissors, Scissors cuts Paper, but Paper covers Rock. (In other words, Rock wins over Scissors, Scissors wins over Paper, and Paper wins over Rock.)

In a round of rock-paper-scissors, each player's name and strategy is encoded as an array of two elements

[ ["Kristen", "P"], ["Jacob", "S"] ] # Jacob would win since S > P

A: Game Winner:

Create a RockPaperScissors class with a class method winner that takes two 2-element arrays like those above, and returns the one representing the winner:

RockPaperScissors.winner(['Kristen,'P'], ['Jacob','S']) # => ['Jacob','S']

If either player's strategy is something other than "R", "P" or "S" (case-insensitive), the method should raise a RockPaperScissors::NoSuchStrategyError exception.

If both players use the same strategy, the first player is the winner.

B: Tournament:

A rock-paper-scissors tournament is encoded as an array of games - that is, each element can be considered its own tournament.

[ [ [ ["Kristen", "P"], ["Ben", "S"] ], [ ["Justin", "R"], ["Walter", "S"] ], ], [ [ ["Allen", "S"], ["Omer", "P"] ], [ ["David", "R"], ["Richard", "P"] ] ] ]

In the tournament above Kristen will always play P and Ben will always play S. This tournament plays out as follows:

Under this scenario, Ben would beat Kristen (S>P) and Justin would beat Walter (R>S), so Ben and Justin would play (Justin wins since R>S); similarly, Allen would beat Omer, Richard would beat David, and Allen and Richard would play (Allen wins since S>P); and finally Justin would beat Allen since R>S. That is, pairwise play continues until there is only a single winner.

Write a method `RockPaperScissors.tournament_winner' that takes a tournament encoded as an array and returns the winner (for the above example, it should return ['Justin', 'R']). You can assume that the array is well formed (that is, there are 2n players, and each one participates in exactly one match per round).

HINT: Formulate the problem as a recursive one whose base case you solved in part A.

Select one of the rspec tests, copy it, and modify it so that it tests a feature of the rock-paper-scissors library you have implemented. Alternatively, you can write your own test.

Finally, go to your text file and summarize the purpose of each rspec test (including your added test).

image text in transcribedimage text in transcribed

1 class RockPaperScissors 3 4 # Exceptions this class can raise: 4 class NoSuchStrategyError

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions