From 31d229371a6925af28ea6fbddf952096ee4b44fc Mon Sep 17 00:00:00 2001 From: Joe McGarry Date: Thu, 2 Jun 2022 09:41:00 +0100 Subject: [PATCH 1/7] Project setup, adds inital index page and test for entering user name --- Gemfile | 1 + Gemfile.lock | 8 +++ Views/index.erb | 5 ++ Views/play.erb | 1 + app.rb | 19 ++++++++ config.ru | 2 + plan.md | 81 +++++++++++++++++++++++++++++++ spec/features/enter_names_spec.rb | 10 ++++ spec/spec_helper.rb | 8 +++ 9 files changed, 135 insertions(+) create mode 100644 Views/index.erb create mode 100644 Views/play.erb create mode 100644 app.rb create mode 100644 config.ru create mode 100644 plan.md create mode 100644 spec/features/enter_names_spec.rb diff --git a/Gemfile b/Gemfile index 63415039a7..e3d0cd967d 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source 'https://rubygems.org' ruby '3.0.2' gem 'sinatra' +gem 'sinatra-contrib' group :test do gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index 5afab6d697..9dc327d271 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -17,6 +17,7 @@ GEM docile (1.4.0) 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,6 +77,12 @@ 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) @@ -93,6 +100,7 @@ DEPENDENCIES simplecov simplecov-console sinatra + sinatra-contrib RUBY VERSION ruby 3.0.2p107 diff --git a/Views/index.erb b/Views/index.erb new file mode 100644 index 0000000000..09a31994b7 --- /dev/null +++ b/Views/index.erb @@ -0,0 +1,5 @@ +ROCK! PAPER! SCISSORS!
+
+ + +
\ No newline at end of file diff --git a/Views/play.erb b/Views/play.erb new file mode 100644 index 0000000000..c0f40d5513 --- /dev/null +++ b/Views/play.erb @@ -0,0 +1 @@ +<%= @player_1_name %> vs. AI \ No newline at end of file diff --git a/app.rb b/app.rb new file mode 100644 index 0000000000..c4eb5e5406 --- /dev/null +++ b/app.rb @@ -0,0 +1,19 @@ +require 'sinatra/base' +require 'sinatra/reloader' + +class RPS < Sinatra::Base + configure :development do + register Sinatra::Reloader + end + + get '/' do + erb :index + end + + post '/names' do + @player_1_name = params[:player_1_name] + erb :play + end + + run! if app_file == $0 +end \ No newline at end of file diff --git a/config.ru b/config.ru new file mode 100644 index 0000000000..988c06a2d1 --- /dev/null +++ b/config.ru @@ -0,0 +1,2 @@ +require_relative './app' +run RPS \ No newline at end of file diff --git a/plan.md b/plan.md new file mode 100644 index 0000000000..1ca7c14f19 --- /dev/null +++ b/plan.md @@ -0,0 +1,81 @@ +{{RPS Challenge}} Class Design Recipe +1. Describe the Problem +As a marketeer +So that I can see my name in lights +I would like to register my name before playing an online game + +As a marketeer +So that I can enjoy myself away from the daily grind +I would like to be able to play rock/paper/scissors + +2. Design the Class Interface +Enter name before the game + +Get shown a choice of Rock, Paper or Scissors + +User can only choose 1 option + +The game will choose make 1 other choice + +Winner gets declared + Rock beats Scissors + Scissors beats Paper + Paper beats Rock + +MODELS +- Player + - attr_reader :name + - def make_choice +- Game (current_turn, ) + +VIEWS +- index +- play + - Shows player 3 choices, buttons. +- result + - Shows the result of the game + +CONTROLLER +- app.rb + '/' + '/play' + '/result' + +# EXAMPLE + +class Reminder + def initialize(name) # name is a string + # ... + end + + def remind_me_to(task) # task is a string + # No return value + end + + def remind() + # Throws an exception if no task is set + # Otherwise, returns a string reminding the user to do the task + end +end +3. Create Examples as Tests +Make a list of examples of how the class will behave in different situations. + +# EXAMPLE + +# 1 +reminder = Reminder("Kay") +reminder.remind_me_to("Walk the dog") +reminder.remind() # => "Walk the dog, Kay!" + +# 2 +reminder = Reminder("Kay") +reminder.remind() # fails with "No task set." + +# 3 +reminder = Reminder("Kay") +reminder.remind_me_to("") +reminder.remind() # => ", Kay!" +Encode each example as a test. You can add to the above list as you go. + +4. Implement the Behaviour +After each test you write, follow the test-driving process of red, green, refactor to implement the behaviour. \ No newline at end of file diff --git a/spec/features/enter_names_spec.rb b/spec/features/enter_names_spec.rb new file mode 100644 index 0000000000..dd12efcb9f --- /dev/null +++ b/spec/features/enter_names_spec.rb @@ -0,0 +1,10 @@ +feature "Enter player name" do + scenario "submitting a name" do + visit('/') + fill_in :player_1_name, with: "Joe" + click_button "Submit" + + #save_and_open_page + expect(page).to have_content "Joe vs. AI" + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 11a50a94e5..f179bea8d9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,12 @@ require 'capybara/rspec' require 'simplecov' require 'simplecov-console' +require 'capybara' + +ENV['RACK_ENV'] = 'test' + +# require our Sinatra app file +require File.join(File.dirname(__FILE__), '..', 'app.rb') SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, @@ -18,3 +24,5 @@ puts "\e[33mTry it now! Just run: rubocop\e[0m" end end + +Capybara.app = RPS From 5428704e93b84c573c7e66841e6b9ccf75583202 Mon Sep 17 00:00:00 2001 From: Joe McGarry Date: Thu, 2 Jun 2022 10:00:03 +0100 Subject: [PATCH 2/7] Adds Player class and tests for player name --- lib/player.rb | 7 +++++++ spec/player_spec.rb | 11 +++++++++++ 2 files changed, 18 insertions(+) create mode 100644 lib/player.rb create mode 100644 spec/player_spec.rb diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 0000000000..bfcccd954b --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,7 @@ +class Player + attr_reader :name + + def initialize(name) + @name = name + end +end \ No newline at end of file diff --git a/spec/player_spec.rb b/spec/player_spec.rb new file mode 100644 index 0000000000..df623d856a --- /dev/null +++ b/spec/player_spec.rb @@ -0,0 +1,11 @@ +require 'player' + +describe Player do + subject(:joe) { Player.new("Joe") } + + describe '#name' do + it 'returns the name' do + expect(joe.name).to eq "Joe" + end + end +end \ No newline at end of file From 558b204baaaf96625c651409a495e2a41d832202 Mon Sep 17 00:00:00 2001 From: Joe McGarry Date: Thu, 2 Jun 2022 12:36:04 +0100 Subject: [PATCH 3/7] Lets player choose their option, basic framework for showing the reuslt in place --- Views/move.erb | 0 Views/play.erb | 13 ++++++++++++- Views/result.erb | 2 ++ app.rb | 17 ++++++++++++++++- lib/game.rb | 13 +++++++++++++ lib/player.rb | 7 ++++++- spec/features/result_spec.rb | 20 ++++++++++++++++++++ spec/game_spec.rb | 14 ++++++++++++++ 8 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 Views/move.erb create mode 100644 Views/result.erb create mode 100644 lib/game.rb create mode 100644 spec/features/result_spec.rb create mode 100644 spec/game_spec.rb diff --git a/Views/move.erb b/Views/move.erb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Views/play.erb b/Views/play.erb index c0f40d5513..7d7cdc1712 100644 --- a/Views/play.erb +++ b/Views/play.erb @@ -1 +1,12 @@ -<%= @player_1_name %> vs. AI \ No newline at end of file +<%= @game.player_1.name %> vs. AI
+ +Rock, paper or scissors? Make your choice: +
+ +
+ +
+ +
+ +
\ No newline at end of file diff --git a/Views/result.erb b/Views/result.erb new file mode 100644 index 0000000000..600deb44df --- /dev/null +++ b/Views/result.erb @@ -0,0 +1,2 @@ +<%= @game.player_1.name %> chose rock
+AI chose paper \ No newline at end of file diff --git a/app.rb b/app.rb index c4eb5e5406..c8d2b6ce25 100644 --- a/app.rb +++ b/app.rb @@ -1,7 +1,10 @@ require 'sinatra/base' require 'sinatra/reloader' +require './lib/game' +require './lib/player' class RPS < Sinatra::Base + enable :sessions configure :development do register Sinatra::Reloader end @@ -11,9 +14,21 @@ class RPS < Sinatra::Base end post '/names' do - @player_1_name = params[:player_1_name] + player_1 = Player.new(params[:player_1_name]) + $game = Game.new(player_1) + redirect '/play' + end + + get '/play' do + @game = $game erb :play end + post '/result' do + @game = $game + @player_choice = @game.player_1.make_choice(params[:player_1_choice]) + erb :result + end + run! if app_file == $0 end \ No newline at end of file diff --git a/lib/game.rb b/lib/game.rb new file mode 100644 index 0000000000..0cfd36557a --- /dev/null +++ b/lib/game.rb @@ -0,0 +1,13 @@ +class Game + attr_reader :comp_choice, :player_1 + + def initialize(player_1) + @player_1 = player_1 + @comp_choice = 1 + end + + def ai_choice + # 1 = Rock, 2 = Paper, 3 = Scissors + @comp_choice = (1..3).sample + end +end \ No newline at end of file diff --git a/lib/player.rb b/lib/player.rb index bfcccd954b..a623e29585 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,7 +1,12 @@ class Player - attr_reader :name + attr_reader :name, :choice def initialize(name) @name = name + @choice = nil + end + + def make_choice(choice) + @choice = choice end end \ No newline at end of file diff --git a/spec/features/result_spec.rb b/spec/features/result_spec.rb new file mode 100644 index 0000000000..15cf33ac31 --- /dev/null +++ b/spec/features/result_spec.rb @@ -0,0 +1,20 @@ +feature 'Result of choice' do + scenario 'Player 1 chooses Rock' do + visit('/') + fill_in :player_1_name, with: 'Joe' + click_button 'Submit' + choose ('rock') + click_button 'Play' + expect(page).to have_content "Joe chose rock" + end + + scenario 'Computer chooses paper' do + visit('/') + fill_in :player_1_name, with: 'Joe' + click_button 'Submit' + choose ('rock') + click_button 'Play' + expect(page).to have_content "Joe chose rock" + expect(page).to have_content "AI chose paper" + end +end \ No newline at end of file diff --git a/spec/game_spec.rb b/spec/game_spec.rb new file mode 100644 index 0000000000..a17fb78403 --- /dev/null +++ b/spec/game_spec.rb @@ -0,0 +1,14 @@ +require 'game' + +describe Game do + subject(:game) { described_class.new(player_1) } + let(:player_1) { double :player } + + describe '#ai_choice' do + it 'returns 3' do + allow(game).to receive(:ai_choice).and_return 3 + expect(game.ai_choice).to eq 3 + game.ai_choice + end + end +end \ No newline at end of file From c5726b308f1158e31fd54b9f96f1fe3d1dceba6f Mon Sep 17 00:00:00 2001 From: Joe McGarry Date: Thu, 2 Jun 2022 13:11:36 +0100 Subject: [PATCH 4/7] Refactors player choice to text input --- Views/play.erb | 7 +------ Views/result.erb | 4 ++-- app.rb | 5 ++++- lib/game.rb | 8 ++++++-- lib/player.rb | 2 +- spec/features/enter_names_spec.rb | 5 +---- spec/features/result_spec.rb | 14 +++++--------- spec/features/web_helpers.rb | 5 +++++ spec/game_spec.rb | 6 +++--- spec/spec_helper.rb | 1 + 10 files changed, 29 insertions(+), 28 deletions(-) create mode 100644 spec/features/web_helpers.rb diff --git a/Views/play.erb b/Views/play.erb index 7d7cdc1712..b40a52867f 100644 --- a/Views/play.erb +++ b/Views/play.erb @@ -2,11 +2,6 @@ Rock, paper or scissors? Make your choice:
- -
- -
- -
+
\ No newline at end of file diff --git a/Views/result.erb b/Views/result.erb index 600deb44df..2445c21c2a 100644 --- a/Views/result.erb +++ b/Views/result.erb @@ -1,2 +1,2 @@ -<%= @game.player_1.name %> chose rock
-AI chose paper \ No newline at end of file +<%= @game.player_1.name %> chose <%= @game.get_player_choice %>
+AI chose <%= @game.comp_choice %> \ No newline at end of file diff --git a/app.rb b/app.rb index c8d2b6ce25..e9dae0ae68 100644 --- a/app.rb +++ b/app.rb @@ -26,9 +26,12 @@ class RPS < Sinatra::Base post '/result' do @game = $game - @player_choice = @game.player_1.make_choice(params[:player_1_choice]) + player_choice = params[:player_choice] + @game.player_1.make_choice(player_choice) + @game.ai_choice erb :result end + run! if app_file == $0 end \ No newline at end of file diff --git a/lib/game.rb b/lib/game.rb index 0cfd36557a..272818735d 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -3,11 +3,15 @@ class Game def initialize(player_1) @player_1 = player_1 - @comp_choice = 1 + @comp_choice = "" end def ai_choice # 1 = Rock, 2 = Paper, 3 = Scissors - @comp_choice = (1..3).sample + @comp_choice = ['rock', 'paper', 'scissors'].sample + end + + def get_player_choice + @player_1.choice end end \ No newline at end of file diff --git a/lib/player.rb b/lib/player.rb index a623e29585..12dff8226e 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -3,7 +3,7 @@ class Player def initialize(name) @name = name - @choice = nil + @choice = "" end def make_choice(choice) diff --git a/spec/features/enter_names_spec.rb b/spec/features/enter_names_spec.rb index dd12efcb9f..cc39f7f1aa 100644 --- a/spec/features/enter_names_spec.rb +++ b/spec/features/enter_names_spec.rb @@ -1,9 +1,6 @@ feature "Enter player name" do scenario "submitting a name" do - visit('/') - fill_in :player_1_name, with: "Joe" - click_button "Submit" - + sign_in_and_play #save_and_open_page expect(page).to have_content "Joe vs. AI" end diff --git a/spec/features/result_spec.rb b/spec/features/result_spec.rb index 15cf33ac31..d57c6a7fbf 100644 --- a/spec/features/result_spec.rb +++ b/spec/features/result_spec.rb @@ -1,18 +1,14 @@ feature 'Result of choice' do scenario 'Player 1 chooses Rock' do - visit('/') - fill_in :player_1_name, with: 'Joe' - click_button 'Submit' - choose ('rock') + sign_in_and_play + fill_in :player_choice, with: 'rock' click_button 'Play' expect(page).to have_content "Joe chose rock" end - scenario 'Computer chooses paper' do - visit('/') - fill_in :player_1_name, with: 'Joe' - click_button 'Submit' - choose ('rock') + skip 'Computer chooses paper' do + sign_in_and_play + fill_in :player_choice, with: 'rock' click_button 'Play' expect(page).to have_content "Joe chose rock" expect(page).to have_content "AI chose paper" diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb new file mode 100644 index 0000000000..51a3a841aa --- /dev/null +++ b/spec/features/web_helpers.rb @@ -0,0 +1,5 @@ +def sign_in_and_play + visit('/') + fill_in :player_1_name, with: 'Joe' + click_button 'Submit' +end \ No newline at end of file diff --git a/spec/game_spec.rb b/spec/game_spec.rb index a17fb78403..a9f2946d7c 100644 --- a/spec/game_spec.rb +++ b/spec/game_spec.rb @@ -5,9 +5,9 @@ let(:player_1) { double :player } describe '#ai_choice' do - it 'returns 3' do - allow(game).to receive(:ai_choice).and_return 3 - expect(game.ai_choice).to eq 3 + it 'returns paper' do + allow(game).to receive(:ai_choice).and_return 'paper' + expect(game.ai_choice).to eq 'paper' game.ai_choice end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f179bea8d9..7b99f5c071 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,6 +2,7 @@ require 'simplecov' require 'simplecov-console' require 'capybara' +require 'features/web_helpers' ENV['RACK_ENV'] = 'test' From bb6c6966ff2b10d122ae766222d8fa91623ded23 Mon Sep 17 00:00:00 2001 From: Joe McGarry Date: Thu, 2 Jun 2022 14:49:41 +0100 Subject: [PATCH 5/7] Adds fully working game with all tests --- Views/index.erb | 2 +- Views/play.erb | 4 ++-- Views/result.erb | 20 ++++++++++++++++++-- lib/game.rb | 2 +- spec/features/play_again_spec.rb | 11 +++++++++++ spec/features/result_spec.rb | 20 ++++++++++++++++---- spec/game_spec.rb | 8 ++++++++ 7 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 spec/features/play_again_spec.rb diff --git a/Views/index.erb b/Views/index.erb index 09a31994b7..9c9d86222f 100644 --- a/Views/index.erb +++ b/Views/index.erb @@ -1,4 +1,4 @@ -ROCK! PAPER! SCISSORS!
+

