-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Kieran's RPS Challenge #2114
base: main
Are you sure you want to change the base?
Kieran's RPS Challenge #2114
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'sinatra/base' | ||
require 'sinatra/reloader' | ||
|
||
class RPS < Sinatra::Base | ||
configure :development do | ||
register Sinatra::Reloader | ||
end | ||
|
||
get '/' do | ||
erb :form | ||
end | ||
|
||
post '/name' do | ||
@name1 = params[:name1] | ||
erb :game_choices | ||
end | ||
|
||
post '/game' do | ||
@player_move = params[:choice] | ||
game = Game.new | ||
@computer_move = game.computer_choice | ||
@result = game.result(@player_move, @computer_move) | ||
"#{@result}" | ||
end | ||
|
||
run! if app_file == $0 | ||
end | ||
|
||
|
||
class Game | ||
def computer_choice | ||
cc = (1 + rand(3)) | ||
case cc | ||
when 1 | ||
choice = "rock" | ||
when 2 | ||
choice = "paper" | ||
when 3 | ||
choice = "scissors" | ||
end | ||
end | ||
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. it could be achieved with less code: |
||
|
||
def result(user_input, choice) | ||
if user_input == choice | ||
return "I chose #{choice} too. It's a tie. Let's try again." | ||
else | ||
if user_input == "rock" && choice == "paper" | ||
return "I chose paper. Paper wraps rock. I win." | ||
elsif user_input == "rock" && choice == "scissors" | ||
return "I chose scissors. Rock destroys scissors. You win." | ||
elsif user_input == "paper" && choice == "rock" | ||
return "I chose rock. Paper wraps rock. You win." | ||
elsif user_input == "paper" && choice == "scissors" | ||
return "I chose scissors. Scissors cut paper. I win." | ||
elsif user_input == "scissors" && choice == "rock" | ||
return "I chose rock. Rock destroys scissors. I win." | ||
elsif user_input == "scissors" && choice == "paper" | ||
return "I chose paper. Scissors cut paper. You win." | ||
else | ||
return "I didn't understand you. Please try again." | ||
end #end nested if statement | ||
end #end if statement | ||
end | ||
end #end game class |
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,36 @@ | ||
# coding: utf-8 | ||
|
||
require "capybara/rspec" | ||
require_relative "../../app" | ||
|
||
Capybara.app = RPS | ||
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 don't think the above is needed in the feature test spec |
||
|
||
feature 'Fills in player name' do | ||
scenario 'players can fill in their names, submit a form, and see those names on-screen' do | ||
visit("/") | ||
name1 = "Gawain" | ||
fill_in :name1 , with: name1 | ||
click_button "Submit" | ||
expect(page).to have_content "Player: #{name1}!" | ||
end | ||
end | ||
|
||
feature 'Game' do | ||
scenario 'can choose one of three choices' do | ||
visit("/name") | ||
expect(page).to have_button('submit') | ||
end | ||
|
||
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. the above is not testing for the choice |
||
scenario 'can choose a winner based on players choice' do | ||
visit("/game") | ||
@player_move = 'rock' | ||
@computer_move = 'paper' | ||
expect(page).to have_content "I chose paper. Paper wraps rock. I win." | ||
end | ||
end | ||
|
||
|
||
# be presented the choices (rock, paper and scissors) | ||
# the marketeer can choose one option | ||
# the game will choose a random option | ||
# a winner will be declared |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<form action="/name" method="post"> | ||
<label for="name">Player:</label> | ||
<input type="text" name="name1" placeholder="type name here"> | ||
<button type="submit" value=submit> Submit to enter name! </button> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<h1> Player: <%= @name1 %>! </h1> | ||
<form action="/game" method="post"> | ||
click to `rock, paper or scissors` to play a game of rock paper scissors. Good luck! | ||
<label for="choice"> Type in rock, paper or scissors </label> | ||
<input type="text" name="choice" placeholder="enter move here"> | ||
<button type="submit" value=submit> Submit move </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.
I think the Game class should be separate from the app.rb file