Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Archie RPS #2122

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ group :test do
gem 'rspec'
gem 'simplecov', require: false
gem 'simplecov-console', require: false
gem 'rack'
end

group :development, :test do
Expand Down
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ PLATFORMS

DEPENDENCIES
capybara
rack
rspec
rubocop (= 1.20)
simplecov
Expand All @@ -98,4 +99,4 @@ RUBY VERSION
ruby 3.0.2p107

BUNDLED WITH
2.2.26
2.3.12
69 changes: 9 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# RPS Challenge
# Rock, Paper, Scissors, Spizard

Instructions


### Instructions
-------

* Feel free to use google, your notes, books, etc. but work on your own
* If you refer to the solution of another coach or student, please put a link to that in your README
* If you have a partial solution, **still check in a partial solution**
* You must submit a pull request to this repo with your code by 9am Monday morning
* Fork this repo, and clone to your local machine
* Run the `gem install bundler` (if you don't have bundler already)
* When installation is complete, run `bundle`

Task
----

Knowing how to build web applications is getting us almost there as web developers!

The Makers Academy Marketing Array ( **MAMA** ) have asked us to provide a game for them. Their daily grind is pretty tough and they need time to steam a little.
It's my third week at Makers.In this challenge I was set the task of creating an interactive Rock, Paper, Scissors app that could be run via Sinatra onto a local host and played on a browser.

Your task is to provide a _Rock, Paper, Scissors_ game for them so they can play on the web with the following user stories:
Here are the user stories:

```
As a marketeer
Expand All @@ -34,53 +33,3 @@ Hints on functionality
- the marketeer can choose one option
- the game will choose a random option
- a winner will be declared


As usual please start by

* Forking this repo
* TEST driving development of your app

[You may find this guide to setting up a new Ruby web project helpful.](https://github.com/makersacademy/course/blob/main/pills/ruby_web_project_setup_list.md)

## Bonus level 1: Multiplayer

Change the game so that two marketeers can play against each other ( _yes there are two of them_ ).

## Bonus level 2: Rock, Paper, Scissors, Spock, Lizard

Use the _special_ rules ( _you can find them here http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock_ )

## Basic Rules

- Rock beats Scissors
- Scissors beats Paper
- Paper beats Rock

In code review we'll be hoping to see:

* All tests passing
* High [Test coverage](https://github.com/makersacademy/course/blob/main/pills/test_coverage.md) (>95% is good)
* The code is elegant: every class has a clear responsibility, methods are short etc.

Reviewers will potentially be using this [code review rubric](docs/review.md). Referring to this rubric in advance may make the challenge somewhat easier. You should be the judge of how much challenge you want this at this moment.

Notes on test coverage
----------------------

Please ensure you have the following **AT THE TOP** of your spec_helper.rb in order to have test coverage stats generated
on your pull request:

```ruby
require 'simplecov'
require 'simplecov-console'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
# Want a nice code coverage website? Uncomment this next line!
# SimpleCov::Formatter::HTMLFormatter
])
SimpleCov.start
```

You can see your test coverage when you run your tests. If you want this in a graphical form, uncomment the `HTMLFormatter` line and see what happens!
34 changes: 34 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'sinatra/base'
require 'sinatra/reloader'
require './lib/game.rb'
require './lib/player.rb'

class RockPaperScissors < Sinatra::Base
configure :development do
register Sinatra::Reloader
end

get '/' do
erb :index
end

post '/named' do
$player_1_name = params[:player_1_name]
redirect '/play'
end

get '/play' do
@player_1_name = $player_1_name
erb :play
end

get '/result' do
@player_choice = params[:choice]
$game = Game.new($player_1_name, @player_choice)
@game = $game
erb :result
end


run! if app_file == $0
end
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative "./app"

run RockPaperScissors
58 changes: 58 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Game

attr_reader :player, :choice, :play, :computer_choice
def initialize(player, choice)
@choice = choice
@player = player
@computer_choice = ['ROCK', 'PAPER', 'SCISSORS', 'SPOCK', 'LIZARD'].sample
end

def choice
@choice.upcase
end


def won?
if self.play == "PLAYER WINS"
self.player + ' wins.'
elsif self.play == "CPU WINS"
'CPU wins.'
elsif self.play == "DRAW"
"It's a draw."
end
end

def play
case
when (self.choice == 'ROCK' && self.computer_choice == 'SCISSORS') ||
(self.choice == 'ROCK' && self.computer_choice == 'LIZARD') ||
(self.choice == 'PAPER' && self.computer_choice == 'SPOCK') ||
(self.choice == 'PAPER' && self.computer_choice == 'ROCK') ||
(self.choice == 'SCISSORS' && self.computer_choice == 'PAPER') ||
(self.choice == 'SCISSORS' && self.computer_choice == 'LIZARD') ||
(self.choice == 'SPOCK' && self.computer_choice == 'SCISSORS') ||
(self.choice == 'SPOCK' && self.computer_choice == 'ROCK') ||
(self.choice == 'LIZARD' && self.computer_choice == 'SPOCK') ||
(self.choice == 'LIZARD' && self.computer_choice == 'PAPER')
"PLAYER WINS"
when (self.computer_choice == 'ROCK' && self.choice == 'SCISSORS') ||
(self.computer_choice == 'ROCK' && self.choice == 'LIZARD') ||
(self.computer_choice == 'PAPER' && self.choice == 'SPOCK') ||
(self.computer_choice == 'PAPER' && self.choice == 'ROCK') ||
(self.computer_choice == 'SCISSORS' && self.choice == 'PAPER') ||
(self.computer_choice == 'SCISSORS' && self.choice == 'LIZARD') ||
(self.computer_choice == 'SPOCK' && self.choice == 'SCISSORS') ||
(self.computer_choice == 'SPOCK' && self.choice == 'ROCK') ||
(self.computer_choice == 'LIZARD' && self.choice == 'SPOCK') ||
(self.computer_choice == 'LIZARD' && self.choice == 'PAPER')
"CPU WINS"
when (self.choice == 'ROCK' && self.computer_choice == 'ROCK') ||
(self.choice == 'PAPER' && self.computer_choice == 'PAPER') ||
(self.choice == 'SCISSORS' && self.computer_choice == 'SCISSORS') ||
(self.choice == 'SPOCK' && self.computer_choice == 'SPOCK') ||
(self.choice == 'LIZARD' && self.computer_choice == 'LIZARD')
"DRAW"
end
end

end
9 changes: 9 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Player

attr_reader :name

def initialize(name)
@name = name
end

end
8 changes: 8 additions & 0 deletions spec/features/enter_names_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
feature 'Enter names' do
scenario 'submitting names' do
visit('/')
fill_in :player_1_name, with: 'Dusty'
click_button 'Submit'
expect(page).to have_content 'Ah, Dusty, you know the drill...'
end
end
Empty file added spec/features/play_zone_spec.rb
Empty file.
10 changes: 10 additions & 0 deletions spec/features/results_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
feature 'display results' do
scenario 'after having submitted r/p/s and comparing to computer result' do
visit('/')
fill_in :player_1_name, with: 'Dusty'
click_button 'Submit'
fill_in :choice, with: 'Rock'
click_button 'Submit'
expect(page).to have_content "Dusty: 'ROCK!'"
end
end
Empty file added spec/features/web_helpers.rb
Empty file.
21 changes: 21 additions & 0 deletions spec/game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'game'

describe Game do
let(:player) { "Dusty" }
subject(:game) {Game.new(player, 'rock')}


it 'when initialized it has the correct number of arguments' do
expect(Game.new(player, 'rock')).to be_a(Game)
end
it 'can return given choice' do
expect(game.choice).to eq 'ROCK'
end
it 'can return given player name' do
expect(game.player).to eq 'Dusty'
end
it 'returns win state' do
expect(game.won?).to be_truthy
end

end
13 changes: 13 additions & 0 deletions spec/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'player'

describe Player do
subject(:player) {Player.new('Dusty')}

it 'when initialized it has the correct number of arguments' do
expect(Player.new('Dusty')).to be_a(Player)
end
it 'can return given name' do
expect(player.name).to eq 'Dusty'
end

end
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
require 'capybara/rspec'
require 'simplecov'
require 'simplecov-console'
require 'features/web_helpers'

ENV['RACK_ENV'] = 'test'

# Bring in the contents of the `app.rb` file. The below is equivalent to: require_relative '../app.rb'
require File.join(File.dirname(__FILE__), '..', 'app.rb')

# Tell Capybara to talk to BookmarkManager
Capybara.app = RockPaperScissors

### the rest of the file ###

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
Expand Down
8 changes: 8 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<form action="/named" method="post">
<label for="enter name">
Enter player name:
<p> </p>
<input type="text" name="player_1_name" id="enter name" required >
</label>
<input type="submit" value="Submit">
</form>
12 changes: 12 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<form action="/result">
Ah, <%= @player_1_name%>, you know the drill...
<br/>
<label for="enter choice">
<p>Rock, Paper, Scissors, Spock, or Lizard?</p>
<p> </p>
<input type="text" name="choice" id="enter choice" placeholder="Enter choice here..." required>
</label>

<input type="submit" value="Submit">
</form>

16 changes: 16 additions & 0 deletions views/result.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<form action="/play">
<%= @game.player%>: '<%= @game.choice %>!'
<br/>
<br/>
CPU: '<%= @game.computer_choice%>!'
<br/>
<br/>
<%= @game.won?%>
<br/>
<br/>

<input type="submit" value="Continue">
</form>
<form action="/">
<input type="submit" value="New Player?">
</form>