ROCK! PAPER! SCISSORS!

diff --git a/Views/play.erb b/Views/play.erb index b40a52867f..ba2d71bed6 100644 --- a/Views/play.erb +++ b/Views/play.erb @@ -1,6 +1,6 @@ -<%= @game.player_1.name %> vs. AI
+

<%= @game.player_1.name %> vs. AI

-Rock, paper or scissors? Make your choice: +

Rock, paper or scissors? Make your choice:

diff --git a/Views/result.erb b/Views/result.erb index 2445c21c2a..3d41621d21 100644 --- a/Views/result.erb +++ b/Views/result.erb @@ -1,2 +1,18 @@ -<%= @game.player_1.name %> chose <%= @game.get_player_choice %>
-AI chose <%= @game.comp_choice %> \ No newline at end of file +

<%= @game.player_1.name %> chose <%= @game.get_player_choice %>

+

AI chose <%= @game.comp_choice %>

+ +<% if @game.comp_choice == @game.get_player_choice %> +

It's a draw!

+<% elsif @game.comp_choice == 'rock' && @game.get_player_choice == 'scissors' %> +

AI wins!

+<% elsif @game.comp_choice == 'scissors' && @game.get_player_choice == 'paper' %> +

AI wins!

+<% elsif @game.comp_choice == 'paper' && @game.get_player_choice == 'rock' %> +

