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 Game - WIP #2113

Open
wants to merge 1 commit 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: 7 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ group :test do
gem 'simplecov-console', require: false
end

group :development, :test do
group :development, :test :deployment do
gem 'rubocop', '1.20'
# gem 'webdrivers', '~> 5.0', require: false
gem 'rspec-sinatra'
gem 'rack'
gem 'launchy'
gem 'webrick'
gem 'sinatra-contrib'
end
25 changes: 24 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ GEM
xpath (~> 3.2)
diff-lcs (1.4.4)
docile (1.4.0)
extlib (0.9.16)
highline (2.0.3)
launchy (2.5.0)
addressable (~> 2.7)
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 @@ -47,6 +52,8 @@ GEM
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-sinatra (0.1.2)
templater (>= 1.0.0)
rspec-support (3.10.2)
rubocop (1.20.0)
parallel (~> 1.10)
Expand Down Expand Up @@ -76,10 +83,21 @@ 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)
templater (1.0.0)
diff-lcs (>= 1.1.2)
extlib (>= 0.9.5)
highline (>= 1.4.0)
terminal-table (3.0.1)
unicode-display_width (>= 1.1.1, < 3)
tilt (2.0.10)
unicode-display_width (2.0.0)
webrick (1.7.0)
xpath (3.2.0)
nokogiri (~> 1.8)

Expand All @@ -88,14 +106,19 @@ PLATFORMS

DEPENDENCIES
capybara
launchy
rack
rspec
rspec-sinatra
rubocop (= 1.20)
simplecov
simplecov-console
sinatra
sinatra-contrib
webrick

RUBY VERSION
ruby 3.0.2p107

BUNDLED WITH
2.2.26
2.3.12
41 changes: 41 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'sinatra/base'
require 'sinatra/reloader'
require './lib/rps'

class RockPaperScissors < Sinatra::Base
enable :sessions

configure :development do
register Sinatra::Reloader
end

get '/' do
erb :index
end

post '/name' do
session[:player_name] = params[:player_name]
redirect '/play'
end

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

post '/select' do
@player_name = session[:player_name]
session[:choice] = params[:choice]
redirect '/result'
end

get '/result' do
@player_name = session[:player_name]
@player_choice = session[:choice]
@game = Game.new
@game.play(@player_choice)
erb :result
end

run! if app_file == $0
end
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require './app'
run RockPaperScissors
27 changes: 27 additions & 0 deletions lib/rps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Game
attr_reader :result, :computer_choice

def initalize
@result = nil
end

def play(player_choice)
player = player_choice
computer = "rock"
# just for testing set to "rock"

if (player == 'scissors' && computer == 'paper') ||
(player == 'paper' && computer == 'rock') ||
(player == 'rock' && computer == 'scissors')
@result = "You win!"
elsif player == computer
@result = "Draw!"
else
@result = "The computer wins..."
end
end

# def random_choice
# @computer_choice = [ "rock", "paper", "scissors"].sample
# end
end
17 changes: 17 additions & 0 deletions spec/features/enter_choice_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

feature 'Enter choice' do

scenario 'submit choice' do
sign_in_and_play
expect(page).to have_button("rock")
expect(page).to have_button("paper")
expect(page).to have_button("scissors")
end

scenario 'submit choice' do
sign_in_and_play
choose("rock")
click_button 'Submit'
expect(page).to have_content 'Max chose: rock'
end
end
6 changes: 6 additions & 0 deletions spec/features/enter_name_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
feature 'Enter name' do
scenario 'submit name' do
sign_in_and_play
expect(page).to have_content 'Max, please choose:'
end
end
6 changes: 6 additions & 0 deletions spec/features/web_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

def sign_in_and_play
visit('/')
fill_in :player_name, with: 'Max'
click_button 'Submit'
end
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
ENV['RACK_ENV'] = 'test'

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

require 'capybara'
require 'capybara/rspec'
require 'rspec'

require 'simplecov'
require 'simplecov-console'

require 'features/web_helpers'

# tell Capybara about our app class
Capybara.app = RockPaperScissors

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
# Want a nice code coverage website? Uncomment this next line!
Expand All @@ -18,3 +30,4 @@
puts "\e[33mTry it now! Just run: rubocop\e[0m"
end
end

11 changes: 11 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Rock, Paper, Scissors</h1>

<h2>Please enter your name</h2>

<form action="/name" method="post">
<%# <label for="player"> %>
<%# player name %>
<input type="text" name="player_name">
<input type="submit" value="Submit">
<%# </label> %>
</form>
17 changes: 17 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>Rock, Paper, Scissors</h1>

<h2><%= @player_name %>, please choose:</h2>

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

<label for="rock"> Rock </label>
<input type="radio" name="choice" value="rock">

<label for="paper"> Paper </label>
<input type="radio" name="choice" value="paper">

<label for="scissors"> Scissors </label>
<input type="radio" name="choice" value="scissors">

<input type="submit" value="Submit">
</form>
5 changes: 5 additions & 0 deletions views/result.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Rock, Paper, Scissors</h1>

<h3><%= @player_name %> chose: <%= @player_choice %></h3>
<h3>The computer chose: <%= @game.computer_choice %></h3>
<h2><%= @game.result %><h2>