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

end of week 5 challenge #239

Open
wants to merge 5 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ ruby '3.0.2'
gem 'sinatra'

group :test do
gem 'webrick'
gem 'launchy'
gem 'capybara'
gem 'rspec'
gem 'simplecov', require: false
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ GEM
xpath (~> 3.2)
diff-lcs (1.4.4)
docile (1.4.0)
launchy (2.5.0)
addressable (~> 2.7)
mini_mime (1.1.1)
mini_portile2 (2.6.1)
mustermann (1.1.1)
Expand Down Expand Up @@ -82,6 +84,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)

Expand All @@ -91,11 +94,13 @@ PLATFORMS

DEPENDENCIES
capybara
launchy
rspec
rubocop (= 1.20)
simplecov
simplecov-console
sinatra
webrick

RUBY VERSION
ruby 3.0.2p107
Expand Down
30 changes: 28 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
require 'sinatra/base'

class RockPaperScissors < Sinatra::Base
get '/test' do
'test page'
enable :sessions

get '/' do
erb :index
end

post '/name' do
session[:user_name] = params[:user_name]
redirect '/play'
end

get '/play' do
@user_name = session[:user_name]
erb :play
end

post '/choice' do
session[:user_choice] = params[:user_choice]
session[:computer_choice] = ["ROCK", "PAPER", "SCISSORS"].sample
redirect :game
end

get '/game' do
@user_name = session[:user_name]
@user_choice = session[:user_choice]
@computer_choice = session[:computer_choice]
erb :game
end

run! if app_file == $0
Expand Down
1 change: 1 addition & 0 deletions capybara-202203251122256029479837.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>You chose rock!</h1>
1 change: 1 addition & 0 deletions capybara-20220325143932360182499.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bella selected: on
11 changes: 11 additions & 0 deletions capybara-20220325144958428819652.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1> Bella vs Computer! </h1>
<h3><STRONG> Bella, please select one: </STRONG></h3>
<form action="/choice" method="post">
<INPUT type="radio" name="user_choice" id="rock">ROCK
<INPUT type="radio" name="user_choice" id="paper">PAPER
<INPUT type="radio" name="user_choice" id="scissors">SCISSORS
<br>
<br>
<input type="submit" value="Submit" name = "submit_button">
</form>

1 change: 1 addition & 0 deletions capybara-202203251450163139914023.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bella selected: on
6 changes: 0 additions & 6 deletions spec/features/test_page_spec.rb

This file was deleted.

20 changes: 20 additions & 0 deletions spec/features/user_choice_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
feature 'User choice' do
scenario 'user picks rock' do
sign_in_and_play
choose("rock")
click_button 'Submit'
expect(page).to have_content "Bella selected: ROCK"
end
scenario 'user picks paper' do
sign_in_and_play
choose("paper")
click_button 'Submit'
expect(page).to have_content "Bella selected: PAPER"
end
scenario 'user picks scissors' do
sign_in_and_play
choose("scissors")
click_button 'Submit'
expect(page).to have_content "Bella selected: SCISSORS"
end
end
6 changes: 6 additions & 0 deletions spec/features/user_registration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
feature 'User registration' do
scenario 'user enters name' do
sign_in_and_play
expect(page).to have_content 'Bella'
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 :user_name, with: 'Bella'
click_button 'Submit'
end
3 changes: 2 additions & 1 deletion 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 'capybara/rspec'
require 'features/web_helpers'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
Expand Down
20 changes: 20 additions & 0 deletions views/game.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

<h3><%= @user_name %> selected: <%= @user_choice %> </h3>
<h3> Computer selected: <%= @computer_choice %> </h3>
<h3>
<% if @user_choice == "ROCK" && @computer_choice == "SCISSORS" %>
<%= @user_name %> wins!
<% elsif @user_choice == "SCISSORS" && @computer_choice == "PAPER" %>
<%= @user_name %> wins!
<% elsif @user_choice == "PAPER" && @computer_choice == "ROCK" %>
<%= @user_name %> wins!
<% elsif @computer_choice == "ROCK" && @user_choice == "SCISSORS" %>
Computer wins!
<% elsif @computer_choice == "SCISSORS" && @userr_choice == "PAPER" %>
Computer wins!
<% elsif @computer_choice == "PAPER" && @user_choice == "ROCK" %>
Computer wins!
<% else %>
It's a draw!
<% end %>
</h3>
10 changes: 10 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>
Hello, user!
</h1>
<h2> This is rock, paper, scissors game. Please enter your name...
</h2>
<form action="/name" method="post">
<input type="text" placeholder="Player name" name="user_name">
<button type="submit">Submit</button>
</form>
<h2>...and let's play! :)</h2>
11 changes: 11 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1> <%= @user_name %> vs Computer! </h1>
<h3><%= @user_name %>, please select one: </h3>
<form action="/choice" method="post">
<INPUT type="radio" name="user_choice" value="ROCK" id="rock">ROCK
<INPUT type="radio" name="user_choice" value="PAPER" id="paper">PAPER
<INPUT type="radio" name="user_choice" value="SCISSORS" id="scissors">SCISSORS
<br>
<br>
<input type="submit" value="Submit" name = "submit_button">
</form>