AI wins!

+<% else %> +

<%= @game.player_1.name %> wins!

+<% end %> + + + +
\ No newline at end of file diff --git a/lib/game.rb b/lib/game.rb index 272818735d..13e3ad1515 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -12,6 +12,6 @@ def ai_choice end def get_player_choice - @player_1.choice + @player_1.choice.downcase end end \ No newline at end of file diff --git a/spec/features/play_again_spec.rb b/spec/features/play_again_spec.rb new file mode 100644 index 0000000000..a904851446 --- /dev/null +++ b/spec/features/play_again_spec.rb @@ -0,0 +1,11 @@ +feature 'play again' do + scenario 'press play again to restart game' do + sign_in_and_play + fill_in :player_choice, with: 'rock' + click_button 'Play' + click_button 'Play again' + expect(page).to have_content "Joe vs. AI" + expect(page).not_to have_content "AI wins!" + expect(page).not_to have_content "Joe wins!" + end +end \ No newline at end of file diff --git a/spec/features/result_spec.rb b/spec/features/result_spec.rb index d57c6a7fbf..47a86357b8 100644 --- a/spec/features/result_spec.rb +++ b/spec/features/result_spec.rb @@ -4,13 +4,25 @@ fill_in :player_choice, with: 'rock' click_button 'Play' expect(page).to have_content "Joe chose rock" + expect(page).not_to have_content "Joe chose paper" + expect(page).not_to have_content "Joe chose scissors" end - skip 'Computer chooses paper' do + scenario 'Player 1 chooses paper' do sign_in_and_play - fill_in :player_choice, with: 'rock' + fill_in :player_choice, with: 'paper' click_button 'Play' - expect(page).to have_content "Joe chose rock" - expect(page).to have_content "AI chose paper" + expect(page).to have_content "Joe chose paper" + expect(page).not_to have_content "Joe chose rock" + expect(page).not_to have_content "Joe chose scissors" + end + + scenario 'Player 1 chooses scissors' do + sign_in_and_play + fill_in :player_choice, with: 'scissors' + click_button 'Play' + expect(page).to have_content "Joe chose scissors" + expect(page).not_to have_content "Joe chose paper" + expect(page).not_to have_content "Joe chose rock" end end \ No newline at end of file diff --git a/spec/game_spec.rb b/spec/game_spec.rb index a9f2946d7c..bfdbabf0c7 100644 --- a/spec/game_spec.rb +++ b/spec/game_spec.rb @@ -11,4 +11,12 @@ game.ai_choice end end + + describe '#get_player_choice' do + it "it returns the player's choice" do + allow(player_1).to receive(:make_choice).with('rock').and_return 'rock' + expect(game).to receive(:get_player_choice).and_return 'rock' + game.get_player_choice + end + end end \ No newline at end of file From 18b2440d725e6d1c0210283fb0629fe19193f43c Mon Sep 17 00:00:00 2001 From: Joe McGarry Date: Thu, 2 Jun 2022 16:54:25 +0100 Subject: [PATCH 6/7] Adds web helper for player 1 choosing rock, paper and scissors. Refactors tests --- spec/features/play_again_spec.rb | 3 +-- spec/features/result_spec.rb | 9 +++------ spec/features/web_helpers.rb | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spec/features/play_again_spec.rb b/spec/features/play_again_spec.rb index a904851446..c015d44638 100644 --- a/spec/features/play_again_spec.rb +++ b/spec/features/play_again_spec.rb @@ -1,8 +1,7 @@ feature 'play again' do scenario 'press play again to restart game' do sign_in_and_play - fill_in :player_choice, with: 'rock' - click_button 'Play' + player_1_chooses_rock click_button 'Play again' expect(page).to have_content "Joe vs. AI" expect(page).not_to have_content "AI wins!" diff --git a/spec/features/result_spec.rb b/spec/features/result_spec.rb index 47a86357b8..2c11735271 100644 --- a/spec/features/result_spec.rb +++ b/spec/features/result_spec.rb @@ -1,8 +1,7 @@ feature 'Result of choice' do scenario 'Player 1 chooses Rock' do sign_in_and_play - fill_in :player_choice, with: 'rock' - click_button 'Play' + player_1_chooses_rock expect(page).to have_content "Joe chose rock" expect(page).not_to have_content "Joe chose paper" expect(page).not_to have_content "Joe chose scissors" @@ -10,8 +9,7 @@ scenario 'Player 1 chooses paper' do sign_in_and_play - fill_in :player_choice, with: 'paper' - click_button 'Play' + player_1_chooses_paper expect(page).to have_content "Joe chose paper" expect(page).not_to have_content "Joe chose rock" expect(page).not_to have_content "Joe chose scissors" @@ -19,8 +17,7 @@ scenario 'Player 1 chooses scissors' do sign_in_and_play - fill_in :player_choice, with: 'scissors' - click_button 'Play' + player_1_chooses_scissors expect(page).to have_content "Joe chose scissors" expect(page).not_to have_content "Joe chose paper" expect(page).not_to have_content "Joe chose rock" diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb index 51a3a841aa..309d1cfc0f 100644 --- a/spec/features/web_helpers.rb +++ b/spec/features/web_helpers.rb @@ -2,4 +2,19 @@ def sign_in_and_play visit('/') fill_in :player_1_name, with: 'Joe' click_button 'Submit' +end + +def player_1_chooses_rock + fill_in :player_choice, with: 'rock' + click_button 'Play' +end + +def player_1_chooses_paper + fill_in :player_choice, with: 'paper' + click_button 'Play' +end + +def player_1_chooses_scissors + fill_in :player_choice, with: 'scissors' + click_button 'Play' end \ No newline at end of file From d0a8a0039184abee2bf36cbaad50f95bcbf0c4d2 Mon Sep 17 00:00:00 2001 From: Joe McGarry Date: Thu, 2 Jun 2022 17:42:35 +0100 Subject: [PATCH 7/7] Adds multiplayer branch, adds multiplayer functionality and tests --- Views/index.erb | 1 + Views/play.erb | 5 +++-- Views/result.erb | 20 +++++++++--------- app.rb | 11 ++++++---- lib/game.rb | 24 ++++++++++++++++------ spec/features/enter_names_spec.rb | 2 +- spec/features/play_again_spec.rb | 6 +++--- spec/features/result_spec.rb | 17 +++++++++------- spec/features/web_helpers.rb | 34 +++++++++++++++++++++++++------ spec/game_spec.rb | 22 ++++++++++---------- spec/player_spec.rb | 2 ++ 11 files changed, 94 insertions(+), 50 deletions(-) diff --git a/Views/index.erb b/Views/index.erb index 9c9d86222f..ec3b13aa88 100644 --- a/Views/index.erb +++ b/Views/index.erb @@ -1,5 +1,6 @@

