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

Kieran's RPS Challenge #2114

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ source 'https://rubygems.org'
ruby '3.0.2'

gem 'sinatra'
gem 'sinatra-contrib'
gem 'rspec'
gem 'capybara'
gem 'puma'
gem 'thin'
gem 'webrick'
gem 'falcon'

group :test do
gem 'capybara'
Expand Down
10 changes: 9 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GEM
docile (1.4.0)
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 Down Expand Up @@ -76,6 +77,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 @@ -93,9 +100,10 @@ DEPENDENCIES
simplecov
simplecov-console
sinatra
sinatra-contrib

RUBY VERSION
ruby 3.0.2p107

BUNDLED WITH
2.2.26
2.3.12
64 changes: 64 additions & 0 deletions app.rb
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
Copy link

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

def computer_choice
cc = (1 + rand(3))
case cc
when 1
choice = "rock"
when 2
choice = "paper"
when 3
choice = "scissors"
end
end
Copy link

Choose a reason for hiding this comment

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

it could be achieved with less code:
[:rock, :paper, :scissors].sample


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
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative "./app"

run RPS
36 changes: 36 additions & 0 deletions spec/features/feature_test_spec.rb
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
Copy link

@LGretzk LGretzk May 9, 2022

Choose a reason for hiding this comment

The 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

Copy link

Choose a reason for hiding this comment

The 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
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
ENV['RACK_ENV'] = 'test'

require File.join(File.dirname(__FILE__), '..', 'app.rb')

require 'capybara'
require 'capybara/rspec'
require 'simplecov'
require 'simplecov-console'

Capybara.app = RPS

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
# Want a nice code coverage website? Uncomment this next line!
Expand Down
5 changes: 5 additions & 0 deletions views/form.erb
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>
7 changes: 7 additions & 0 deletions views/game_choices.erb
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>