From 5b711d0ca17b3e89814feae400ad3b8af582104e Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Thu, 2 Jun 2022 12:52:41 +0100 Subject: [PATCH 01/13] able to enter name --- Gemfile | 4 ++++ Gemfile.lock | 10 ++++++++++ app.rb | 27 +++++++++++++++++++++++++++ config.ru | 5 +++++ lib/game.rb | 10 ++++++++++ spec/features/landing_page_spec.rb | 16 ++++++++++++++++ spec/spec_helper.rb | 8 ++++++++ views/game.erb | 1 + views/index.erb | 11 +++++++++++ 9 files changed, 92 insertions(+) create mode 100644 app.rb create mode 100644 config.ru create mode 100644 lib/game.rb create mode 100644 spec/features/landing_page_spec.rb create mode 100644 views/game.erb create mode 100644 views/index.erb diff --git a/Gemfile b/Gemfile index 63415039a7..833d185b8f 100644 --- a/Gemfile +++ b/Gemfile @@ -14,3 +14,7 @@ end group :development, :test do gem 'rubocop', '1.20' end + +gem "webrick", "~> 1.7" + +gem "sinatra-contrib", "~> 2.1" diff --git a/Gemfile.lock b/Gemfile.lock index 5afab6d697..791994b9b1 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,10 +77,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) @@ -93,6 +101,8 @@ DEPENDENCIES simplecov simplecov-console sinatra + sinatra-contrib (~> 2.1) + webrick (~> 1.7) RUBY VERSION ruby 3.0.2p107 diff --git a/app.rb b/app.rb new file mode 100644 index 0000000000..51368ef6f7 --- /dev/null +++ b/app.rb @@ -0,0 +1,27 @@ +# app.rb + +require 'sinatra' +require 'sinatra/base' +require 'sinatra/reloader' # if development? +require './lib/game.rb' + + +class RPS < Sinatra::Base + configure :development do + register Sinatra::Reloader + end + + get '/' do + erb(:index) + end + + post '/name' do + name = params[:player_1_name] + $game = Game.new(name) + @game = $game + erb(:game) + 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..83375a3521 --- /dev/null +++ b/config.ru @@ -0,0 +1,5 @@ +# ./config.ru + +require_relative "./app" + +run RPS \ No newline at end of file diff --git a/lib/game.rb b/lib/game.rb new file mode 100644 index 0000000000..076f6a835e --- /dev/null +++ b/lib/game.rb @@ -0,0 +1,10 @@ +# lib/game.rb + +class Game + attr_reader :player_1_name + + def initialize(player_1_name) + @player_1_name = player_1_name + end + +end \ No newline at end of file diff --git a/spec/features/landing_page_spec.rb b/spec/features/landing_page_spec.rb new file mode 100644 index 0000000000..1ffd24f07d --- /dev/null +++ b/spec/features/landing_page_spec.rb @@ -0,0 +1,16 @@ +# spec/features/landing_page_spec.rb + +feature 'Landing page' do + scenario 'Showing name of game' do + visit('/') + expect(page).to have_content 'Welcome to the Ultimate Rock Paper Scissors Game!' + end + + scenario 'Allows user to enter name' do + visit('/') + fill_in :player_1_name, with: "Michael" + click_button "Let's Go!" + expect(page).to have_content 'Michael, please choose your choice.' + end + +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 11a50a94e5..6ec80ca265 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,14 @@ +ENV['RACK_ENV'] = 'test' + +require File.join(File.dirname(__FILE__), '..', 'app.rb') + +require 'capybara' require 'capybara/rspec' require 'simplecov' require 'simplecov-console' +require 'rspec' + +Capybara.app = RPS SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, diff --git a/views/game.erb b/views/game.erb new file mode 100644 index 0000000000..f45afff235 --- /dev/null +++ b/views/game.erb @@ -0,0 +1 @@ +<%= @game.player_1_name %>, please choose your choice. \ No newline at end of file diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000000..aca4e911b3 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,11 @@ +

Welcome to the Ultimate Rock Paper Scissors Game!

+ +
+

Before we start...

