-
Notifications
You must be signed in to change notification settings - Fork 250
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
Basic RPS program - JM #257
base: main
Are you sure you want to change the base?
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,8 +1,57 @@ | ||
require 'sinatra/base' | ||
|
||
class RockPaperScissors < Sinatra::Base | ||
|
||
get '/' do | ||
erb(:index) | ||
end | ||
|
||
get '/test' do | ||
'test page' | ||
end | ||
|
||
post '/game' do | ||
@name = params[:name] | ||
erb(:game) | ||
end | ||
|
||
post '/result' do | ||
@user_choice = params[:choice] | ||
@opponent_choice = [:rock, :paper, :scissors].sample | ||
@result = pick_winner(@user_choice, @opponent_choice) | ||
erb(:result) | ||
end | ||
|
||
def pick_winner(player, opponent) | ||
case player | ||
when "rock" | ||
if(opponent == :scissors) | ||
return "You win!" | ||
elsif(opponent == :rock) | ||
return "It's a tie!" | ||
else | ||
return "You lose :(" | ||
end | ||
when "paper" | ||
if(opponent == :rock) | ||
return "You win!" | ||
elsif(opponent == :paper) | ||
return "It's a tie!" | ||
else | ||
return "You lose :(" | ||
end | ||
when "scissors" | ||
if(opponent == :paper) | ||
return "You win!" | ||
elsif(opponent == :scissors) | ||
return "It's a tie!" | ||
else | ||
return "You lose :(" | ||
end | ||
else | ||
return "Invalid input." | ||
end | ||
end | ||
|
||
run! if app_file == $0 | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
feature 'Playing RPS' do | ||
scenario "When I select 'Rock', I will lose if the machine selects 'Paper'" do | ||
allow_any_instance_of(@opponent_choice).to receive(:sample).and_return(:paper) | ||
visit '/' | ||
fill_in('name', with: 'Jordan') | ||
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. Add a web_helpers page where you can create a separate method and call it here, to keep the test more tidy and avoid repeating too many lines. |
||
click_button('Submit') | ||
click_button('Rock') | ||
expect(page).to have_content "You lose :(" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
feature 'Entering username' do | ||
scenario "When I enter Jordan, the next page will include my name" do | ||
visit '/' | ||
fill_in('name', with: 'Jordan') | ||
click_button('Submit') | ||
expect(page).to have_content "Hello Jordan. Choose one of the options below to play!" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<h1>Hello <%= @name %>. Choose one of the options below to play!</h1> | ||
|
||
|
||
<form action="/result" method="post"> | ||
<button name="choice" type="submit" value="rock" >Rock</button> | ||
<button name="choice" type="submit" value="paper">Paper</button> | ||
<button name="choice" type="submit" value="scissors">Scissors</button> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<h1>Welcome to Rock, Paper, Scissors. Please enter your username!</h1> | ||
|
||
<form action="/game" method="post"> | ||
<label for="name">Name:</label> | ||
<input type="text" name="name"/> | ||
<button type="submit" name="submit">Submit</button> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<h1><%= @result %></h1> | ||
|
||
<h2>You picked <%= @user_choice %>, and your opponent picked <%= @opponent_choice %></h2> | ||
|
||
<form action="/" method="get"> | ||
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. Redirect to the game and keep the user's name so that they don't need to keep entering it again. |
||
<button type="submit">Play Again!</button> | ||
</form> |
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.
Nested if statement could be optimised better. Maybe use a hash.
Also, put this in a class file and instantiate in the app file to keep it more tidy.