-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Rmorbey
wants to merge
5
commits into
makersacademy:main
Choose a base branch
from
Rmorbey:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a68454e
Rock Paper Scissors. All tests pass. Two User stories complete.
Rmorbey f4c51e1
Updated README.
Rmorbey 084f772
Made Changes to README
Rmorbey 7c7e986
Added Planning to README
Rmorbey 8dab386
Changed how engine works. Added spock/lizard special rules. Added tex…
Rmorbey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
run! if app_file == $0 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require_relative "./app" | ||
|
||
run RPS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.