+
+ + +
+
\ No newline at end of file From d701dafaebfb474927e7174e8cb0ac055d20eaa9 Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Thu, 2 Jun 2022 12:57:28 +0100 Subject: [PATCH 02/13] web_helpers.rb and spec_helper.rb --- spec/features/landing_page_spec.rb | 4 +--- spec/features/web_helpers.rb | 7 +++++++ spec/spec_helper.rb | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 spec/features/web_helpers.rb diff --git a/spec/features/landing_page_spec.rb b/spec/features/landing_page_spec.rb index 1ffd24f07d..bf35601b24 100644 --- a/spec/features/landing_page_spec.rb +++ b/spec/features/landing_page_spec.rb @@ -7,9 +7,7 @@ end scenario 'Allows user to enter name' do - visit('/') - fill_in :player_1_name, with: "Michael" - click_button "Let's Go!" + sign_in_as_michael expect(page).to have_content 'Michael, please choose your choice.' end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb new file mode 100644 index 0000000000..546316c76a --- /dev/null +++ b/spec/features/web_helpers.rb @@ -0,0 +1,7 @@ +# spec/features/web_helpers.rb + +def sign_in_as_michael + visit('/') + fill_in :player_1_name, with: "Michael" + click_button "Let's Go!" +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6ec80ca265..29ae285637 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,7 @@ require 'simplecov' require 'simplecov-console' require 'rspec' +require 'features/web_helpers.rb' Capybara.app = RPS From 24ca71c68ab7a5bdf652a0f77dbadad323c2bdb0 Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Thu, 2 Jun 2022 15:00:52 +0100 Subject: [PATCH 03/13] player.rb and game.rb --- app.rb | 15 ++++++++--- lib/game.rb | 41 ++++++++++++++++++++++++++--- lib/player.rb | 15 +++++++++++ spec/features/choose_option_spec.rb | 10 +++++++ spec/features/show_results_spec.rb | 25 ++++++++++++++++++ views/game.erb | 15 ++++++++++- 6 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 lib/player.rb create mode 100644 spec/features/choose_option_spec.rb create mode 100644 spec/features/show_results_spec.rb diff --git a/app.rb b/app.rb index 51368ef6f7..ee53afe77c 100644 --- a/app.rb +++ b/app.rb @@ -3,7 +3,8 @@ require 'sinatra' require 'sinatra/base' require 'sinatra/reloader' # if development? -require './lib/game.rb' +require './lib/game' +require './lib/player' class RPS < Sinatra::Base @@ -16,12 +17,20 @@ class RPS < Sinatra::Base end post '/name' do - name = params[:player_1_name] - $game = Game.new(name) + player_1 = Player.new(params[:player_1_name]) + player_2 = Player.new("Computer") + $game = Game.new(player_1, player_2) @game = $game erb(:game) end + get '/result' do + @game = $game + @game.player_1.choice = params[:throw] + @game.player_2.throw + erb(:match) + end + run! if app_file ==$0 end \ No newline at end of file diff --git a/lib/game.rb b/lib/game.rb index 076f6a835e..76c88a72de 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,10 +1,45 @@ # lib/game.rb +require './lib/player' + class Game - attr_reader :player_1_name + attr_reader :players, :winner + + winmap = { + :rock => :scissors, + :scissors => :paper, + :paper => :rock + } + + def initialize(player1, player2) + @players = [player1, player2] + @winner = nil + end + + def player_1 + @players[0] + end - def initialize(player_1_name) - @player_1_name = player_1_name + def player_2 + @players[1] end + def match + pick_winner(player1, player2) + @winner + end + + private + + def pick_winner(player1, player2) + if player1.choice == player2.choice + @winner = nil + elsif winmap[player1.choice] == player2.choice + @winner = player1 + else + @winner = player2 + end + end + + end \ No newline at end of file diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 0000000000..689fee91c6 --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,15 @@ +# lib/player.rb + +class Player + attr_reader :name + attr_accessor :choice + + def initialize(name) + @name = name + @choice = nil + end + + def throw + @choice = [:rock, :scissors, :paper].sample + end +end \ No newline at end of file diff --git a/spec/features/choose_option_spec.rb b/spec/features/choose_option_spec.rb new file mode 100644 index 0000000000..f132df402a --- /dev/null +++ b/spec/features/choose_option_spec.rb @@ -0,0 +1,10 @@ +# spec/features/choose_option_spec.rb + +feature 'Throwing page' do + scenario 'Showing options' do + sign_in_as_michael + expect(page).to have_content 'ROCK!' + expect(page).to have_content 'PAPER!' + expect(page).to have_content 'SCISSORS!' + end +end \ No newline at end of file diff --git a/spec/features/show_results_spec.rb b/spec/features/show_results_spec.rb new file mode 100644 index 0000000000..40ad7d6d79 --- /dev/null +++ b/spec/features/show_results_spec.rb @@ -0,0 +1,25 @@ +# spec/features/show_results_spec.rb + +feature 'Throwing page' do + scenario 'Win' do + sign_in_as_michael + + expect(page).to have_content 'ROCK!' + expect(page).to have_content 'PAPER!' + expect(page).to have_content 'SCISSORS!' + end + + scenario 'Lose' do + sign_in_as_michael + expect(page).to have_content 'ROCK!' + expect(page).to have_content 'PAPER!' + expect(page).to have_content 'SCISSORS!' + end + + scenario 'Tie' do + sign_in_as_michael + expect(page).to have_content 'ROCK!' + expect(page).to have_content 'PAPER!' + expect(page).to have_content 'SCISSORS!' + end +end \ No newline at end of file diff --git a/views/game.erb b/views/game.erb index f45afff235..ff3c0b8871 100644 --- a/views/game.erb +++ b/views/game.erb @@ -1 +1,14 @@ -<%= @game.player_1_name %>, please choose your choice. \ No newline at end of file +

<%= @game.player_1.name %>, please choose your choice.

+ +
+ +
+ +
+ +
+ +
+ +
+
\ No newline at end of file From 9319ae10b6261d2808b6ead525531e4d5fe32ec6 Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Thu, 2 Jun 2022 15:11:37 +0100 Subject: [PATCH 04/13] result page --- .gitignore | 2 ++ spec/features/show_results_spec.rb | 22 ++++++++++------------ views/game.erb | 2 ++ views/index.erb | 2 ++ views/match.erb | 4 ++++ 5 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 views/match.erb diff --git a/.gitignore b/.gitignore index d1a1edf06f..ab94d756c1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ # Local cache of Rubocop remote config .rubocop-* + +capybara-*.html diff --git a/spec/features/show_results_spec.rb b/spec/features/show_results_spec.rb index 40ad7d6d79..9b783a91e9 100644 --- a/spec/features/show_results_spec.rb +++ b/spec/features/show_results_spec.rb @@ -3,23 +3,21 @@ feature 'Throwing page' do scenario 'Win' do sign_in_as_michael - - expect(page).to have_content 'ROCK!' - expect(page).to have_content 'PAPER!' - expect(page).to have_content 'SCISSORS!' + choose('rock') + save_and_open_page + click_button("Throw!") + expect(page).to have_content 'You Win!' end - scenario 'Lose' do + xscenario 'Lose' do sign_in_as_michael - expect(page).to have_content 'ROCK!' - expect(page).to have_content 'PAPER!' - expect(page).to have_content 'SCISSORS!' + choose(rock) + expect(page).to have_content 'You Lose!' end - scenario 'Tie' do + xscenario 'Tie' do sign_in_as_michael - expect(page).to have_content 'ROCK!' - expect(page).to have_content 'PAPER!' - expect(page).to have_content 'SCISSORS!' + choose(rock) + expect(page).to have_content 'It is a tie!' end end \ No newline at end of file diff --git a/views/game.erb b/views/game.erb index ff3c0b8871..12a0a5c918 100644 --- a/views/game.erb +++ b/views/game.erb @@ -1,3 +1,5 @@ + +

<%= @game.player_1.name %>, please choose your choice.

diff --git a/views/index.erb b/views/index.erb index aca4e911b3..899e9dd96c 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,3 +1,5 @@ + +

Welcome to the Ultimate Rock Paper Scissors Game!

diff --git a/views/match.erb b/views/match.erb new file mode 100644 index 0000000000..ea64b5bb97 --- /dev/null +++ b/views/match.erb @@ -0,0 +1,4 @@ + + +

