Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Will M - RPS challenge #231

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
31 changes: 30 additions & 1 deletion app.rb
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions lib/paper.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions lib/rock.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions lib/scissors.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions lib/weapon.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions spec/features/enter_name_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions spec/features/weapon_selection_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/features/web_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def sign_in_and_play
visit "/"
fill_in(:player_name, with: "Ian")
click_button("Submit")
end
20 changes: 20 additions & 0 deletions spec/game_spec.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions spec/paper_spec.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions spec/rock_spec.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions spec/scissors_spec.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
23 changes: 23 additions & 0 deletions spec/weapon_spec.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Rock, Paper, Scissors</h1>
<h2>Sign in to play</h2>
<form action="/welcome" method=post>
<label for="player_name">
Your name:
<input type=text name="player_name">
</label>
<input type=submit value="Submit">
</form>
11 changes: 11 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Welcome, <%= @player_name %>!</h1>
<form action="/selection" method=post>
<p>Choose your weapon:</p>
<input type=radio id="rock" name="weapon" value="Rock">
<label for="rock">Rock</label><br>
<input type=radio id="paper" name="weapon" value="Paper">
<label for="paper">Paper</label><br>
<input type=radio id="scissors" name="weapon" value="Scissors">
<label for="scissors">Scissors</label><br><br>
<input type=submit value="Submit">
</form>
3 changes: 3 additions & 0 deletions views/result.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You chose <%= $game.weapon.name %>.
The computer chose <%= $game.opponent %>.
<%= $game.result %>