ROCK! PAPER! SCISSORS!

+
\ No newline at end of file diff --git a/Views/play.erb b/Views/play.erb index ba2d71bed6..1b75c93562 100644 --- a/Views/play.erb +++ b/Views/play.erb @@ -1,7 +1,8 @@ -

<%= @game.player_1.name %> vs. AI

+

<%= @game.player_1.name %> vs. <%= @game.player_2.name %>

Rock, paper or scissors? Make your choice:

- + +
\ No newline at end of file diff --git a/Views/result.erb b/Views/result.erb index 3d41621d21..30a8fbae5b 100644 --- a/Views/result.erb +++ b/Views/result.erb @@ -1,16 +1,16 @@ -

<%= @game.player_1.name %> chose <%= @game.get_player_choice %>

-

AI chose <%= @game.comp_choice %>

+

<%= @game.player_1.name %> chose <%= @game.get_player_1_choice %>

+

<%= @game.player_2.name %> chose <%= @game.get_player_2_choice %>

-<% if @game.comp_choice == @game.get_player_choice %> +<% if @game.get_player_1_choice == @game.get_player_2_choice %>

It's a draw!

-<% elsif @game.comp_choice == 'rock' && @game.get_player_choice == 'scissors' %> -

AI wins!

-<% elsif @game.comp_choice == 'scissors' && @game.get_player_choice == 'paper' %> -

