From ef60e71ef663d170b2cfd78ecd958d958a181e3a Mon Sep 17 00:00:00 2001 From: William Marshall Date: Fri, 25 Mar 2022 11:10:45 +0000 Subject: [PATCH 1/3] Add enter name feature --- Gemfile | 4 ++++ app.rb | 9 +++++++++ spec/features/enter_name_spec.rb | 8 ++++++++ views/index.erb | 9 +++++++++ 4 files changed, 30 insertions(+) create mode 100644 spec/features/enter_name_spec.rb create mode 100644 views/index.erb diff --git a/Gemfile b/Gemfile index 63415039..a79efc62 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,10 @@ source 'https://rubygems.org' ruby '3.0.2' gem 'sinatra' +gem 'puma' +gem 'thin' +gem 'falcon' +gem 'webrick' group :test do gem 'capybara' diff --git a/app.rb b/app.rb index 4abb71ec..06ad2f83 100644 --- a/app.rb +++ b/app.rb @@ -4,5 +4,14 @@ class RockPaperScissors < Sinatra::Base 'test page' end + get "/" do + erb(:index) + end + + post "/welcome" do + @player_name = params[:player_name] + "Welcome, #{@player_name}!" + end + run! if app_file == $0 end diff --git a/spec/features/enter_name_spec.rb b/spec/features/enter_name_spec.rb new file mode 100644 index 00000000..993411fa --- /dev/null +++ b/spec/features/enter_name_spec.rb @@ -0,0 +1,8 @@ +feature "Enter name" do + scenario "Player can enter and see their name" do + visit "/" + fill_in(:player_name, with: "Ian") + click_button("Submit") + expect(page).to have_content("Welcome, Ian!") + end +end diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..93d69915 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,9 @@ +

Rock, Paper, Scissors

+

Sign in to play

+
+ + +
\ No newline at end of file From b36f4f079772b4946143b2b50d57f376893ffb5b Mon Sep 17 00:00:00 2001 From: William Marshall Date: Fri, 25 Mar 2022 13:34:23 +0000 Subject: [PATCH 2/3] Add weapon selection feature --- app.rb | 20 +++++++++++++++++--- spec/features/enter_name_spec.rb | 4 +--- spec/features/weapon_selection_spec.rb | 8 ++++++++ spec/features/web_helpers.rb | 5 +++++ spec/spec_helper.rb | 1 + views/play.erb | 11 +++++++++++ 6 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 spec/features/weapon_selection_spec.rb create mode 100644 spec/features/web_helpers.rb create mode 100644 views/play.erb diff --git a/app.rb b/app.rb index 06ad2f83..17093a05 100644 --- a/app.rb +++ b/app.rb @@ -1,5 +1,8 @@ -require 'sinatra/base' +require "sinatra/base" + class RockPaperScissors < Sinatra::Base + enable :sessions + get '/test' do 'test page' end @@ -9,9 +12,20 @@ class RockPaperScissors < Sinatra::Base end post "/welcome" do - @player_name = params[:player_name] - "Welcome, #{@player_name}!" + session[:player_name] = params[:player_name] + redirect to("/play") + end + + get "/play" do + @player_name = session[:player_name] + erb(:play) + end + + post "/result" do + weapon = params[:weapon] + "You chose #{weapon}" end + run! if app_file == $0 end diff --git a/spec/features/enter_name_spec.rb b/spec/features/enter_name_spec.rb index 993411fa..ef8e78c8 100644 --- a/spec/features/enter_name_spec.rb +++ b/spec/features/enter_name_spec.rb @@ -1,8 +1,6 @@ feature "Enter name" do scenario "Player can enter and see their name" do - visit "/" - fill_in(:player_name, with: "Ian") - click_button("Submit") + sign_in_and_play expect(page).to have_content("Welcome, Ian!") end end diff --git a/spec/features/weapon_selection_spec.rb b/spec/features/weapon_selection_spec.rb new file mode 100644 index 00000000..ccc03d06 --- /dev/null +++ b/spec/features/weapon_selection_spec.rb @@ -0,0 +1,8 @@ +feature "Weapon selection" do + scenario "Player can select a weapon" do + sign_in_and_play + choose("Paper") + click_on("Submit") + expect(page).to have_content("You chose Paper") + end +end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb new file mode 100644 index 00000000..d2515804 --- /dev/null +++ b/spec/features/web_helpers.rb @@ -0,0 +1,5 @@ +def sign_in_and_play + visit "/" + fill_in(:player_name, with: "Ian") + click_button("Submit") +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2177ec6a..7cf1241f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ require 'capybara/rspec' require 'simplecov' require 'simplecov-console' +require_relative "./features/web_helpers" SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, diff --git a/views/play.erb b/views/play.erb new file mode 100644 index 00000000..882ce47f --- /dev/null +++ b/views/play.erb @@ -0,0 +1,11 @@ +

