-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: main
Are you sure you want to change the base?
RPS Challenge #2132
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'sinatra/base' | ||
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require_relative "./app" | ||
|
||
run RPS |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<h1>Welcome <%= @name %> to Rock, Paper, Scissors <%= @name_1 %></h1> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>You played <%=@tool_selected%></h1> | ||
<h1>The computer played <%= @comp_selected %></h1> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 %> |
There was a problem hiding this comment.
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.