You Win!

+ From 48ed27a6bce7aca5016375fc881988aa7d1c87a5 Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Fri, 3 Jun 2022 00:53:48 +0100 Subject: [PATCH 05/13] fixed spec/show_results_spec.rb --- lib/game.rb | 48 +++++++++++++++++++----------- lib/player.rb | 2 ++ spec/features/show_results_spec.rb | 13 +++++--- spec/show_results_spec.rb | 36 ++++++++++++++++++++++ spec/throw_spec.rb | 22 ++++++++++++++ views/match.erb | 1 + 6 files changed, 100 insertions(+), 22 deletions(-) create mode 100644 spec/show_results_spec.rb create mode 100644 spec/throw_spec.rb diff --git a/lib/game.rb b/lib/game.rb index 76c88a72de..ce47c187cc 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -3,19 +3,22 @@ require './lib/player' class Game - attr_reader :players, :winner - - winmap = { - :rock => :scissors, - :scissors => :paper, - :paper => :rock - } + attr_reader :players def initialize(player1, player2) + @winmap = { + :rock => :scissors, + :scissors => :paper, + :paper => :rock + } @players = [player1, player2] @winner = nil end + def winner + @winner + end + def player_1 @players[0] end @@ -25,21 +28,30 @@ def player_2 end def match - pick_winner(player1, player2) - @winner - end - - private - - def pick_winner(player1, player2) - if player1.choice == player2.choice + if player_1.choice == player_2.choice @winner = nil - elsif winmap[player1.choice] == player2.choice - @winner = player1 + elsif @winmap[player_1.choice] == player_2.choice + @winner = player_1 + p "there #{@winner}" else - @winner = player2 + @winner = player_2 + p "here" end + @winner end + # private + + # def pick_winner + # if player_1.choice == player_2.choice + # @winner = nil + # elsif @winmap[player_1.choice] == player_2.choice + # @winner = player_1 + # p "there #{@winner}" + # else + # @winner = player_2 + # p "here" + # end + # end end \ No newline at end of file diff --git a/lib/player.rb b/lib/player.rb index 689fee91c6..8b49a3df2b 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -11,5 +11,7 @@ def initialize(name) def throw @choice = [:rock, :scissors, :paper].sample + puts @choice + return @choice end end \ No newline at end of file diff --git a/spec/features/show_results_spec.rb b/spec/features/show_results_spec.rb index 9b783a91e9..aaa5d5ac44 100644 --- a/spec/features/show_results_spec.rb +++ b/spec/features/show_results_spec.rb @@ -1,23 +1,28 @@ # spec/features/show_results_spec.rb feature 'Throwing page' do - scenario 'Win' do + xscenario 'Win' do sign_in_as_michael choose('rock') - save_and_open_page click_button("Throw!") + + expect(page).to have_content "Michael: rock vs Computer: scissors" expect(page).to have_content 'You Win!' end xscenario 'Lose' do sign_in_as_michael - choose(rock) + choose('rock') + click_button("Throw!") + expect(page).to have_content "Michael: rock vs Computer: paper" expect(page).to have_content 'You Lose!' end xscenario 'Tie' do sign_in_as_michael - choose(rock) + choose('rock') + click_button("Throw!") + expect(page).to have_content "Michael: rock vs Computer: rock" expect(page).to have_content 'It is a tie!' end end \ No newline at end of file diff --git a/spec/show_results_spec.rb b/spec/show_results_spec.rb new file mode 100644 index 0000000000..fce8616b33 --- /dev/null +++ b/spec/show_results_spec.rb @@ -0,0 +1,36 @@ +# spec/show_results_spec.rb +require 'game' + +describe Game do + subject(:object) { described_class } + let(:player1) { double :player } + let(:player2) { double :player } + + describe '#match' do + it 'declares Player 1 wins' do + + expect(player1).to receive(:choice).and_return(:rock).at_most(3).times + expect(player2).to receive(:choice).and_return(:scissors).at_most(3).times + game = Game.new(player1, player2) + game.match + expect(game.winner).to eq player1 + end + + it 'declares Player 1 loses' do + + expect(player1).to receive(:choice).at_most(3).times.and_return(:rock) + expect(player2).to receive(:choice).at_most(3).times.and_return(:paper) + game = Game.new(player1, player2) + game.match + expect(game.winner).to eq player2 + end + + it 'declares the game was tie' do + game = Game.new(player1, player2) + expect(player1).to receive(:choice).and_return(:rock).at_most(3).times + expect(player2).to receive(:choice).and_return(:rock).at_most(3).times + game.match + expect(game.winner).to eq nil + end + end +end diff --git a/spec/throw_spec.rb b/spec/throw_spec.rb new file mode 100644 index 0000000000..16f97a0d37 --- /dev/null +++ b/spec/throw_spec.rb @@ -0,0 +1,22 @@ +# spec/throw_spec.rb + +require 'player' + +describe Player do + it 'initialises with nil choice' do + player = Player.new("Felix") + expect(player.choice).to eq nil + end + + it 'shows the name' do + player = Player.new("Felix") + expect(player.name).to eq "Felix" + end + + it 'throws and can change the result' do + player = Player.new("Felix") + # allow_any_instance_of(Player).to receive(:throw).and_return(:rock) + player.throw + expect(player.choice).to eq :rock + end +end \ No newline at end of file diff --git a/views/match.erb b/views/match.erb index ea64b5bb97..d96dd12de6 100644 --- a/views/match.erb +++ b/views/match.erb @@ -1,4 +1,5 @@ +<%= @game.player_1.name %>: <%= @game.player_1.choice.to_s %> vs <%= @game.player_2.name%>: <%= @game.player_2.choice.to_s%>

You Win!

