Rock Paper Scissors (Lizard Spock)
Problem Description
This is a refactoring kata built around Chapter 21 of “Working Effectively With Legacy Code” by Martin Fowler. Fowler focuses on the problem of having to make changes in many places. While the exercise laid out in the chapter is a great one, it’s a pretty involved refactor and I needed something for a shorter event.
The Problem
In Season Five, Episode 17 of the Big Bang Theory, Sheldon and Barry Kripke are fighting over Professor Rothman’s office. While the dispute is canonically solved by seeing who can bounce a basketball higher, the first proposed contest is a friendly game of “Rock Paper Scissors Lizard Spock.”
Let us imagine an alternate reality in which this game determines who wins the office. Since both Sheldon and Kripke will only ever pick “Spock,” the pair agree to automate the playing of the game and abide by the result. Unfortunately, they can’t find any implementations of RPSLS and the only RPS game on the network is some old Python code Wolowitz side-loaded onto his mechanical hand.
Assignment
Refactor Wolowitz’s horrific code into an implementation of Rock, Paper, Scissors, Lizard, Spock, adding appropriate text outputs to incorporate the additional choices.
Hints
- Start by finding the smallest duplicated structures you can and DRYing them up; work your way up
- There are probably a few classes hiding in here as well
- You can implement the “what-beats-what” chain as a series of “if” statements, but how would your implementation change if you expected to add new “throws” every few weeks?