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..077b17a2 100644 --- a/app.rb +++ b/app.rb @@ -1,8 +1,37 @@ -require 'sinatra/base' +require "sinatra/base" +require_relative "./lib/game" + class RockPaperScissors < Sinatra::Base + enable :sessions + get '/test' do 'test page' end + get "/" do + erb(:index) + end + + post "/welcome" do + session[:player_name] = params[:player_name] + redirect to("/play") + end + + get "/play" do + @player_name = session[:player_name] + erb(:play) + end + + post "/selection" do + $game = Game.new(params[:weapon]) + redirect to("/result") + end + + get "/result" do + $game.play + erb(:result) + end + + run! if app_file == $0 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/enter_name_spec.rb b/spec/features/enter_name_spec.rb new file mode 100644 index 00000000..ef8e78c8 --- /dev/null +++ b/spec/features/enter_name_spec.rb @@ -0,0 +1,6 @@ +feature "Enter name" do + scenario "Player can enter and see their name" do + 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..b20cdb43 --- /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 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/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/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/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 diff --git a/views/play.erb b/views/play.erb new file mode 100644 index 00000000..d7e41373 --- /dev/null +++ b/views/play.erb @@ -0,0 +1,11 @@ +

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