Build Rock, Paper, Scissors in Ruby with Player vs Computer Gameplay in Ruby

Rock, paper, scissors may seem like a simple game to build, however it offers a great base case for how to build a rule's engine that can be scaled up for more complex systems. In this guide we'll examine how to build the game so that users can play against the computer.

large

View Video

Summary

Develop a Rock, Paper, Scissors game that allows users to play against the computer.

Exercise File

Code File

Exercise Description

Build a game class that allows users to guess rock, paper, or scissors. Additionally, build a method that generates a random guess from the computer. Finally, pass both guesses through a rule's engine to return who is the winner of the game is. Focus on building a rule's engine that could scale to other rules instead of simply creating a large conditional.

Example Input/Output

# Examples below assume the computer guesses 'paper'

rps = RPS.new(guess: 'rock')
rps.winner_is # => 'Computer wins'

rps = RPS.new(guess: 'Scissors')
rps.winner_is # => 'You win!'

rps = RPS.new(guess: 'paper')
rps.winner_is # => 'Tie'

Real World Usage

This is a popular coding interview question, not because it's overly complex, but because it allows you to demonstrate your problem solving ability, especially as it relates to code flexibility. If you were to build this game and the rule's engine was comprised of a long set of conditionals, as soon as new rules were added to the game, the program would become convoluted and difficult to alter. However if you properly separate the game concerns it becomes easier to manage.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

Jordan Hudgens

I've been a software engineer for the past decade and have traveled the world building applications and training individuals on a wide variety of topics.


View All Posts