Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with this Cloud9 Ruby programming project. In a game of rock-paper-scissors, each player chooses to play Rock (R), Paper (P), or Scissors (S).

Please help with this Cloud9 Ruby programming project.

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 a round of rock-paper-scissors, each player's name and strategy is encoded as an array of two elements

[ "Armando", "P" ] # => Armando plays Paper [ "Dave", "S" ] # => Dave plays Scissors 

(In this example, Dave would win because Scissors cuts Paper.)

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(['Armando','P'], ['Dave','S']) # => ['Dave','S'] 

If either player's strategy is something other than "R", "P" or "S" (case-SENSITIVE), the method should raise aRockPaperScissors::NoSuchStrategyError exception and provide the message: "Strategy must be one of R,P,S".

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.

[ [ [ ["Armando", "P"], ["Dave", "S"] ], [ ["Richard", "R"], ["Michael", "S"] ] ], [ [ ["Allen", "S"], ["Omer", "P"] ], [ ["David E.", "R"], ["Richard X.", "P"] ] ] ] 

Under this scenario, Dave would beat Armando (S>P) and Richard would beat Michael (R>S), so Dave and Richard would play (Richard wins since R>S); similarly, Allen would beat Omer, David E. would beat Richard X., and Allen and Richard X. would play (Allen wins since S>P); and finally Richard would beat Allen since R>P. 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 ['Richard', 'R']). You can assume that the array is well formed (that is, there are 2^n 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 1.

class RockPaperScissors
# Exceptions this class can raise:
class NoSuchStrategyError < StandardError ; end
def self.winner(player1, player2)
# YOUR CODE HERE
end
def self.tournament_winner(tournament)
# YOUR CODE HERE
end
end

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

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions

Question

2. What potential barriers would you encourage Samuel to avoid?

Answered: 1 week ago

Question

6. How would you design your ideal position?

Answered: 1 week ago