Welcome, <%= @player_name %>!

+
+

Choose your weapon:

+ +
+ +
+ +

+ +
From f4ea2d5548facb0d88fab61aaf59d248d43c5a79 Mon Sep 17 00:00:00 2001 From: William Marshall Date: Fri, 25 Mar 2022 18:43:22 +0000 Subject: [PATCH 3/3] Add result route showing game outcome --- app.rb | 12 +++++++--- lib/game.rb | 45 ++++++++++++++++++++++++++++++++++++ lib/paper.rb | 22 ++++++++++++++++++ lib/rock.rb | 22 ++++++++++++++++++ lib/scissors.rb | 22 ++++++++++++++++++ lib/weapon.rb | 14 +++++++++++ spec/features/web_helpers.rb | 2 +- spec/game_spec.rb | 20 ++++++++++++++++ spec/paper_spec.rb | 23 ++++++++++++++++++ spec/rock_spec.rb | 22 ++++++++++++++++++ spec/scissors_spec.rb | 23 ++++++++++++++++++ spec/weapon_spec.rb | 23 ++++++++++++++++++ views/play.erb | 2 +- views/result.erb | 3 +++ 14 files changed, 250 insertions(+), 5 deletions(-) create mode 100644 lib/game.rb create mode 100644 lib/paper.rb create mode 100644 lib/rock.rb create mode 100644 lib/scissors.rb create mode 100644 lib/weapon.rb create mode 100644 spec/game_spec.rb create mode 100644 spec/paper_spec.rb create mode 100644 spec/rock_spec.rb create mode 100644 spec/scissors_spec.rb create mode 100644 spec/weapon_spec.rb create mode 100644 views/result.erb diff --git a/app.rb b/app.rb index 17093a05..077b17a2 100644 --- a/app.rb +++ b/app.rb @@ -1,4 +1,5 @@ require "sinatra/base" +require_relative "./lib/game" class RockPaperScissors < Sinatra::Base enable :sessions @@ -21,9 +22,14 @@ class RockPaperScissors < Sinatra::Base erb(:play) end - post "/result" do - weapon = params[:weapon] - "You chose #{weapon}" + post "/selection" do + $game = Game.new(params[:weapon]) + redirect to("/result") + end + + get "/result" do + $game.play + erb(:result) end diff --git a/lib/game.rb b/lib/game.rb new file mode 100644 index 00000000..dd6052de --- /dev/null +++ b/lib/game.rb @@ -0,0 +1,45 @@ +require_relative "./rock" +require_relative "./paper" +require_relative "./scissors" + +class Game + attr_reader :weapon, :opponent + + def initialize(player_selection) + @player_selection = player_selection + @weapon = select_weapon + @opponent = random_weapon + end + + def play + @weapon.challenge(@opponent) + end + + def result + if @weapon.winner == true + "You won!" + elsif @weapon.loser == true + "You lost!" + elsif @weapon.draw == true + "You drew!" + end + end + + private + + def select_weapon + case @player_selection + when "Rock" + Rock.new + when "Paper" + Paper.new + when "Scissors" + Scissors.new + end + end + + def random_weapon + ["Rock", "Paper", "Scissors"].sample + end + +end diff --git a/lib/paper.rb b/lib/paper.rb new file mode 100644 index 00000000..834261f9 --- /dev/null +++ b/lib/paper.rb @@ -0,0 +1,22 @@ +require_relative "./weapon" + +class Paper < Weapon + + def name + "Paper" + end + +private + + def generate_result(opponent) + case opponent + when "Rock" + @winner = true + when "Paper" + @draw = true + when "Scissors" + @loser = true + end + end + +end diff --git a/lib/rock.rb b/lib/rock.rb new file mode 100644 index 00000000..432cf8c9 --- /dev/null +++ b/lib/rock.rb @@ -0,0 +1,22 @@ +require_relative "./weapon" + +class Rock < Weapon + + def name + "Rock" + end + +private + + def generate_result(opponent) + case opponent + when "Rock" + @draw = true + when "Paper" + @loser = true + when "Scissors" + @winner = true + end + end + +end diff --git a/lib/scissors.rb b/lib/scissors.rb new file mode 100644 index 00000000..3313ab90 --- /dev/null +++ b/lib/scissors.rb @@ -0,0 +1,22 @@ +require_relative "./weapon" + +class Scissors < Weapon + + def name + "Scissors" + end + +private + + def generate_result(opponent) + case opponent + when "Rock" + @loser = true + when "Paper" + @winner = true + when "Scissors" + @draw = true + end + end + +end diff --git a/lib/weapon.rb b/lib/weapon.rb new file mode 100644 index 00000000..f9330cc0 --- /dev/null +++ b/lib/weapon.rb @@ -0,0 +1,14 @@ +class Weapon + attr_reader :winner, :loser, :draw + + def initialize + @winner = false + @loser = false + @draw = false + end + + def challenge(opponent) + generate_result(opponent) + end + +end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb index d2515804..b20cdb43 100644 --- a/spec/features/web_helpers.rb +++ b/spec/features/web_helpers.rb @@ -2,4 +2,4 @@ def sign_in_and_play visit "/" fill_in(:player_name, with: "Ian") click_button("Submit") -end \ No newline at end of file +end diff --git a/spec/game_spec.rb b/spec/game_spec.rb new file mode 100644 index 00000000..d09ef964 --- /dev/null +++ b/spec/game_spec.rb @@ -0,0 +1,20 @@ +require "game" + +describe Game do + subject(:game) { Game.new("Rock") } + let(:weapon) { double(:weapon, winner: false, loser: false, draw: false) } + + describe "#weapon" do + it "returns the user's chosen weapon" do + expect(game.weapon).to be_an_instance_of(Rock) + end + end + + describe "#opponent" do + it "returns the opponent's weapon" do + weapons = ["Rock", "Paper", "Scissors"] + expect(weapons).to include(game.opponent) + end + end + +end diff --git a/spec/paper_spec.rb b/spec/paper_spec.rb new file mode 100644 index 00000000..adff179d --- /dev/null +++ b/spec/paper_spec.rb @@ -0,0 +1,23 @@ +require "paper" + +describe Paper do + subject(:paper) { Paper.new } + + describe "#name" do + it "returns the weapon name" do + expect(paper.name).to eq("Paper") + end + end + + describe "#challenge" do + it "accepts an opponent and generates a result" do + paper.challenge("Rock") + expect(paper.winner).to eq(true) + paper.challenge("Paper") + expect(paper.draw).to eq(true) + paper.challenge("Scissors") + expect(paper.loser).to eq(true) + end + end + +end \ No newline at end of file diff --git a/spec/rock_spec.rb b/spec/rock_spec.rb new file mode 100644 index 00000000..cce768a0 --- /dev/null +++ b/spec/rock_spec.rb @@ -0,0 +1,22 @@ +require "rock" + +describe Rock do + subject(:rock) { Rock.new } + + describe "#name" do + it "returns the weapon name" do + expect(rock.name).to eq("Rock") + end + end + + describe "#challenge" do + it "accepts an opponent and generates a result" do + rock.challenge("Rock") + expect(rock.draw).to eq(true) + rock.challenge("Paper") + expect(rock.loser).to eq(true) + rock.challenge("Scissors") + expect(rock.winner).to eq(true) + end + end +end \ No newline at end of file diff --git a/spec/scissors_spec.rb b/spec/scissors_spec.rb new file mode 100644 index 00000000..4c6d933c --- /dev/null +++ b/spec/scissors_spec.rb @@ -0,0 +1,23 @@ +require "scissors" + +describe Scissors do + subject(:scissors) { Scissors.new } + + describe "#name" do + it "returns the weapon name" do + expect(scissors.name).to eq("Scissors") + end + end + + describe "#challenge" do + it "accepts an opponent and generates a result" do + scissors.challenge("Rock") + expect(scissors.loser).to eq(true) + scissors.challenge("Paper") + expect(scissors.winner).to eq(true) + scissors.challenge("Scissors") + expect(scissors.draw).to eq(true) + end + end + +end \ No newline at end of file diff --git a/spec/weapon_spec.rb b/spec/weapon_spec.rb new file mode 100644 index 00000000..ba1c166c --- /dev/null +++ b/spec/weapon_spec.rb @@ -0,0 +1,23 @@ +require "weapon" + +describe Weapon do + subject(:weapon) { Weapon.new } + + describe "#winner" do + it "is initialized as false" do + expect(weapon.winner).to eq(false) + end + end + + describe "#loser" do + it "is initialized as false" do + expect(weapon.loser).to eq(false) + end + end + + describe "#draw" do + it "is initialized as false" do + expect(weapon.draw).to eq(false) + end + end +end diff --git a/views/play.erb b/views/play.erb index 882ce47f..d7e41373 100644 --- a/views/play.erb +++ b/views/play.erb @@ -1,5 +1,5 @@

Welcome, <%= @player_name %>!

-
+

Choose your weapon:


diff --git a/views/result.erb b/views/result.erb new file mode 100644 index 00000000..86eca50c --- /dev/null +++ b/views/result.erb @@ -0,0 +1,3 @@ +You chose <%= $game.weapon.name %>. +The computer chose <%= $game.opponent %>. +<%= $game.result %> \ No newline at end of file