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

Russells - Rock/Paper/Scissors Challenge #2110

Open
wants to merge 5 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ source 'https://rubygems.org'
ruby '3.0.2'

gem 'sinatra'
gem 'sinatra-contrib'
gem 'launchy'
gem 'webrick'

group :test do
gem 'capybara'
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ GEM
xpath (~> 3.2)
diff-lcs (1.4.4)
docile (1.4.0)
launchy (2.5.0)
addressable (~> 2.7)
mini_mime (1.1.1)
mini_portile2 (2.6.1)
multi_json (1.15.0)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nokogiri (1.12.3)
Expand Down Expand Up @@ -76,10 +79,17 @@ GEM
rack (~> 2.2)
rack-protection (= 2.1.0)
tilt (~> 2.0)
sinatra-contrib (2.1.0)
multi_json
mustermann (~> 1.0)
rack-protection (= 2.1.0)
sinatra (= 2.1.0)
tilt (~> 2.0)
terminal-table (3.0.1)
unicode-display_width (>= 1.1.1, < 3)
tilt (2.0.10)
unicode-display_width (2.0.0)
webrick (1.7.0)
xpath (3.2.0)
nokogiri (~> 1.8)

Expand All @@ -88,11 +98,14 @@ PLATFORMS

DEPENDENCIES
capybara
launchy
rspec
rubocop (= 1.20)
simplecov
simplecov-console
sinatra
sinatra-contrib
webrick

RUBY VERSION
ruby 3.0.2p107
Expand Down
55 changes: 50 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
# RPS Challenge

Instructions
Russell's - Rock . Paper . Scissors

Play Rock, Paper, Scissors against a Robot and find out who will win.
-------

* Clone from this repo: https://github.com/Rmorbey/rps-challenge.git
* Run bundle install in the project root folder
* Type rackup, which will create a local server to play the game.
* Go to http://localhost:9292
* Choose Single player
* Enter your name
* Pick either: Rock . Paper . Scissors
* Find out the results.
* Hit Play again?

-------
Recent changes since initial pull request - refactored some things

* 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
* Added Spock/Lizard special rules
* Added text to show what the player chose, and what the robot chose so you can verify the outcome.
* Changed how my engine works
* Some of the unit spec tests are no longer working, an issue with srand I believe, that I can't figure out currently.

-------

Planning

---------------------------- ----------------------------
| Rock / Paper / Scissors | | Rock / Paper / Scissors |
| | | |
| Player vs Robot | | Submit name to play |
| _____________ | | ______ |
| |Single Player| | | _________ |Submit| |
| ------------- | | ------ |
| | | |
| | | |
| | | |
---------------------------- ----------------------------
1 2
---------------------------- ----------------------------
| Rock / Paper / Scissors | | Rock / Paper / Scissors |
| | | |
| Russell vs. Robot | | Russell vs. Robot |
| | | |
| Choose | | The results are: You win.|
| |Rock| | | |
| | | ____________ |
| |Paper| | | |Play again?| |
| | | ------------ |
| |Scissors| | | |
---------------------------- ----------------------------
3 4
Task

----

Knowing how to build web applications is getting us almost there as web developers!
Expand Down
86 changes: 86 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require 'sinatra/base'
require 'sinatra/reloader'
require_relative './lib/rock_paper_scissors'

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

enable :sessions

get '/' do
erb :index
end

post '/single' do
erb :single
end

post '/names' do
session[:player_1_name] = params[:player_1_name]
redirect '/pvr'
end

get '/pvr' do
@player_1_name = session[:player_1_name]
erb :pvr
end

post '/rock' do
rps = RockPaperScissors.new(guess: 'rock')
$player_guess = rps.view_player
$robot_guess = rps.view_computer
$pvr_results = rps.winner_is
redirect '/pvr_results'
end

post '/paper' do
rps = RockPaperScissors.new(guess: 'paper')
$player_guess = rps.view_player
$robot_guess = rps.view_computer
$pvr_results = rps.winner_is
redirect '/pvr_results'
end

post '/scissors' do
rps = RockPaperScissors.new(guess: 'scissors')
$player_guess = rps.view_player
$robot_guess = rps.view_computer
$pvr_results = rps.winner_is
redirect '/pvr_results'
end

post '/spock' do
rps = RockPaperScissors.new(guess: 'spock')
$player_guess = rps.view_player
$robot_guess = rps.view_computer
$pvr_results = rps.winner_is
redirect '/pvr_results'
end

post '/lizard' do
rps = RockPaperScissors.new(guess: 'lizard')
$player_guess = rps.view_player
$robot_guess = rps.view_computer
$pvr_results = rps.winner_is
redirect '/pvr_results'
end

get '/pvr_results' do
@player_1_name = session[:player_1_name]
@player_guess = $player_guess
@robot_guess = $robot_guess
@pvr_results = $pvr_results
erb :pvrresults
end