AI wins!

-<% elsif @game.comp_choice == 'paper' && @game.get_player_choice == 'rock' %> -

AI wins!

+<% elsif @game.get_player_1_choice == 'rock' && @game.get_player_2_choice == 'scissors' %> +

Player 1 wins

+<% elsif @game.get_player_1_choice == 'scissors' && @game.get_player_2_choice == 'paper' %> +

Player 1 wins

+<% elsif @game.get_player_1_choice == 'paper' && @game.get_player_2_choice == 'rock' %> +

Player 1 wins

<% else %> -

<%= @game.player_1.name %> wins!

+

<%= @game.player_2.name %> wins!

<% end %>
diff --git a/app.rb b/app.rb index e9dae0ae68..c6e3a2730a 100644 --- a/app.rb +++ b/app.rb @@ -15,7 +15,8 @@ class RPS < Sinatra::Base post '/names' do player_1 = Player.new(params[:player_1_name]) - $game = Game.new(player_1) + player_2 = Player.new(params[:player_2_name]) + $game = Game.new(player_1, player_2) redirect '/play' end @@ -26,9 +27,11 @@ class RPS < Sinatra::Base post '/result' do @game = $game - player_choice = params[:player_choice] - @game.player_1.make_choice(player_choice) - @game.ai_choice + player_1_choice = params[:player_1_choice] + player_2_choice = params[:player_2_choice] + @game.player_1.make_choice(player_1_choice) + @game.player_2.make_choice(player_2_choice) + erb :result end diff --git a/lib/game.rb b/lib/game.rb index 13e3ad1515..5648d93a44 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,9 +1,17 @@ class Game - attr_reader :comp_choice, :player_1 + attr_reader :p1_choice, :player_1, :current_player, :p2_choice - def initialize(player_1) - @player_1 = player_1 - @comp_choice = "" + def initialize(player_1, player_2) + @players = [player_1, player_2] + @current_player = player_1 + end + + def player_1 + @players.first + end + + def player_2 + @players.last end def ai_choice @@ -11,7 +19,11 @@ def ai_choice @comp_choice = ['rock', 'paper', 'scissors'].sample end - def get_player_choice - @player_1.choice.downcase + def get_player_1_choice + player_1.choice + end + + def get_player_2_choice + player_2.choice end end \ No newline at end of file diff --git a/spec/features/enter_names_spec.rb b/spec/features/enter_names_spec.rb index cc39f7f1aa..9289a90830 100644 --- a/spec/features/enter_names_spec.rb +++ b/spec/features/enter_names_spec.rb @@ -2,6 +2,6 @@ scenario "submitting a name" do sign_in_and_play #save_and_open_page - expect(page).to have_content "Joe vs. AI" + expect(page).to have_content "Joe vs. Dan" end end \ No newline at end of file diff --git a/spec/features/play_again_spec.rb b/spec/features/play_again_spec.rb index c015d44638..69b58c180f 100644 --- a/spec/features/play_again_spec.rb +++ b/spec/features/play_again_spec.rb @@ -1,10 +1,10 @@ feature 'play again' do scenario 'press play again to restart game' do sign_in_and_play - player_1_chooses_rock + player_1_rock_p2_scissors click_button 'Play again' - expect(page).to have_content "Joe vs. AI" - expect(page).not_to have_content "AI wins!" + expect(page).to have_content "Joe vs. Dan" + expect(page).not_to have_content "Dan wins!" expect(page).not_to have_content "Joe wins!" end end \ No newline at end of file diff --git a/spec/features/result_spec.rb b/spec/features/result_spec.rb index 2c11735271..0851912ec6 100644 --- a/spec/features/result_spec.rb +++ b/spec/features/result_spec.rb @@ -1,24 +1,27 @@ feature 'Result of choice' do - scenario 'Player 1 chooses Rock' do + scenario 'Player 1 chooses Rock, player 2 scissors' do sign_in_and_play - player_1_chooses_rock + player_1_rock_p2_scissors expect(page).to have_content "Joe chose rock" + expect(page).to have_content "Dan chose scissors" expect(page).not_to have_content "Joe chose paper" expect(page).not_to have_content "Joe chose scissors" end - scenario 'Player 1 chooses paper' do + scenario 'Player 1 chooses paper, player 2 rock' do sign_in_and_play - player_1_chooses_paper + player_2_rock_p1_paper expect(page).to have_content "Joe chose paper" + expect(page).to have_content "Dan chose rock" expect(page).not_to have_content "Joe chose rock" - expect(page).not_to have_content "Joe chose scissors" + expect(page).not_to have_content "Dan chose scissors" end - scenario 'Player 1 chooses scissors' do + scenario 'Player 1 chooses scissors, player 2 paper' do sign_in_and_play - player_1_chooses_scissors + player_2_paper_p1_scissors expect(page).to have_content "Joe chose scissors" + expect(page).to have_content "Dan chose paper" expect(page).not_to have_content "Joe chose paper" expect(page).not_to have_content "Joe chose rock" end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb index 309d1cfc0f..175df4aca6 100644 --- a/spec/features/web_helpers.rb +++ b/spec/features/web_helpers.rb @@ -1,20 +1,42 @@ def sign_in_and_play visit('/') fill_in :player_1_name, with: 'Joe' + fill_in :player_2_name, with: 'Dan' click_button 'Submit' end -def player_1_chooses_rock - fill_in :player_choice, with: 'rock' +def player_1_rock_p2_scissors + fill_in :player_1_choice, with: 'rock' + fill_in :player_2_choice, with: 'scissors' click_button 'Play' end -def player_1_chooses_paper - fill_in :player_choice, with: 'paper' +def player_1_paper_p2_scissors + fill_in :player_1_choice, with: 'paper' + fill_in :player_2_choice, with: 'scissors' click_button 'Play' end -def player_1_chooses_scissors - fill_in :player_choice, with: 'scissors' +def player_1_scissors_p2_scissors + fill_in :player_1_choice, with: 'scissors' + fill_in :player_2_choice, with: 'scissors' + click_button 'Play' +end + +def player_2_rock_p1_paper + fill_in :player_1_choice, with: 'paper' + fill_in :player_2_choice, with: 'rock' + click_button 'Play' +end + +def player_2_paper_p1_scissors + fill_in :player_1_choice, with: 'scissors' + fill_in :player_2_choice, with: 'paper' + click_button 'Play' +end + +def player_2_scissors_p1_rock + fill_in :player_1_choice, with: 'rock' + fill_in :player_2_choice, with: 'scissors' click_button 'Play' end \ No newline at end of file diff --git a/spec/game_spec.rb b/spec/game_spec.rb index bfdbabf0c7..4f5d7bc5d5 100644 --- a/spec/game_spec.rb +++ b/spec/game_spec.rb @@ -1,22 +1,22 @@ require 'game' describe Game do - subject(:game) { described_class.new(player_1) } + subject(:game) { described_class.new(player_1, player_2) } let(:player_1) { double :player } + let(:player_2) { double :player } - describe '#ai_choice' do - it 'returns paper' do - allow(game).to receive(:ai_choice).and_return 'paper' - expect(game.ai_choice).to eq 'paper' - game.ai_choice - end - end describe '#get_player_choice' do - it "it returns the player's choice" do + it "it returns player 1 choice" do allow(player_1).to receive(:make_choice).with('rock').and_return 'rock' - expect(game).to receive(:get_player_choice).and_return 'rock' - game.get_player_choice + expect(game).to receive(:get_player_1_choice).and_return 'rock' + game.get_player_1_choice + end + + it "it returns player 2 choice" do + allow(player_2).to receive(:make_choice).with('paper').and_return 'paper' + expect(game).to receive(:get_player_2_choice).and_return 'paper' + game.get_player_2_choice end end end \ No newline at end of file diff --git a/spec/player_spec.rb b/spec/player_spec.rb index df623d856a..013f46e4dd 100644 --- a/spec/player_spec.rb +++ b/spec/player_spec.rb @@ -2,10 +2,12 @@ describe Player do subject(:joe) { Player.new("Joe") } + subject(:dan) { Player.new("Dan") } describe '#name' do it 'returns the name' do expect(joe.name).to eq "Joe" + expect(dan.name).to eq "Dan" end end end \ No newline at end of file