diff --git a/Gemfile b/Gemfile index 63415039a7..aea1788a09 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 5afab6d697..c93a28e27b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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 diff --git a/README.md b/README.md index 3bd26e2d7d..f7cb5a2e21 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/app.rb b/app.rb new file mode 100644 index 0000000000..80cccb2796 --- /dev/null +++ b/app.rb @@ -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 diff --git a/config.ru b/config.ru new file mode 100644 index 0000000000..8eb50192d3 --- /dev/null +++ b/config.ru @@ -0,0 +1,3 @@ +require_relative "./app" + +run RPS diff --git a/lib/rock_paper_scissors.rb b/lib/rock_paper_scissors.rb new file mode 100644 index 0000000000..a21642bae1 --- /dev/null +++ b/lib/rock_paper_scissors.rb @@ -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 + end + + def view_computer + @computer_guess + end + + def view_player + @guess + end +end diff --git a/spec/feature/home_page_spec.rb b/spec/feature/home_page_spec.rb new file mode 100644 index 0000000000..7c20953c53 --- /dev/null +++ b/spec/feature/home_page_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/feature/names_spec.rb b/spec/feature/names_spec.rb new file mode 100644 index 0000000000..ee5a580601 --- /dev/null +++ b/spec/feature/names_spec.rb @@ -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 diff --git a/spec/feature/pvr_again_spec.rb b/spec/feature/pvr_again_spec.rb new file mode 100644 index 0000000000..9f3673d5ad --- /dev/null +++ b/spec/feature/pvr_again_spec.rb @@ -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 diff --git a/spec/feature/pvr_results_spec.rb b/spec/feature/pvr_results_spec.rb new file mode 100644 index 0000000000..1405b26063 --- /dev/null +++ b/spec/feature/pvr_results_spec.rb @@ -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 diff --git a/spec/feature/pvr_spec.rb b/spec/feature/pvr_spec.rb new file mode 100644 index 0000000000..53e0e3b7e1 --- /dev/null +++ b/spec/feature/pvr_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 11a50a94e5..aafaf9a8b7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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' diff --git a/spec/unit/rock_paper_scissors_spec.rb b/spec/unit/rock_paper_scissors_spec.rb new file mode 100644 index 0000000000..3af2da939d --- /dev/null +++ b/spec/unit/rock_paper_scissors_spec.rb @@ -0,0 +1,34 @@ +require './lib/rock_paper_scissors' + +describe RockPaperScissors do + + it 'paper beats rock' do + rps = RockPaperScissors.new(guess: 'rock') + srand(1) + expect(rps.winner_is).to eq('Robot wins') + end + + it 'paper loses to scissors' do + rps = RockPaperScissors.new(guess: 'scissors') + srand(1) + expect(rps.winner_is).to eq('You win') + end + + it 'paper ties with paper' do + rps = RockPaperScissors.new(guess: 'paper') + srand(1) + expect(rps.winner_is).to eq('Tie') + end + + it 'paper beats spock' do + rps = RockPaperScissors.new(guess: 'spock') + srand(1) + expect(rps.winner_is).to eq('Robot wins') + end + + it 'paper loses to lizard' do + rps = RockPaperScissors.new(guess: 'lizard') + srand(1) + expect(rps.winner_is).to eq('You win') + end +end diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000000..8c5ba05dd2 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,20 @@ + + + + + + Rock/Paper/Scissors + + +

Rock / Paper / Scissors

+ + +

Player vs. Robot!

+ + +
+ +
+ + + diff --git a/views/pvr.erb b/views/pvr.erb new file mode 100644 index 0000000000..48084e02e2 --- /dev/null +++ b/views/pvr.erb @@ -0,0 +1,24 @@ +

Rock/Paper/Scissors!

+

<%= @player_1_name %> vs. Robot

+ +

Choose

+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
diff --git a/views/pvrresults.erb b/views/pvrresults.erb new file mode 100644 index 0000000000..cfc249eaee --- /dev/null +++ b/views/pvrresults.erb @@ -0,0 +1,14 @@ +

Rock/Paper/Scissors!

+

<%= @player_1_name %> vs. Robot

+ +

<%= @results%>

+ +

<%= @player_1_name %> chose: <%= @player_guess %>

+ +

Robot chose: <%= @robot_guess %>

+ +

The Results are:<%= @pvr_results %>

+ +
+ +
diff --git a/views/single.erb b/views/single.erb new file mode 100644 index 0000000000..2ede924b14 --- /dev/null +++ b/views/single.erb @@ -0,0 +1,9 @@ +

Rock/Paper/Scissors!

+

Submit name to play

+
+ + +