From 712e55901abb32f785d1b000eb83ee36b1f1e59b Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Fri, 3 Jun 2022 13:51:19 +0100 Subject: [PATCH 06/13] tried to fix throw_spec.rb by fixing random sequence --- app.rb | 9 +++++---- lib/game.rb | 14 ++++++++++++-- lib/player.rb | 8 +++++++- spec/features/show_results_spec.rb | 5 ++++- spec/show_results_spec.rb | 5 +++-- spec/throw_spec.rb | 7 ++++--- views/game.erb | 3 ++- views/index.erb | 2 +- views/match.erb | 8 ++++++-- 9 files changed, 44 insertions(+), 17 deletions(-) diff --git a/app.rb b/app.rb index ee53afe77c..914c0fab98 100644 --- a/app.rb +++ b/app.rb @@ -16,7 +16,7 @@ class RPS < Sinatra::Base erb(:index) end - post '/name' do + post '/game' do player_1 = Player.new(params[:player_1_name]) player_2 = Player.new("Computer") $game = Game.new(player_1, player_2) @@ -24,10 +24,11 @@ class RPS < Sinatra::Base erb(:game) end - get '/result' do + post '/result' do + $game.player_1.choice = params[:throw] + $game.player_2.choice + $game.match @game = $game - @game.player_1.choice = params[:throw] - @game.player_2.throw erb(:match) end diff --git a/lib/game.rb b/lib/game.rb index ce47c187cc..369da7de7c 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -13,6 +13,11 @@ def initialize(player1, player2) } @players = [player1, player2] @winner = nil + @declaration = "" + end + + def declaration + @declaration end def winner @@ -30,14 +35,19 @@ def player_2 def match if player_1.choice == player_2.choice @winner = nil + puts "here" + @declaration = "It is a tie!" elsif @winmap[player_1.choice] == player_2.choice @winner = player_1 - p "there #{@winner}" + puts "there" + @declaration = "You Win!" else @winner = player_2 - p "here" + puts "everywhere" + @declaration = "You Lose!" end @winner + puts @declaration end # private diff --git a/lib/player.rb b/lib/player.rb index 8b49a3df2b..98f4841d0d 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -12,6 +12,12 @@ def initialize(name) def throw @choice = [:rock, :scissors, :paper].sample puts @choice - return @choice + @choice end + + # private + + # def throw + # @choice = [:rock, :scissors, :paper].sample if @choice == nil + # end end \ No newline at end of file diff --git a/spec/features/show_results_spec.rb b/spec/features/show_results_spec.rb index aaa5d5ac44..fc1574b907 100644 --- a/spec/features/show_results_spec.rb +++ b/spec/features/show_results_spec.rb @@ -4,8 +4,8 @@ xscenario 'Win' do sign_in_as_michael choose('rock') + expect($game.player_2).to receive(:choice).and_return(:scissors).at_most(3).times # partial double? click_button("Throw!") - expect(page).to have_content "Michael: rock vs Computer: scissors" expect(page).to have_content 'You Win!' end @@ -13,6 +13,7 @@ xscenario 'Lose' do sign_in_as_michael choose('rock') + expect($game.player_2).to receive(:choice).and_return(:paper).at_most(3).times # partial double? click_button("Throw!") expect(page).to have_content "Michael: rock vs Computer: paper" expect(page).to have_content 'You Lose!' @@ -21,6 +22,8 @@ xscenario 'Tie' do sign_in_as_michael choose('rock') + allow($game.player_2).to receive(:throw).and_return(:rock) + expect($game.player_2).to receive(:choice).and_return(:rock).at_most(3).times # partial double? click_button("Throw!") expect(page).to have_content "Michael: rock vs Computer: rock" expect(page).to have_content 'It is a tie!' diff --git a/spec/show_results_spec.rb b/spec/show_results_spec.rb index fce8616b33..7abba1c29a 100644 --- a/spec/show_results_spec.rb +++ b/spec/show_results_spec.rb @@ -8,21 +8,21 @@ describe '#match' do it 'declares Player 1 wins' do - expect(player1).to receive(:choice).and_return(:rock).at_most(3).times expect(player2).to receive(:choice).and_return(:scissors).at_most(3).times game = Game.new(player1, player2) game.match expect(game.winner).to eq player1 + expect(game.declaration).to eq "You Win!" end it 'declares Player 1 loses' do - expect(player1).to receive(:choice).at_most(3).times.and_return(:rock) expect(player2).to receive(:choice).at_most(3).times.and_return(:paper) game = Game.new(player1, player2) game.match expect(game.winner).to eq player2 + expect(game.declaration).to eq "You Lose!" end it 'declares the game was tie' do @@ -31,6 +31,7 @@ expect(player2).to receive(:choice).and_return(:rock).at_most(3).times game.match expect(game.winner).to eq nil + expect(game.declaration).to eq "It is a tie!" end end end diff --git a/spec/throw_spec.rb b/spec/throw_spec.rb index 16f97a0d37..bff83c8c5e 100644 --- a/spec/throw_spec.rb +++ b/spec/throw_spec.rb @@ -14,9 +14,10 @@ end it 'throws and can change the result' do - player = Player.new("Felix") - # allow_any_instance_of(Player).to receive(:throw).and_return(:rock) + + player = Player.new("Felix") + srand(3) # to fixed the random sequence player.throw - expect(player.choice).to eq :rock + expect(player.choice).to eq :paper end end \ No newline at end of file diff --git a/views/game.erb b/views/game.erb index 12a0a5c918..9910866641 100644 --- a/views/game.erb +++ b/views/game.erb @@ -1,10 +1,11 @@

<%= @game.player_1.name %>, please choose your choice.

+

Your opponent's name: <%= @game.player_2.name %>

-
+
diff --git a/views/index.erb b/views/index.erb index 899e9dd96c..24cbe1663d 100644 --- a/views/index.erb +++ b/views/index.erb @@ -4,7 +4,7 @@

Before we start...

- + diff --git a/views/match.erb b/views/match.erb index d96dd12de6..4b7503ac12 100644 --- a/views/match.erb +++ b/views/match.erb @@ -1,5 +1,9 @@ -<%= @game.player_1.name %>: <%= @game.player_1.choice.to_s %> vs <%= @game.player_2.name%>: <%= @game.player_2.choice.to_s%> -

You Win!

+
+ <%= @game.player_1.name %>: <%= @game.player_1.choice.to_s %> vs <%= @game.player_2.name%>: <%= @game.player_2.choice.to_s%> +
+ + +

<%= @game.declaration %>

