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

RPS Challenge #2132

Open
wants to merge 2 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
8 changes: 6 additions & 2 deletions Gemfile
@@ -1,16 +1,20 @@
source 'https://rubygems.org'

ruby '3.0.2'
ruby '3.0.0'

gem 'sinatra'
gem 'rerun'
gem 'sinatra-contrib'

group :test do
gem 'capybara'
gem 'rspec'
gem 'simplecov', require: false
gem 'simplecov-console', require: false
gem 'simplecov-console', require: false
end

group :development, :test do
gem 'rubocop', '1.20'
end

# frozen_string_literal: true
22 changes: 20 additions & 2 deletions Gemfile.lock
Expand Up @@ -15,8 +15,13 @@ GEM
xpath (~> 3.2)
diff-lcs (1.4.4)
docile (1.4.0)
ffi (1.15.5)
listen (3.7.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
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)
Expand All @@ -33,7 +38,12 @@ GEM
rack-test (1.1.0)
rack (>= 1.0, < 3)
rainbow (3.0.0)
rb-fsevent (0.11.1)
rb-inotify (0.10.1)
ffi (~> 1.0)
regexp_parser (2.1.1)
rerun (0.13.1)
listen (~> 3.0)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
Expand Down Expand Up @@ -76,6 +86,12 @@ 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)
Expand All @@ -88,14 +104,16 @@ PLATFORMS

DEPENDENCIES
capybara
rerun
rspec
rubocop (= 1.20)
simplecov
simplecov-console
sinatra
sinatra-contrib

RUBY VERSION
ruby 3.0.2p107
ruby 3.0.0p0

BUNDLED WITH
2.2.26
2.3.13
37 changes: 37 additions & 0 deletions app.rb
@@ -0,0 +1,37 @@
require 'sinatra/base'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding test cases would be easier for testing multiple times.

require 'sinatra/reloader'

class RPS < Sinatra::Base
enable :sessions
configure :development do
register Sinatra::Reloader
end

get '/' do
erb :index
end

post '/name' do
session[:namegiven] = params[:username]
redirect '/play'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasonable to see the separation of retrieving user input from displaying a new page.
I think it's a good thing.

end

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

post '/move' do
session[:tool_select] = params[:select]
redirect '/result'
end

get '/result' do
@tool_selected = session[:tool_select]
@name = session[:namegiven]
@comp_selected = ['rock','paper','scissors'].sample
erb :result
end

run! if app_file == $0
end
3 changes: 3 additions & 0 deletions config.ru
@@ -0,0 +1,3 @@
require_relative "./app"

run RPS
9 changes: 9 additions & 0 deletions views/index.erb
@@ -0,0 +1,9 @@
<h1>Rock, Paper and Scissors</h1>

<form action="/name" method="post">
<label for="username">
<h2> What is your name?</h2>
<input type="text" name="username">
</label>
<input type="submit" value="Submit">
</form>
10 changes: 10 additions & 0 deletions views/play.erb
@@ -0,0 +1,10 @@
<h1>Welcome <%= @name %> to Rock, Paper, Scissors <%= @name_1 %></h1>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@name_1 probably needs to be deleted


<form action="/move" method="post">

<label for="select">
<button name="select" value="rock">rock</button>
<button name="select" value="paper">paper</button>
<button name="select" value="scissors">scissors</button>
</label>
</form>
9 changes: 9 additions & 0 deletions views/result.erb
@@ -0,0 +1,9 @@
<h1>You played <%=@tool_selected%></h1>
<h1>The computer played <%= @comp_selected %></h1>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice to show both choices: user and computer. Good user experience, also good for testing.

<%if @tool_selected == @comp_selected %>
<h1>It is a draw</h1>
<%elsif @tool_selected == "paper" && @comp_selected == "rock" || @tool_selected == "rock" && @comp_selected == "scissors" || @tool_selected == "scissors" && @comp_selected == "paper" %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use a hash in winning conditions, because it's easier to edit later

<h1>You have won <%= @name %></h1>
<% else %>
<h1>Oh no! Better luck next time <%= @name %> padawan</h1>
<% end %>