From 4a8998a5ea2039c105b2ae6547c42df03211ba68 Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 11:12:42 +0000 Subject: [PATCH 01/12] sign in working --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app.rb | 18 ++++++++++++++++++ lib/player.rb | 6 ++++++ spec/features/register_name_spec.rb | 8 ++++++++ spec/features/web_helpers.rb | 5 +++++ views/index.erb | 4 ++++ views/play.erb | 1 + 8 files changed, 46 insertions(+) create mode 100644 lib/player.rb create mode 100644 spec/features/register_name_spec.rb create mode 100644 spec/features/web_helpers.rb create mode 100644 views/index.erb create mode 100644 views/play.erb diff --git a/Gemfile b/Gemfile index 63415039..b3fa8158 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,8 @@ ruby '3.0.2' gem 'sinatra' +gem 'webrick' + group :test do gem 'capybara' gem 'rspec' diff --git a/Gemfile.lock b/Gemfile.lock index b5673894..90098ca3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -82,6 +82,7 @@ GEM unicode-display_width (>= 1.1.1, < 3) tilt (2.0.10) unicode-display_width (1.6.1) + webrick (1.7.0) xpath (3.2.0) nokogiri (~> 1.8) @@ -96,6 +97,7 @@ DEPENDENCIES simplecov simplecov-console sinatra + webrick RUBY VERSION ruby 3.0.2p107 diff --git a/app.rb b/app.rb index 4abb71ec..64f36ae0 100644 --- a/app.rb +++ b/app.rb @@ -1,8 +1,26 @@ require 'sinatra/base' +require_relative 'lib/player' + class RockPaperScissors < Sinatra::Base + enable :sessions + get '/test' do 'test page' end + get '/' do + erb :index + end + + get '/names' do + $player_one = Player.new(params[:player_one]) + redirect to('/play') + end + + get '/play' do + @player_one = $player_one + erb :play + end + run! if app_file == $0 end diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 00000000..50cd50f5 --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,6 @@ +class Player + def initialize(name) + @name = name + end + attr_reader :name +end diff --git a/spec/features/register_name_spec.rb b/spec/features/register_name_spec.rb new file mode 100644 index 00000000..663ac9c7 --- /dev/null +++ b/spec/features/register_name_spec.rb @@ -0,0 +1,8 @@ +feature 'adding player name' do + scenario 'the player can submit their name before playing the game' do + visit('/') + fill_in 'player_one', with: 'Billy' + click_button 'Submit' + expect(page.html).to include("

Welcome to the game, Billy

") + end +end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb new file mode 100644 index 00000000..5aca18d1 --- /dev/null +++ b/spec/features/web_helpers.rb @@ -0,0 +1,5 @@ +def sign_in_and_play + visit('/') + fill_in 'player_one', with: 'Billy' + click_button 'Submit' +end diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..a11058db --- /dev/null +++ b/views/index.erb @@ -0,0 +1,4 @@ +
+ + +
\ No newline at end of file diff --git a/views/play.erb b/views/play.erb new file mode 100644 index 00000000..6494f3a7 --- /dev/null +++ b/views/play.erb @@ -0,0 +1 @@ +

Welcome to the game, <%= @player_one.name%>

\ No newline at end of file From 90e5553fbd70db01cb2168020511db838555ad5d Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 11:41:35 +0000 Subject: [PATCH 02/12] game testing passing --- lib/game.rb | 30 +++++++++++++++++++++++++ lib/player.rb | 5 +++++ spec/features/register_name_spec.rb | 4 ++-- spec/game_spec.rb | 34 +++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 lib/game.rb create mode 100644 spec/game_spec.rb diff --git a/lib/game.rb b/lib/game.rb new file mode 100644 index 00000000..e610581e --- /dev/null +++ b/lib/game.rb @@ -0,0 +1,30 @@ +class Game + def initialize(player_one, player_two) + @player_one = player_one + @player_two = player_two + end + + def make_choices + player_one_choice = @player_one.choose + player_two_choice = ["rock", "paper", "scissors"].sample + return [player_one_choice, player_two_choice] + end + + def run + choices = make_choices + player_one_choice = choices[0] + player_two_choice = choices[1] + if player_one_choice == player_two_choice + return "It's a draw!" + elsif player_one_choice == "rock" && player_two_choice == "scissors" + return "#{@player_one.name} wins!" + elsif player_one_choice == "paper" && player_two_choice == "rock" + return "#{@player_one.name} wins!" + elsif player_one_choice == "scissors" && player_two_choice == "paper" + return "#{player_one.name} wins!" + else + return "#{player_two.name} wins!" + end + end + +end diff --git a/lib/player.rb b/lib/player.rb index 50cd50f5..6648fe22 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -2,5 +2,10 @@ class Player def initialize(name) @name = name end + + def choose(choice) + choice + end + attr_reader :name end diff --git a/spec/features/register_name_spec.rb b/spec/features/register_name_spec.rb index 663ac9c7..0e6d8e93 100644 --- a/spec/features/register_name_spec.rb +++ b/spec/features/register_name_spec.rb @@ -1,8 +1,8 @@ feature 'adding player name' do scenario 'the player can submit their name before playing the game' do visit('/') - fill_in 'player_one', with: 'Billy' + fill_in 'player_one', with: 'Homer' click_button 'Submit' - expect(page.html).to include("

Welcome to the game, Billy

") + expect(page.html).to include("

Welcome to the game, Homer

") end end diff --git a/spec/game_spec.rb b/spec/game_spec.rb new file mode 100644 index 00000000..7e8158c7 --- /dev/null +++ b/spec/game_spec.rb @@ -0,0 +1,34 @@ +require 'game' + +describe Game do + let(:player_one) { double :player, choose: "rock", name: "Homer" } + let(:player_two) { double :player, choose: "scissors", name: "Marge" } + subject(:game) { described_class.new(player_one, player_two) } + + describe '#initialize' do + it 'creates a new game' do + expect(game).to be_an_instance_of(Game) + end + end + + describe '#make_choices' do + it 'returns an array of two choices' do + expect(game.make_choices).to be_an_instance_of(Array) + expect(game.make_choices.length).to eq(2) + end + end + + describe '#run' do + it 'returns the winner of the game' do + expect(game.run).to be_an_instance_of(String) + end + end + + describe 'player one wins when choosing rock' do + let(:player_one) { double :player, choose: "rock", name: "Homer" } + let(:player_two) { double :player, choose: "scissors", name: "Marge" } + it 'returns player one wins' do + expect(game.run).to eq("#{player_one.name} wins!") + end + end +end From 0b7945773204cdaf2abf98cb21442f3c03dc58b6 Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 12:36:07 +0000 Subject: [PATCH 03/12] game tests passing --- lib/game.rb | 31 ++++++++++----------- lib/player.rb | 4 +-- spec/features/play_game_spec.rb | 0 spec/game_spec.rb | 49 ++++++++++++++++++--------------- 4 files changed, 44 insertions(+), 40 deletions(-) create mode 100644 spec/features/play_game_spec.rb diff --git a/lib/game.rb b/lib/game.rb index e610581e..c7ecccd8 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,29 +1,28 @@ class Game - def initialize(player_one, player_two) + def initialize(player_one) @player_one = player_one - @player_two = player_two end - def make_choices - player_one_choice = @player_one.choose - player_two_choice = ["rock", "paper", "scissors"].sample - return [player_one_choice, player_two_choice] + def self.run_vs_ai(player_one) + new(player_one).run_vs_ai end - def run - choices = make_choices - player_one_choice = choices[0] - player_two_choice = choices[1] - if player_one_choice == player_two_choice + def make_ai_choice + ["rock", "paper", "scissors"].sample + end + + def run_vs_ai + ai_choice = make_ai_choice + if @player_one.choice == ai_choice return "It's a draw!" - elsif player_one_choice == "rock" && player_two_choice == "scissors" + elsif @player_one.choice == "rock" && ai_choice == "scissors" + return "#{@player_one.name} wins!" + elsif @player_one.choice == "paper" && ai_choice == "rock" return "#{@player_one.name} wins!" - elsif player_one_choice == "paper" && player_two_choice == "rock" + elsif @player_one.choice == "scissors" && ai_choice == "paper" return "#{@player_one.name} wins!" - elsif player_one_choice == "scissors" && player_two_choice == "paper" - return "#{player_one.name} wins!" else - return "#{player_two.name} wins!" + return "You lose!" end end diff --git a/lib/player.rb b/lib/player.rb index 6648fe22..df1e2e5e 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -4,8 +4,8 @@ def initialize(name) end def choose(choice) - choice + @choice = choice end - attr_reader :name + attr_reader :name, :choice end diff --git a/spec/features/play_game_spec.rb b/spec/features/play_game_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/spec/game_spec.rb b/spec/game_spec.rb index 7e8158c7..d5355289 100644 --- a/spec/game_spec.rb +++ b/spec/game_spec.rb @@ -1,34 +1,39 @@ require 'game' describe Game do - let(:player_one) { double :player, choose: "rock", name: "Homer" } - let(:player_two) { double :player, choose: "scissors", name: "Marge" } - subject(:game) { described_class.new(player_one, player_two) } - - describe '#initialize' do - it 'creates a new game' do - expect(game).to be_an_instance_of(Game) + let(:player_one) { double :player, choice: "rock", name: "Homer" } + # let(:player_two) { double :player, choice: "scissors", name: "Marge" } + + describe '#make_ai_choice returns a random choice' do + it 'returns a random choice' do + ai_choice = Game.new(player_one).make_ai_choice + expect(["rock", "paper", "scissors"]).to include(ai_choice) end end - - describe '#make_choices' do - it 'returns an array of two choices' do - expect(game.make_choices).to be_an_instance_of(Array) - expect(game.make_choices.length).to eq(2) + + describe 'player chooses rock, ai chooses scissors, ' do + it 'player wins' do + srand(1) + srand(3) + expect(Game.run_vs_ai(player_one)).to eq "#{player_one.name} wins!" end end - - describe '#run' do - it 'returns the winner of the game' do - expect(game.run).to be_an_instance_of(String) + + describe 'player chooses scissors, ai chooses scissors, ' do + let(:player_one) { double :player, choice: "scissors" } + it "it's a draw" do + srand(1) + srand(3) + expect(Game.run_vs_ai(player_one)).to eq "It's a draw!" end end - - describe 'player one wins when choosing rock' do - let(:player_one) { double :player, choose: "rock", name: "Homer" } - let(:player_two) { double :player, choose: "scissors", name: "Marge" } - it 'returns player one wins' do - expect(game.run).to eq("#{player_one.name} wins!") + + describe 'player chooses paper, ai chooses scissors, ' do + let(:player_one) { double :player, choice: "paper" } + it "it's a draw" do + srand(1) + srand(3) + expect(Game.run_vs_ai(player_one)).to eq "You lose!" end end end From 9fdc02efa3bb67ac16944829d6751bd2eefcd711 Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 12:54:45 +0000 Subject: [PATCH 04/12] gets to final page --- Gemfile | 4 ++-- Gemfile.lock | 12 ++++++++++++ app.rb | 7 +++++++ spec/features/play_game_spec.rb | 7 +++++++ spec/features/web_helpers.rb | 2 +- spec/spec_helper.rb | 1 + views/play.erb | 17 ++++++++++++++++- views/result.erb | 1 + 8 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 views/result.erb diff --git a/Gemfile b/Gemfile index b3fa8158..4020a46f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,10 @@ source 'https://rubygems.org' ruby '3.0.2' - gem 'sinatra' - gem 'webrick' +gem 'selenium-webdriver' +gem 'webdrivers', '~> 5.0', require: false group :test do gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index 90098ca3..8b0ecbbb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -13,6 +13,7 @@ GEM rack-test (>= 0.6.3) regexp_parser (>= 1.5, < 3.0) xpath (~> 3.2) + childprocess (4.1.0) diff-lcs (1.4.4) docile (1.4.0) mini_mime (1.1.1) @@ -63,6 +64,11 @@ GEM parser (>= 3.0.1.1) ruby-progressbar (1.11.0) ruby2_keywords (0.0.5) + rubyzip (2.3.2) + selenium-webdriver (4.1.0) + childprocess (>= 0.5, < 5.0) + rexml (~> 3.2, >= 3.2.5) + rubyzip (>= 1.2.2) simplecov (0.21.2) docile (~> 1.1) simplecov-html (~> 0.11) @@ -82,6 +88,10 @@ GEM unicode-display_width (>= 1.1.1, < 3) tilt (2.0.10) unicode-display_width (1.6.1) + webdrivers (5.0.0) + nokogiri (~> 1.6) + rubyzip (>= 1.3.0) + selenium-webdriver (~> 4.0) webrick (1.7.0) xpath (3.2.0) nokogiri (~> 1.8) @@ -94,9 +104,11 @@ DEPENDENCIES capybara rspec rubocop (= 1.20) + selenium-webdriver simplecov simplecov-console sinatra + webdrivers (~> 5.0) webrick RUBY VERSION diff --git a/app.rb b/app.rb index 64f36ae0..30203d25 100644 --- a/app.rb +++ b/app.rb @@ -1,5 +1,6 @@ require 'sinatra/base' require_relative 'lib/player' +require_relative 'lib/game' class RockPaperScissors < Sinatra::Base enable :sessions @@ -22,5 +23,11 @@ class RockPaperScissors < Sinatra::Base erb :play end + post '/result' do + $player_one.choose(params) + Game.run_vs_ai($player_one) + erb :result + end + run! if app_file == $0 end diff --git a/spec/features/play_game_spec.rb b/spec/features/play_game_spec.rb index e69de29b..bfc4f173 100644 --- a/spec/features/play_game_spec.rb +++ b/spec/features/play_game_spec.rb @@ -0,0 +1,7 @@ +feature 'play the game' do + scenario 'the player makes a choice against the ai and sees the result' do + sign_in_and_play + click_button 'rock' + expect(page.html).to include("

Game result

") + end +end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb index 5aca18d1..2956f17e 100644 --- a/spec/features/web_helpers.rb +++ b/spec/features/web_helpers.rb @@ -1,5 +1,5 @@ def sign_in_and_play visit('/') - fill_in 'player_one', with: 'Billy' + fill_in 'player_one', with: 'Homer' click_button 'Submit' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2177ec6a..1a164471 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ require 'capybara/rspec' require 'simplecov' require 'simplecov-console' +require 'features/web_helpers' SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, diff --git a/views/play.erb b/views/play.erb index 6494f3a7..fef0b97a 100644 --- a/views/play.erb +++ b/views/play.erb @@ -1 +1,16 @@ -

Welcome to the game, <%= @player_one.name%>

\ No newline at end of file +

Welcome to the game, <%= @player_one.name %>

+
+
+ + +
+ +
+ + +
+ +
+ + +
\ No newline at end of file diff --git a/views/result.erb b/views/result.erb new file mode 100644 index 00000000..711c1078 --- /dev/null +++ b/views/result.erb @@ -0,0 +1 @@ +

Game result

\ No newline at end of file From f52fec309d0843f07ebba37cc8d0c3b4218ae2c8 Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 14:26:14 +0000 Subject: [PATCH 05/12] all is working --- app.rb | 8 +++++--- lib/game.rb | 16 +++++++--------- lib/player.rb | 2 +- spec/game_spec.rb | 3 +-- views/play.erb | 12 ++++++------ views/result.erb | 11 ++++++++++- 6 files changed, 30 insertions(+), 22 deletions(-) diff --git a/app.rb b/app.rb index 30203d25..48ea429f 100644 --- a/app.rb +++ b/app.rb @@ -23,9 +23,11 @@ class RockPaperScissors < Sinatra::Base erb :play end - post '/result' do - $player_one.choose(params) - Game.run_vs_ai($player_one) + get '/result' do + @player_one = $player_one + @player_one.choose(params) + @game = Game.new(@player_one) + @result = @game.run_vs_ai erb :result end diff --git a/lib/game.rb b/lib/game.rb index c7ecccd8..0e9552a4 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,25 +1,23 @@ class Game + attr_reader :player_one, :ai_choice + def initialize(player_one) @player_one = player_one + @ai_choice = ["rock", "paper", "scissors"].sample end def self.run_vs_ai(player_one) new(player_one).run_vs_ai end - def make_ai_choice - ["rock", "paper", "scissors"].sample - end - def run_vs_ai - ai_choice = make_ai_choice - if @player_one.choice == ai_choice + if @player_one.choice == @ai_choice return "It's a draw!" - elsif @player_one.choice == "rock" && ai_choice == "scissors" + elsif @player_one.choice == "rock" && @ai_choice == "scissors" return "#{@player_one.name} wins!" - elsif @player_one.choice == "paper" && ai_choice == "rock" + elsif @player_one.choice == "paper" && @ai_choice == "rock" return "#{@player_one.name} wins!" - elsif @player_one.choice == "scissors" && ai_choice == "paper" + elsif @player_one.choice == "scissors" && @ai_choice == "paper" return "#{@player_one.name} wins!" else return "You lose!" diff --git a/lib/player.rb b/lib/player.rb index df1e2e5e..0c74ed47 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -4,7 +4,7 @@ def initialize(name) end def choose(choice) - @choice = choice + choice.each { |_key, value| @choice = value } end attr_reader :name, :choice diff --git a/spec/game_spec.rb b/spec/game_spec.rb index d5355289..c298be37 100644 --- a/spec/game_spec.rb +++ b/spec/game_spec.rb @@ -6,8 +6,7 @@ describe '#make_ai_choice returns a random choice' do it 'returns a random choice' do - ai_choice = Game.new(player_one).make_ai_choice - expect(["rock", "paper", "scissors"]).to include(ai_choice) + expect(["rock", "paper", "scissors"]).to include(Game.new(player_one).ai_choice) end end diff --git a/views/play.erb b/views/play.erb index fef0b97a..bfdd1a0a 100644 --- a/views/play.erb +++ b/views/play.erb @@ -1,16 +1,16 @@

Welcome to the game, <%= @player_one.name %>


-
- + +
-
- + +
-
- + +
\ No newline at end of file diff --git a/views/result.erb b/views/result.erb index 711c1078..b6bbdb1d 100644 --- a/views/result.erb +++ b/views/result.erb @@ -1 +1,10 @@ -

Game result

\ No newline at end of file +

Game result

+ +

You chose <%= @player_one.choice%>

+

The AI chose <%= @game.ai_choice%>

+ +

<%= @result %>

+ +
+ +
\ No newline at end of file From 012cb193f88027388d496597caee8d947a8b5e2d Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 14:40:01 +0000 Subject: [PATCH 06/12] added forgotten tests --- spec/features/play_game_spec.rb | 7 +++++++ spec/features/web_helpers.rb | 5 +++++ spec/game_spec.rb | 9 +++------ spec/player_spec.rb | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 spec/player_spec.rb diff --git a/spec/features/play_game_spec.rb b/spec/features/play_game_spec.rb index bfc4f173..e12b3a38 100644 --- a/spec/features/play_game_spec.rb +++ b/spec/features/play_game_spec.rb @@ -4,4 +4,11 @@ click_button 'rock' expect(page.html).to include("

Game result

") end + + scenario 'the player can click a button to play again' do + sign_in_and_play + click_button 'rock' + click_button 'Play again' + expect(page.html).to include("

Welcome to the game, Homer

") + end end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb index 2956f17e..41f0f085 100644 --- a/spec/features/web_helpers.rb +++ b/spec/features/web_helpers.rb @@ -3,3 +3,8 @@ def sign_in_and_play fill_in 'player_one', with: 'Homer' click_button 'Submit' end + +def randomise + srand(1) + srand(3) +end \ No newline at end of file diff --git a/spec/game_spec.rb b/spec/game_spec.rb index c298be37..599d53a9 100644 --- a/spec/game_spec.rb +++ b/spec/game_spec.rb @@ -12,8 +12,7 @@ describe 'player chooses rock, ai chooses scissors, ' do it 'player wins' do - srand(1) - srand(3) + randomise expect(Game.run_vs_ai(player_one)).to eq "#{player_one.name} wins!" end end @@ -21,8 +20,7 @@ describe 'player chooses scissors, ai chooses scissors, ' do let(:player_one) { double :player, choice: "scissors" } it "it's a draw" do - srand(1) - srand(3) + randomise expect(Game.run_vs_ai(player_one)).to eq "It's a draw!" end end @@ -30,8 +28,7 @@ describe 'player chooses paper, ai chooses scissors, ' do let(:player_one) { double :player, choice: "paper" } it "it's a draw" do - srand(1) - srand(3) + randomise expect(Game.run_vs_ai(player_one)).to eq "You lose!" end end diff --git a/spec/player_spec.rb b/spec/player_spec.rb new file mode 100644 index 00000000..39702de5 --- /dev/null +++ b/spec/player_spec.rb @@ -0,0 +1,20 @@ +require 'player' + +describe Player do + + describe '#name' do + it 'returns the name' do + player = Player.new('Homer') + expect(player.name).to eq 'Homer' + end + end + + describe '#choose' do + it 'returns the choice of the player' do + player = Player.new('Homer') + choice = { rock: 'rock' } + player.choose(choice) + expect(player.choice).to eq "rock" + end + end +end From d37cef3366f908e52d27e9e9c5e84c5780f623d7 Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 16:18:14 +0000 Subject: [PATCH 07/12] two players can enter names --- spec/features/play_game_spec.rb | 4 ++-- spec/features/register_name_spec.rb | 5 +++-- spec/features/web_helpers.rb | 3 ++- spec/spec_helper.rb | 2 +- views/index.erb | 3 ++- views/play.erb | 6 +++--- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/spec/features/play_game_spec.rb b/spec/features/play_game_spec.rb index e12b3a38..4022844b 100644 --- a/spec/features/play_game_spec.rb +++ b/spec/features/play_game_spec.rb @@ -1,11 +1,11 @@ feature 'play the game' do - scenario 'the player makes a choice against the ai and sees the result' do + scenario 'both players can make a choice' do sign_in_and_play click_button 'rock' expect(page.html).to include("

Game result

") end - scenario 'the player can click a button to play again' do + xscenario 'the player can click a button to play again' do sign_in_and_play click_button 'rock' click_button 'Play again' diff --git a/spec/features/register_name_spec.rb b/spec/features/register_name_spec.rb index 0e6d8e93..0eaad085 100644 --- a/spec/features/register_name_spec.rb +++ b/spec/features/register_name_spec.rb @@ -1,8 +1,9 @@ feature 'adding player name' do - scenario 'the player can submit their name before playing the game' do + scenario 'the players can submit their names before playing the game' do visit('/') fill_in 'player_one', with: 'Homer' + fill_in 'player_two', with: 'Marge' click_button 'Submit' - expect(page.html).to include("

Welcome to the game, Homer

") + expect(page.html).to include("

Homer, make your choice

") end end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb index 41f0f085..0474e3d8 100644 --- a/spec/features/web_helpers.rb +++ b/spec/features/web_helpers.rb @@ -1,10 +1,11 @@ def sign_in_and_play visit('/') fill_in 'player_one', with: 'Homer' + fill_in 'player_one', with: 'Marge' click_button 'Submit' end def randomise srand(1) srand(3) -end \ No newline at end of file +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1a164471..cef686d8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,7 +6,7 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, # Want a nice code coverage website? Uncomment this next line! - # SimpleCov::Formatter::HTMLFormatter + SimpleCov::Formatter::HTMLFormatter ]) SimpleCov.start diff --git a/views/index.erb b/views/index.erb index a11058db..9bc0d1e4 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,4 +1,5 @@
- + +
\ No newline at end of file diff --git a/views/play.erb b/views/play.erb index bfdd1a0a..e964de46 100644 --- a/views/play.erb +++ b/views/play.erb @@ -1,4 +1,4 @@ -

Welcome to the game, <%= @player_one.name %>

+

<%= @player_one.name %>, make your choice


@@ -7,10 +7,10 @@ - +
- +
\ No newline at end of file From fb35f1f938880af53f37c6d15350ae7ed972cb2f Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 16:47:05 +0000 Subject: [PATCH 08/12] Added Play vs AI button to index page --- app.rb | 5 +++++ spec/features/choose_ai_or_pvp_spec.rb | 9 +++++++++ spec/features/play_game_spec.rb | 4 ++-- spec/features/register_name_spec.rb | 6 +++--- spec/features/web_helpers.rb | 4 ++-- views/index.erb | 6 ++---- views/play.erb | 4 ++-- views/vs_ai_name.erb | 5 +++++ 8 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 spec/features/choose_ai_or_pvp_spec.rb create mode 100644 views/vs_ai_name.erb diff --git a/app.rb b/app.rb index 48ea429f..7a86a0e7 100644 --- a/app.rb +++ b/app.rb @@ -15,8 +15,13 @@ class RockPaperScissors < Sinatra::Base get '/names' do $player_one = Player.new(params[:player_one]) + $player_two = Player.new(params[:player_two]) redirect to('/play') end + + get '/vs-ai-name' do + erb :vs_ai_name + end get '/play' do @player_one = $player_one diff --git a/spec/features/choose_ai_or_pvp_spec.rb b/spec/features/choose_ai_or_pvp_spec.rb new file mode 100644 index 00000000..ac8ccf0b --- /dev/null +++ b/spec/features/choose_ai_or_pvp_spec.rb @@ -0,0 +1,9 @@ +feature 'choose to play vs AI or another player' do + scenario 'the player chooses to play vs AI' do + visit('/') + click_button 'Play vs AI' + fill_in 'player_one', with: 'Homer' + click_button 'Submit' + expect(page.html).to include("

Homer, make your choice

") + end +end diff --git a/spec/features/play_game_spec.rb b/spec/features/play_game_spec.rb index 4022844b..83dd2c68 100644 --- a/spec/features/play_game_spec.rb +++ b/spec/features/play_game_spec.rb @@ -1,12 +1,12 @@ feature 'play the game' do scenario 'both players can make a choice' do - sign_in_and_play + sign_in_and_play_vs_ai click_button 'rock' expect(page.html).to include("

Game result

") end xscenario 'the player can click a button to play again' do - sign_in_and_play + sign_in_and_play_vs_ai click_button 'rock' click_button 'Play again' expect(page.html).to include("

Welcome to the game, Homer

") diff --git a/spec/features/register_name_spec.rb b/spec/features/register_name_spec.rb index 0eaad085..da2bf38c 100644 --- a/spec/features/register_name_spec.rb +++ b/spec/features/register_name_spec.rb @@ -1,8 +1,8 @@ -feature 'adding player name' do - scenario 'the players can submit their names before playing the game' do +feature 'adding player name vs ai' do + scenario 'the player can submit their name' do visit('/') + click_button 'Play vs AI' fill_in 'player_one', with: 'Homer' - fill_in 'player_two', with: 'Marge' click_button 'Submit' expect(page.html).to include("

Homer, make your choice

") end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb index 0474e3d8..4766ad3e 100644 --- a/spec/features/web_helpers.rb +++ b/spec/features/web_helpers.rb @@ -1,7 +1,7 @@ -def sign_in_and_play +def sign_in_and_play_vs_ai visit('/') + click_button 'Play vs AI' fill_in 'player_one', with: 'Homer' - fill_in 'player_one', with: 'Marge' click_button 'Submit' end diff --git a/views/index.erb b/views/index.erb index 9bc0d1e4..6ae01b96 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,5 +1,3 @@ -
- - - + +
\ No newline at end of file diff --git a/views/play.erb b/views/play.erb index e964de46..3261cc28 100644 --- a/views/play.erb +++ b/views/play.erb @@ -7,10 +7,10 @@
- +
- +
\ No newline at end of file diff --git a/views/vs_ai_name.erb b/views/vs_ai_name.erb new file mode 100644 index 00000000..a85d667f --- /dev/null +++ b/views/vs_ai_name.erb @@ -0,0 +1,5 @@ +
+ + + +
\ No newline at end of file From 43bc8d8a727718fd09de77f08c731bf8a52cb9d5 Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 16:56:39 +0000 Subject: [PATCH 09/12] written test for starting the pvp game' --- spec/features/choose_ai_or_pvp_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/features/choose_ai_or_pvp_spec.rb b/spec/features/choose_ai_or_pvp_spec.rb index ac8ccf0b..a4f688b1 100644 --- a/spec/features/choose_ai_or_pvp_spec.rb +++ b/spec/features/choose_ai_or_pvp_spec.rb @@ -6,4 +6,14 @@ click_button 'Submit' expect(page.html).to include("

Homer, make your choice

") end + + scenario 'the player chooses to play vs another player' do + visit('/') + click_button 'Player vs Player' + fill_in 'player_one', with: 'Homer' + fill_in 'player_one', with: 'Marge' + click_button 'Submit' + click_button 'rock' + expect(page.html).to include("

Marge, make your choice

") + end end From a9ea9873c67128f2e1e38643cb2db41c507b2c0a Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 17:10:55 +0000 Subject: [PATCH 10/12] pvp track enabled --- app.rb | 12 +++++++++++- views/index.erb | 4 ++++ views/pvp_names.erb | 5 +++++ views/pvp_play.erb | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 views/pvp_names.erb create mode 100644 views/pvp_play.erb diff --git a/app.rb b/app.rb index 7a86a0e7..59194b3b 100644 --- a/app.rb +++ b/app.rb @@ -19,13 +19,23 @@ class RockPaperScissors < Sinatra::Base redirect to('/play') end + get '/pvp-names' do + erb :pvp_names + end + get '/vs-ai-name' do erb :vs_ai_name end get '/play' do @player_one = $player_one - erb :play + @player_two = $player_two + + if @player_two.name == 'computer' + erb :play + else + erb :pvp_play + end end get '/result' do diff --git a/views/index.erb b/views/index.erb index 6ae01b96..aebff95b 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,3 +1,7 @@
+
+ +
+
\ No newline at end of file diff --git a/views/pvp_names.erb b/views/pvp_names.erb new file mode 100644 index 00000000..09729537 --- /dev/null +++ b/views/pvp_names.erb @@ -0,0 +1,5 @@ +
+ + + +
\ No newline at end of file diff --git a/views/pvp_play.erb b/views/pvp_play.erb new file mode 100644 index 00000000..4b34a80b --- /dev/null +++ b/views/pvp_play.erb @@ -0,0 +1,33 @@ +

<%= @player_one.name %>, make your choice

+
+
+ + +
+ +
+ + +
+ +
+ + +
+ +

<%= @player_two.name %>, make your choice

+
+
+ + +
+ +
+ + +
+ +
+ + +
\ No newline at end of file From 93d0c0158b63f8d8f69fbd9a0d4d68abf00ebc65 Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 17:28:33 +0000 Subject: [PATCH 11/12] second player choice page appears, params not sorted yet --- app.rb | 6 +++++- views/pvp_play.erb | 23 +++-------------------- views/pvp_play_two.erb | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 views/pvp_play_two.erb diff --git a/app.rb b/app.rb index 59194b3b..eda7247a 100644 --- a/app.rb +++ b/app.rb @@ -30,13 +30,17 @@ class RockPaperScissors < Sinatra::Base get '/play' do @player_one = $player_one @player_two = $player_two - if @player_two.name == 'computer' erb :play else erb :pvp_play end end + + get '/pvp-play-2' do + @player_two = $player_two + erb :pvp_play_two + end get '/result' do @player_one = $player_one diff --git a/views/pvp_play.erb b/views/pvp_play.erb index 4b34a80b..aa0d8ee6 100644 --- a/views/pvp_play.erb +++ b/views/pvp_play.erb @@ -1,33 +1,16 @@

<%= @player_one.name %>, make your choice


-
+
-
+
-
+
- -

<%= @player_two.name %>, make your choice

-
-
- - -
- -
- - -
- -
- - -
\ No newline at end of file diff --git a/views/pvp_play_two.erb b/views/pvp_play_two.erb new file mode 100644 index 00000000..77c3c65c --- /dev/null +++ b/views/pvp_play_two.erb @@ -0,0 +1,16 @@ +

<%= @player_two.name %>, make your choice

+
+
+ + +
+ +
+ + +
+ +
+ + +
From 59bf27a140c32e9aa03833291c8e7cc6b8195bca Mon Sep 17 00:00:00 2001 From: Jon Clarke Date: Fri, 25 Mar 2022 17:37:20 +0000 Subject: [PATCH 12/12] finished for the week. need to sort params to encapsulate choices before running the game --- app.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app.rb b/app.rb index eda7247a..71cda559 100644 --- a/app.rb +++ b/app.rb @@ -38,10 +38,12 @@ class RockPaperScissors < Sinatra::Base end get '/pvp-play-2' do + $player_one.choose(params) @player_two = $player_two erb :pvp_play_two end + # need to get this working with a second player's choices now get '/result' do @player_one = $player_one @player_one.choose(params)