post '/pvr_again' do
@player_guess = nil
@robot_guess = nil
@pvr_results = nil
redirect '/pvr'
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You trigger a different route depending on what move the player chooses, and each of these routes runs the same five lines of code - there's an opportunity here to re-use existing code, keeping your implementation DRY.

An alternative approach would pass the player's chosen move as a param instead, then passing this into sessions or an instance of Player, for example.


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 RPS
49 changes: 49 additions & 0 deletions lib/rock_paper_scissors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class RockPaperScissors

def initialize(guess:)
@guess = guess.to_sym
@computer_guess = computer_guess.to_sym
end

def computer_guess
computer_guesses = %w{rock paper scissors lizard spock}
computer_guesses.sample
end

def winner_is
case
when @guess == @computer_guess
'Tie'
when @guess == :rock && @computer_guess == :scissors ||
@guess == :rock && @computer_guess == :lizard ||
@guess == :paper && @computer_guess == :rock ||
@guess == :paper && @computer_guess == :spock ||
@guess == :scissors && @computer_guess == :paper ||
@guess == :scissors && @computer_guess == :lizard ||
@guess == :lizard && @computer_guess == :paper ||
@guess == :lizard && @computer_guess == :spock ||
@guess == :spock && @computer_guess == :scissors ||
@guess == :spock && @computer_guess == :rock
'You win'
when @computer_guess == :rock && @guess == :scissors ||
@computer_guess == :rock && @guess == :lizard ||
@computer_guess == :paper && @guess == :rock ||
@computer_guess == :paper && @guess == :spock ||
@computer_guess == :scissors && @guess == :paper ||
@computer_guess == :scissors && @guess == :lizard ||
@computer_guess == :lizard && @guess == :paper ||
@computer_guess == :lizard && @guess == :spock ||
@computer_guess == :spock && @guess == :scissors ||
@computer_guess == :spock && @guess == :rock
'Robot wins'
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few approaches to the this game logic, and although your logic is easy to read, an alternative approach could be more concise: https://github.com/makersacademy/rps-challenge/blob/main/docs/review.md#use-of-ifelsif-conditionals-for-business-logic

end

def view_computer
@computer_guess
end

def view_player
@guess
end
end
11 changes: 11 additions & 0 deletions spec/feature/home_page_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "capybara/rspec"
require_relative "../../app"

Capybara.app = RPS

feature 'Player vs Robot' do
scenario 'Choosing Single Player' do
visit('/')
click_button 'Single Player'
end
end
14 changes: 14 additions & 0 deletions spec/feature/names_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "capybara/rspec"
require_relative "../../app"

Capybara.app = RPS

feature 'Enter name' do
scenario 'submitting name' do
visit('/')
click_button 'Single Player'
fill_in :player_1_name, with: "Russell"
click_button 'Submit'
expect(page).to have_content "Russell vs. Robot"
end
end
19 changes: 19 additions & 0 deletions spec/feature/pvr_again_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "capybara/rspec"
require_relative "../../app"

Capybara.app = RPS

feature 'Play again?' do
scenario 'play again? should redirect to /pvr page' do
visit('/')
click_button 'Single Player'
fill_in :player_1_name, with: "Russell"
click_button 'Submit'
expect(page).to have_content "Russell vs. Robot"
click_button 'Rock'
click_button 'Play again?'
expect(page).to have_button 'Rock'
expect(page).to have_button 'Paper'
expect(page).to have_button 'Scissors'
end
end
16 changes: 16 additions & 0 deletions spec/feature/pvr_results_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "capybara/rspec"
require_relative "../../app"

Capybara.app = RPS

feature 'Play again?' do
scenario 'Choosing play again? button' do
visit('/')
click_button 'Single Player'
fill_in :player_1_name, with: "Russell"
click_button 'Submit'
expect(page).to have_content "Russell vs. Robot"
click_button 'Rock'
click_button 'Play again?'
end
end
33 changes: 33 additions & 0 deletions spec/feature/pvr_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "capybara/rspec"
require_relative "../../app"

Capybara.app = RPS

feature 'Choose R/P/S' do
scenario 'Choosing rock' do
visit('/')
click_button 'Single Player'
fill_in :player_1_name, with: "Russell"
click_button 'Submit'
expect(page).to have_content "Russell vs. Robot"
click_button 'Rock'
end

scenario 'Choosing paper' do
visit('/')
click_button 'Single Player'
fill_in :player_1_name, with: "Russell"
click_button 'Submit'
expect(page).to have_content "Russell vs. Robot"
click_button 'Paper'
end

scenario 'Choosing scissors' do
visit('/')
click_button 'Single Player'
fill_in :player_1_name, with: "Russell"
click_button 'Submit'
expect(page).to have_content "Russell vs. Robot"
click_button 'Scissors'
end
end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
ENV['RACK_ENV'] = 'test'

require File.join(File.dirname(__FILE__), '..', 'app.rb')

require 'capybara'
require 'rspec'

Capybara.app = RPS

require 'capybara/rspec'
require 'simplecov'
require 'simplecov-console'
Expand Down
Loading