From fec99dfdfb552a64d26cd641c39e60c2fac6e57d Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Fri, 3 Jun 2022 14:27:27 +0100 Subject: [PATCH 07/13] basic single player function. fixed basic tests --- app.rb | 4 ++-- lib/game.rb | 6 +----- lib/player.rb | 1 - spec/features/show_results_spec.rb | 21 ++++++++++----------- spec/throw_spec.rb | 1 - 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/app.rb b/app.rb index 914c0fab98..d8ec628a9f 100644 --- a/app.rb +++ b/app.rb @@ -25,8 +25,8 @@ class RPS < Sinatra::Base end post '/result' do - $game.player_1.choice = params[:throw] - $game.player_2.choice + $game.player_1.choice = params[:throw].to_sym + $game.player_2.throw $game.match @game = $game erb(:match) diff --git a/lib/game.rb b/lib/game.rb index 369da7de7c..9c3542b456 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -3,7 +3,7 @@ require './lib/player' class Game - attr_reader :players + attr_reader :players, :declaration def initialize(player1, player2) @winmap = { @@ -35,19 +35,15 @@ def player_2 def match if player_1.choice == player_2.choice @winner = nil - puts "here" @declaration = "It is a tie!" elsif @winmap[player_1.choice] == player_2.choice @winner = player_1 - puts "there" @declaration = "You Win!" else @winner = player_2 - puts "everywhere" @declaration = "You Lose!" end @winner - puts @declaration end # private diff --git a/lib/player.rb b/lib/player.rb index 98f4841d0d..e66ea0b43c 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -11,7 +11,6 @@ def initialize(name) def throw @choice = [:rock, :scissors, :paper].sample - puts @choice @choice end diff --git a/spec/features/show_results_spec.rb b/spec/features/show_results_spec.rb index fc1574b907..9d7cff31ea 100644 --- a/spec/features/show_results_spec.rb +++ b/spec/features/show_results_spec.rb @@ -1,31 +1,30 @@ # spec/features/show_results_spec.rb feature 'Throwing page' do - xscenario 'Win' do + scenario 'Win' do sign_in_as_michael - choose('rock') - expect($game.player_2).to receive(:choice).and_return(:scissors).at_most(3).times # partial double? + choose('scissors') + srand(4) click_button("Throw!") - expect(page).to have_content "Michael: rock vs Computer: scissors" + expect(page).to have_content "Michael: scissors vs Computer: paper" expect(page).to have_content 'You Win!' end - xscenario 'Lose' do + scenario 'Lose' do sign_in_as_michael choose('rock') - expect($game.player_2).to receive(:choice).and_return(:paper).at_most(3).times # partial double? + srand(4) click_button("Throw!") expect(page).to have_content "Michael: rock vs Computer: paper" expect(page).to have_content 'You Lose!' end - xscenario 'Tie' do + scenario 'Tie' do sign_in_as_michael - choose('rock') - allow($game.player_2).to receive(:throw).and_return(:rock) - expect($game.player_2).to receive(:choice).and_return(:rock).at_most(3).times # partial double? + choose('paper') + srand(4) click_button("Throw!") - expect(page).to have_content "Michael: rock vs Computer: rock" + expect(page).to have_content "Michael: paper vs Computer: paper" expect(page).to have_content 'It is a tie!' end end \ No newline at end of file diff --git a/spec/throw_spec.rb b/spec/throw_spec.rb index bff83c8c5e..8f245815c2 100644 --- a/spec/throw_spec.rb +++ b/spec/throw_spec.rb @@ -14,7 +14,6 @@ end it 'throws and can change the result' do - player = Player.new("Felix") srand(3) # to fixed the random sequence player.throw From e67f3f63c2ade780113a1442b3d5338baf989478 Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Sun, 5 Jun 2022 10:47:50 +0100 Subject: [PATCH 08/13] initialising a multi player game --- app.rb | 6 +++--- lib/game.rb | 12 ++++++++++-- lib/multi.rb | 30 +++++++++++++++++++++++++++++ spec/features/landing_page_spec.rb | 6 ++++++ spec/features/multi_player_spec.rb | 31 ++++++++++++++++++++++++++++++ spec/features/web_helpers.rb | 9 +++++++++ spec/show_results_spec.rb | 6 +++--- views/game.erb | 4 ++-- views/index.erb | 11 ++++++++++- 9 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 lib/multi.rb create mode 100644 spec/features/multi_player_spec.rb diff --git a/app.rb b/app.rb index d8ec628a9f..c3acd443b8 100644 --- a/app.rb +++ b/app.rb @@ -5,6 +5,7 @@ require 'sinatra/reloader' # if development? require './lib/game' require './lib/player' +require './lib/multi' class RPS < Sinatra::Base @@ -17,9 +18,8 @@ class RPS < Sinatra::Base end post '/game' do - player_1 = Player.new(params[:player_1_name]) - player_2 = Player.new("Computer") - $game = Game.new(player_1, player_2) + multi = Multi.new(params[:player_1_name], params[:player_2_name], params[:players_num].to_i) + $game = multi.game_creation @game = $game erb(:game) end diff --git a/lib/game.rb b/lib/game.rb index 9c3542b456..83c6fdfea3 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -3,9 +3,9 @@ require './lib/player' class Game - attr_reader :players, :declaration + attr_reader :players, :declaration, :players_num, :current_player, :other_player - def initialize(player1, player2) + def initialize(player1, player2, players_num) @winmap = { :rock => :scissors, :scissors => :paper, @@ -13,13 +13,21 @@ def initialize(player1, player2) } @players = [player1, player2] @winner = nil + @current_player = @players[0] + @other_player = @players[1] @declaration = "" + @players_num = players_num end def declaration @declaration end + def switch_turn + @current_player = player_2 + @other_player = player_1 + end + def winner @winner end diff --git a/lib/multi.rb b/lib/multi.rb new file mode 100644 index 0000000000..dc8a1b31fe --- /dev/null +++ b/lib/multi.rb @@ -0,0 +1,30 @@ +# lib/multi.rb + +require './lib/player' +require './lib/game' + +class Multi + attr_reader :players_num + + def initialize(player_1_name, player_2_name, players_num) + @players_num = players_num + @player_1_name = player_1_name + @player_2_name = player_2_name + end + + def set_players + @player_1 = Player.new(@player_1_name) + if @players_num == 1 + @player_2 = Player.new("Computer") + else + @player_2 = Player.new(@player_2_name) + end + end + + def game_creation + self.set_players + game = Game.new(@player_1, @player_2, players_num) + return game + end + +end \ No newline at end of file diff --git a/spec/features/landing_page_spec.rb b/spec/features/landing_page_spec.rb index bf35601b24..46cdb6120e 100644 --- a/spec/features/landing_page_spec.rb +++ b/spec/features/landing_page_spec.rb @@ -6,6 +6,12 @@ expect(page).to have_content 'Welcome to the Ultimate Rock Paper Scissors Game!' end + scenario 'allow to choose single player or multiple player' do + visit('/') + expect(page).to have_content "Enter Player 1's name:" + expect(page).to have_content "Enter Player 2's name:" + end + scenario 'Allows user to enter name' do sign_in_as_michael expect(page).to have_content 'Michael, please choose your choice.' diff --git a/spec/features/multi_player_spec.rb b/spec/features/multi_player_spec.rb new file mode 100644 index 0000000000..3d38de3189 --- /dev/null +++ b/spec/features/multi_player_spec.rb @@ -0,0 +1,31 @@ +# spec/features/muti_player_spec.rb + +feature 'Two throwing page' do + scenario 'Showing Player 2 name for two players (when first player choosing)' do + sign_in_as_tom_and_jerry + expect(page).to have_content "Tom, please choose your choice." + expect(page).to have_content "Your opponent's name: Jerry" + end + + xscenario 'Showing Player 1 name for two players (when second player choosing)' do + sign_in_as_tom_and_jerry + choose('rock') + click_button("Throw!") + expect(page).to have_content "Jerry, please choose your choice." + expect(page).to have_content "Your opponent's name: Tom" + end + + xscenario 'Showing options for two players one by one' do + sign_in_as_tom_and_jerry + expect(page).to have_content "Tom, please choose your choice." + expect(page).to have_content 'ROCK!' + expect(page).to have_content 'PAPER!' + expect(page).to have_content 'SCISSORS!' + choose('rock') + click_button("Throw!") + expect(page).to have_content "Jerry, please choose your choice." + expect(page).to have_content 'ROCK!' + expect(page).to have_content 'PAPER!' + expect(page).to have_content 'SCISSORS!' + end +end \ No newline at end of file diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb index 546316c76a..cc82b812ba 100644 --- a/spec/features/web_helpers.rb +++ b/spec/features/web_helpers.rb @@ -3,5 +3,14 @@ def sign_in_as_michael visit('/') fill_in :player_1_name, with: "Michael" + choose('1') + click_button "Let's Go!" +end + +def sign_in_as_tom_and_jerry + visit('/') + fill_in :player_1_name, with: "Tom" + fill_in :player_2_name, with: "Jerry" + choose('2') click_button "Let's Go!" end \ No newline at end of file diff --git a/spec/show_results_spec.rb b/spec/show_results_spec.rb index 7abba1c29a..c0adf4085d 100644 --- a/spec/show_results_spec.rb +++ b/spec/show_results_spec.rb @@ -10,7 +10,7 @@ it 'declares Player 1 wins' do expect(player1).to receive(:choice).and_return(:rock).at_most(3).times expect(player2).to receive(:choice).and_return(:scissors).at_most(3).times - game = Game.new(player1, player2) + game = Game.new(player1, player2, 1) game.match expect(game.winner).to eq player1 expect(game.declaration).to eq "You Win!" @@ -19,14 +19,14 @@ it 'declares Player 1 loses' do expect(player1).to receive(:choice).at_most(3).times.and_return(:rock) expect(player2).to receive(:choice).at_most(3).times.and_return(:paper) - game = Game.new(player1, player2) + game = Game.new(player1, player2, 1) game.match expect(game.winner).to eq player2 expect(game.declaration).to eq "You Lose!" end it 'declares the game was tie' do - game = Game.new(player1, player2) + game = Game.new(player1, player2, 1) expect(player1).to receive(:choice).and_return(:rock).at_most(3).times expect(player2).to receive(:choice).and_return(:rock).at_most(3).times game.match diff --git a/views/game.erb b/views/game.erb index 9910866641..f03c01a4a9 100644 --- a/views/game.erb +++ b/views/game.erb @@ -1,7 +1,7 @@ -

<%= @game.player_1.name %>, please choose your choice.

-

Your opponent's name: <%= @game.player_2.name %>

+

<%= @game.current_player.name %>, please choose your choice.

+

Your opponent's name: <%= @game.other_player.name %>

diff --git a/views/index.erb b/views/index.erb index 24cbe1663d..2e9fa77461 100644 --- a/views/index.erb +++ b/views/index.erb @@ -5,9 +5,18 @@

Before we start...

-
\ No newline at end of file From 523ce89aa6f03328c023abf0dcce1d178959b326 Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Sun, 5 Jun 2022 13:34:38 +0100 Subject: [PATCH 09/13] multi player done, refactored not complete --- app.rb | 21 +++++++++++++++++---- lib/game.rb | 22 +++++++++++++++++----- spec/features/multi_player_spec.rb | 4 ++-- spec/features/show_results_spec.rb | 25 +++++++++++++++++++++++++ views/game.erb | 2 +- 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/app.rb b/app.rb index c3acd443b8..ff05c14ea1 100644 --- a/app.rb +++ b/app.rb @@ -24,14 +24,27 @@ class RPS < Sinatra::Base erb(:game) end - post '/result' do - $game.player_1.choice = params[:throw].to_sym - $game.player_2.throw + get '/player_2' do + if $game.players_num == 1 + $game.player_1.choice = params[:throw].to_sym + $game.player_2.throw + redirect '/result' + elsif $game.turn == 1 + $game.player_1.choice = params[:throw].to_sym + $game.switch_turn + @game = $game + erb(:game) + elsif $game.turn == 2 + $game.player_2.choice = params[:throw].to_sym + redirect '/result' + end + end + + get '/result' do $game.match @game = $game erb(:match) end - run! if app_file ==$0 end \ No newline at end of file diff --git a/lib/game.rb b/lib/game.rb index 83c6fdfea3..d38a406dd7 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -3,7 +3,7 @@ require './lib/player' class Game - attr_reader :players, :declaration, :players_num, :current_player, :other_player + attr_reader :players, :declaration, :players_num, :current_player, :other_player, :turn def initialize(player1, player2, players_num) @winmap = { @@ -13,8 +13,11 @@ def initialize(player1, player2, players_num) } @players = [player1, player2] @winner = nil + + @turn = 1 @current_player = @players[0] @other_player = @players[1] + @declaration = "" @players_num = players_num end @@ -26,6 +29,7 @@ def declaration def switch_turn @current_player = player_2 @other_player = player_1 + @turn = 2 end def winner @@ -43,15 +47,23 @@ def player_2 def match if player_1.choice == player_2.choice @winner = nil - @declaration = "It is a tie!" elsif @winmap[player_1.choice] == player_2.choice @winner = player_1 - @declaration = "You Win!" else @winner = player_2 - @declaration = "You Lose!" end - @winner + + if @winner == nil + @declaration = "It is a tie!" + elsif players_num == 2 + @declaration = "#{@winner.name} wins!" + else + if @winner == player_1 + @declaration = "You Win!" + else + @declaration = "You Lose!" + end + end end # private diff --git a/spec/features/multi_player_spec.rb b/spec/features/multi_player_spec.rb index 3d38de3189..69c3dcaa7b 100644 --- a/spec/features/multi_player_spec.rb +++ b/spec/features/multi_player_spec.rb @@ -7,7 +7,7 @@ expect(page).to have_content "Your opponent's name: Jerry" end - xscenario 'Showing Player 1 name for two players (when second player choosing)' do + scenario 'Showing Player 1 name for two players (when second player choosing)' do sign_in_as_tom_and_jerry choose('rock') click_button("Throw!") @@ -15,7 +15,7 @@ expect(page).to have_content "Your opponent's name: Tom" end - xscenario 'Showing options for two players one by one' do + scenario 'Showing options for two players one by one' do sign_in_as_tom_and_jerry expect(page).to have_content "Tom, please choose your choice." expect(page).to have_content 'ROCK!' diff --git a/spec/features/show_results_spec.rb b/spec/features/show_results_spec.rb index 9d7cff31ea..63574f4462 100644 --- a/spec/features/show_results_spec.rb +++ b/spec/features/show_results_spec.rb @@ -27,4 +27,29 @@ expect(page).to have_content "Michael: paper vs Computer: paper" expect(page).to have_content 'It is a tie!' end + + scenario 'Two players - Player 1 wins' do + sign_in_as_tom_and_jerry + choose('rock') + click_button("Throw!") + choose('scissors') + click_button("Throw!") + expect(page).to have_content "Tom wins!" + end + scenario 'Two players - Player 2 wins' do + sign_in_as_tom_and_jerry + choose('rock') + click_button("Throw!") + choose('paper') + click_button("Throw!") + expect(page).to have_content "Jerry wins!" + end + scenario 'Two players - Tie' do + sign_in_as_tom_and_jerry + choose('rock') + click_button("Throw!") + choose('rock') + click_button("Throw!") + expect(page).to have_content "It is a tie!" + end end \ No newline at end of file diff --git a/views/game.erb b/views/game.erb index f03c01a4a9..8fa9ef0f5c 100644 --- a/views/game.erb +++ b/views/game.erb @@ -5,7 +5,7 @@
-
+
From 16831d7472c17c0692f6485e52110ee3366855fb Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Sun, 5 Jun 2022 23:29:00 +0100 Subject: [PATCH 10/13] additional play again button at the end --- views/match.erb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/views/match.erb b/views/match.erb index 4b7503ac12..2c8b047534 100644 --- a/views/match.erb +++ b/views/match.erb @@ -4,6 +4,8 @@ <%= @game.player_1.name %>: <%= @game.player_1.choice.to_s %> vs <%= @game.player_2.name%>: <%= @game.player_2.choice.to_s%>
-

<%= @game.declaration %>

+ + + \ No newline at end of file From b3dcf91e0028d238bc11190a05d7c47110a25fc6 Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Mon, 6 Jun 2022 13:52:08 +0100 Subject: [PATCH 11/13] some refactoring --- app.rb | 2 +- lib/game.rb | 70 +++++++++-------------------- lib/multi.rb | 8 ++-- lib/player.rb | 8 +--- spec/features/choose_option_spec.rb | 2 +- spec/features/landing_page_spec.rb | 2 +- spec/features/multi_player_spec.rb | 2 +- spec/features/show_results_spec.rb | 2 +- spec/features/web_helpers.rb | 2 +- spec/throw_spec.rb | 2 +- views/game.erb | 2 +- 11 files changed, 33 insertions(+), 69 deletions(-) diff --git a/app.rb b/app.rb index ff05c14ea1..26b7ea2a1d 100644 --- a/app.rb +++ b/app.rb @@ -47,4 +47,4 @@ class RPS < Sinatra::Base end run! if app_file ==$0 -end \ No newline at end of file +end diff --git a/lib/game.rb b/lib/game.rb index d38a406dd7..6f7bf2e078 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -3,7 +3,7 @@ require './lib/player' class Game - attr_reader :players, :declaration, :players_num, :current_player, :other_player, :turn + attr_reader :declaration, :players_num, :current_player, :other_player, :turn, :player_1, :player_2, :winner def initialize(player1, player2, players_num) @winmap = { @@ -11,37 +11,19 @@ def initialize(player1, player2, players_num) :scissors => :paper, :paper => :rock } - @players = [player1, player2] @winner = nil - + @player_1 = player1 + @player_2 = player2 @turn = 1 - @current_player = @players[0] - @other_player = @players[1] - - @declaration = "" + @current_player = @player_1 + @other_player = @player_2 @players_num = players_num end - def declaration - @declaration - end - def switch_turn @current_player = player_2 @other_player = player_1 - @turn = 2 - end - - def winner - @winner - end - - def player_1 - @players[0] - end - - def player_2 - @players[1] + @turn += 1 end def match @@ -52,32 +34,22 @@ def match else @winner = player_2 end - - if @winner == nil - @declaration = "It is a tie!" - elsif players_num == 2 - @declaration = "#{@winner.name} wins!" - else - if @winner == player_1 - @declaration = "You Win!" - else - @declaration = "You Lose!" - end - end + set_winning_declaration end - # private + private - # def pick_winner - # if player_1.choice == player_2.choice - # @winner = nil - # elsif @winmap[player_1.choice] == player_2.choice - # @winner = player_1 - # p "there #{@winner}" - # else - # @winner = player_2 - # p "here" - # end - # end + def set_winning_declaration + @declaration = "It is a tie!" if @winner == nil + + if players_num == 2 && (@winner != nil) + @declaration = "#{@winner.name} wins!" + end -end \ No newline at end of file + if @winner == player_1 && players_num == 1 + @declaration = "You Win!" + elsif @winner == player_2 && players_num == 1 + @declaration = "You Lose!" + end + end +end diff --git a/lib/multi.rb b/lib/multi.rb index dc8a1b31fe..804dc9dc4b 100644 --- a/lib/multi.rb +++ b/lib/multi.rb @@ -22,9 +22,7 @@ def set_players end def game_creation - self.set_players - game = Game.new(@player_1, @player_2, players_num) - return game + set_players + return game = Game.new(@player_1, @player_2, players_num) end - -end \ No newline at end of file +end diff --git a/lib/player.rb b/lib/player.rb index e66ea0b43c..a9ac2e1ece 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -13,10 +13,4 @@ def throw @choice = [:rock, :scissors, :paper].sample @choice end - - # private - - # def throw - # @choice = [:rock, :scissors, :paper].sample if @choice == nil - # end -end \ No newline at end of file +end diff --git a/spec/features/choose_option_spec.rb b/spec/features/choose_option_spec.rb index f132df402a..e3f7b14ea2 100644 --- a/spec/features/choose_option_spec.rb +++ b/spec/features/choose_option_spec.rb @@ -7,4 +7,4 @@ expect(page).to have_content 'PAPER!' expect(page).to have_content 'SCISSORS!' end -end \ No newline at end of file +end diff --git a/spec/features/landing_page_spec.rb b/spec/features/landing_page_spec.rb index 46cdb6120e..488f751db7 100644 --- a/spec/features/landing_page_spec.rb +++ b/spec/features/landing_page_spec.rb @@ -17,4 +17,4 @@ expect(page).to have_content 'Michael, please choose your choice.' end -end \ No newline at end of file +end diff --git a/spec/features/multi_player_spec.rb b/spec/features/multi_player_spec.rb index 69c3dcaa7b..b301cd92fe 100644 --- a/spec/features/multi_player_spec.rb +++ b/spec/features/multi_player_spec.rb @@ -28,4 +28,4 @@ expect(page).to have_content 'PAPER!' expect(page).to have_content 'SCISSORS!' end -end \ No newline at end of file +end diff --git a/spec/features/show_results_spec.rb b/spec/features/show_results_spec.rb index 63574f4462..b9e42a738a 100644 --- a/spec/features/show_results_spec.rb +++ b/spec/features/show_results_spec.rb @@ -52,4 +52,4 @@ click_button("Throw!") expect(page).to have_content "It is a tie!" end -end \ No newline at end of file +end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb index cc82b812ba..b175a4d43d 100644 --- a/spec/features/web_helpers.rb +++ b/spec/features/web_helpers.rb @@ -13,4 +13,4 @@ def sign_in_as_tom_and_jerry fill_in :player_2_name, with: "Jerry" choose('2') click_button "Let's Go!" -end \ No newline at end of file +end diff --git a/spec/throw_spec.rb b/spec/throw_spec.rb index 8f245815c2..3f44bc765f 100644 --- a/spec/throw_spec.rb +++ b/spec/throw_spec.rb @@ -19,4 +19,4 @@ player.throw expect(player.choice).to eq :paper end -end \ No newline at end of file +end diff --git a/views/game.erb b/views/game.erb index 8fa9ef0f5c..8956a4db9b 100644 --- a/views/game.erb +++ b/views/game.erb @@ -1,4 +1,4 @@ - +

<%= @game.current_player.name %>, please choose your choice.

Your opponent's name: <%= @game.other_player.name %>

From 7e83e9e74b1dee9552abb26e9c55bb06761648f5 Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Tue, 7 Jun 2022 12:13:37 +0100 Subject: [PATCH 12/13] rename multiplayer --- app.rb | 7 ++++--- lib/{multi.rb => multiplayer.rb} | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) rename lib/{multi.rb => multiplayer.rb} (96%) diff --git a/app.rb b/app.rb index 26b7ea2a1d..258c454f23 100644 --- a/app.rb +++ b/app.rb @@ -5,7 +5,7 @@ require 'sinatra/reloader' # if development? require './lib/game' require './lib/player' -require './lib/multi' +require './lib/multiplayer' class RPS < Sinatra::Base @@ -18,12 +18,13 @@ class RPS < Sinatra::Base end post '/game' do - multi = Multi.new(params[:player_1_name], params[:player_2_name], params[:players_num].to_i) - $game = multi.game_creation + multiplayer = Multiplayer.new(params[:player_1_name], params[:player_2_name], params[:players_num].to_i) + $game = multiplayer.game_creation @game = $game erb(:game) end + # Can this part be refactored? Trying to reuse the game.erb for multiplayer, but the controller grew complex get '/player_2' do if $game.players_num == 1 $game.player_1.choice = params[:throw].to_sym diff --git a/lib/multi.rb b/lib/multiplayer.rb similarity index 96% rename from lib/multi.rb rename to lib/multiplayer.rb index 804dc9dc4b..706a51ad6f 100644 --- a/lib/multi.rb +++ b/lib/multiplayer.rb @@ -3,7 +3,7 @@ require './lib/player' require './lib/game' -class Multi +class Multiplayer attr_reader :players_num def initialize(player_1_name, player_2_name, players_num) From ddaf45137c631f87aa00f6374958be34cf0763c7 Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Tue, 7 Jun 2022 20:02:07 +0100 Subject: [PATCH 13/13] fixed rackup port --- config.ru | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config.ru b/config.ru index 83375a3521..0427d7deb6 100644 --- a/config.ru +++ b/config.ru @@ -1,5 +1,8 @@ # ./config.ru +# This line is to assign port number for rackup +#\ -p 4567 + require_relative "./app" run RPS \ No newline at end of file