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

Add notes file #252

Open
wants to merge 9 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
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ end
group :development, :test do
gem 'rubocop', '1.20'
end

gem "webrick", "~> 1.7"

gem "rspec-expectations", "~> 3.11"
76 changes: 38 additions & 38 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,49 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
ansi (1.5.0)
ast (2.4.2)
capybara (3.35.3)
capybara (3.37.1)
addressable
matrix
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
diff-lcs (1.4.4)
diff-lcs (1.5.0)
docile (1.4.0)
mini_mime (1.1.1)
mini_portile2 (2.6.1)
matrix (0.4.2)
mini_mime (1.1.2)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nokogiri (1.12.3)
mini_portile2 (~> 2.6.1)
nokogiri (1.13.6-arm64-darwin)
racc (~> 1.4)
nokogiri (1.12.3-x86_64-darwin)
racc (~> 1.4)
parallel (1.20.1)
parser (3.0.2.0)
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
public_suffix (4.0.6)
racc (1.5.2)
rack (2.2.3)
rack-protection (2.1.0)
public_suffix (4.0.7)
racc (1.6.0)
rack (2.2.4)
rack-protection (2.2.0)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rainbow (3.0.0)
regexp_parser (2.1.1)
rack-test (2.0.2)
rack (>= 1.3)
rainbow (3.1.1)
regexp_parser (2.5.0)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rubocop (1.20.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
Expand All @@ -59,8 +57,8 @@ GEM
rubocop-ast (>= 1.9.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.11.0)
parser (>= 3.0.1.1)
rubocop-ast (1.18.0)
parser (>= 3.1.1.0)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
simplecov (0.21.2)
Expand All @@ -72,30 +70,32 @@ GEM
simplecov
terminal-table
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
sinatra (2.1.0)
simplecov_json_formatter (0.1.4)
sinatra (2.2.0)
mustermann (~> 1.0)
rack (~> 2.2)
rack-protection (= 2.1.0)
rack-protection (= 2.2.0)
tilt (~> 2.0)
terminal-table (3.0.1)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
tilt (2.0.10)
unicode-display_width (1.6.1)
unicode-display_width (2.2.0)
webrick (1.7.0)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
ruby
x86_64-darwin-20
arm64-darwin-21

DEPENDENCIES
capybara
rspec
rspec-expectations (~> 3.11)
rubocop (= 1.20)
simplecov
simplecov-console
sinatra
webrick (~> 1.7)

RUBY VERSION
ruby 3.0.2p107
Expand Down
8 changes: 0 additions & 8 deletions app.rb

This file was deleted.

5 changes: 4 additions & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
require_relative './app'
require_relative './lib/app'
require_relative './lib/player'
require_relative './lib/random_action'
require_relative './lib/game'
run RockPaperScissors
38 changes: 38 additions & 0 deletions lib/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'sinatra/base'

class RockPaperScissors < Sinatra::Base
enable :sessions

get '/test' do
'test page'
end

get '/' do
erb :name_form
end

post '/register-name' do
session[:player] = Player.new(params[:player_name])
redirect '/select-action'
end

get '/select-action' do
@player_name = session[:player].name
erb :select_action
end

post '/record-action' do
session[:player].choose_action(params[:action])
redirect '/result'
end

get '/result' do
@player_name = session[:player].name
@player_action = session[:player].action
@computer_action = RandomAction.new.action
@result = Game.new.result(@player_action, @computer_action)
erb :result
end

run! if app_file == $0
end
19 changes: 19 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Game
def result(player, computer)
return "draw" if draw?(player, computer)
return "win" if win?(player, computer)
return "loss"
end

private

def draw?(player, computer)
return player == computer
end

def win?(player, computer)
return (player == "rock" && computer == "scissors") ||
(player == "paper" && computer == "rock") ||
(player == "scissors" && computer == "paper")
end
end
18 changes: 18 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Player
def initialize(name)
@name = name
@action = nil
end

def name
return @name
end

def choose_action(action)
@action = action
end

def action
return @action
end
end
15 changes: 15 additions & 0 deletions lib/random_action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class RandomAction
def initialize
@actions = ["rock", "paper", "scissors"]
end

def action
return @actions[random_index]
end

private

def random_index
return [0, 1, 2].sample
end
end
4 changes: 4 additions & 0 deletions lib/views/name_form.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<form action="/register-name" method="post">
Name: <input type="text" name="player_name"/>
<input type="submit" value="Submit"/>
</form>
17 changes: 17 additions & 0 deletions lib/views/result.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div><%= @player_name %> chose <%= @player_action.capitalize %>...</div><br>

<div>...the computer chose <%= @computer_action.capitalize %></div><br>

<h2><%= @player_action.capitalize %> vs. <%= @computer_action.capitalize %></h2><br>

<% if @result == "win" %>
<div>You won!</div>
<% elsif @result == "draw" %>
<div>It's a draw!</div>
<% elsif @result == "loss" %>
<div>You lost!</div>
<% end %><br>

<form action="/select-action" method="get">
<input type="submit" value="Play again"/>
</form>
18 changes: 18 additions & 0 deletions lib/views/select_action.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h2>Greetings, <%= @player_name %>!</h2>

<div>Choose Rock, Paper or Scissors</div><br>

<form action="/record-action" method="post">
<input type="hidden" name="action" value="rock"/>
<input type="submit" value="Rock"/>
</form>

<form action="/record-action" method="post">
<input type="hidden" name="action" value="paper"/>
<input type="submit" value="Paper"/>
</form>

<form action="/record-action" method="post">
<input type="hidden" name="action" value="scissors"/>
<input type="submit" value="Scissors"/>
</form>
11 changes: 11 additions & 0 deletions notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
To do:
- reformat HTML, and add styling
- add error handling
e.g. user leaves name field blank,
or navigates to /result directly

Additional features:
- track results
e.g. "You've won 2 out of 5 games"
- track player action selection
e.g. "You choose Rock 40% of the time"
9 changes: 9 additions & 0 deletions spec/features/declare_result_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
feature 'declare result' do
context 'after player and computer actions have been selected' do
scenario 'display result' do
enter_name
click_button 'Rock'
expect(page).to have_content("You won!").or have_content("It's a draw").or have_content("You lost!")
end
end
end
10 changes: 10 additions & 0 deletions spec/features/play_again_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
feature 'play again' do
context 'after result has been declared' do
scenario 'allow user to play again' do
enter_name
click_button 'Rock'
click_button 'Play again'
expect(page).to have_content("Choose Rock, Paper or Scissors")
end
end
end
6 changes: 6 additions & 0 deletions spec/features/register_player_name_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
feature 'register player name' do
scenario 'display name entered by user' do
enter_name
expect(page).to have_content("Ollie")
end
end
30 changes: 30 additions & 0 deletions spec/features/select_player_action_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
feature 'select player action' do
scenario 'display available actions to user' do
enter_name
expect(page).to have_content("Choose Rock, Paper or Scissors")
end

context 'when user selects Rock' do
scenario 'display Rock as selected action' do
enter_name
click_button 'Rock'
expect(page).to have_content("Rock")
end
end

context 'when user selects Paper' do
scenario 'display Paper as selected action' do
enter_name
click_button 'Paper'
expect(page).to have_content("Paper")
end
end

context 'when user selects Scissors' do
scenario 'display Scissors as selected action' do
enter_name
click_button 'Scissors'
expect(page).to have_content("Scissors")
end
end
end
7 changes: 7 additions & 0 deletions spec/features/select_random_action_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
feature 'select random action' do
scenario 'display random action for computer' do
enter_name
click_button 'Rock'
expect(page).to have_content("Rock vs. Rock").or have_content("Rock vs. Scissors").or have_content("Rock vs. Paper")
end
end
2 changes: 1 addition & 1 deletion spec/features/test_page_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
feature 'test page' do
scenario 'visit test page' do
visit '/test'
expect(page).to have_content('test page')
expect(page).to have_content("test page")
end
end
5 changes: 5 additions & 0 deletions spec/features/web_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def enter_name
visit '/'
fill_in 'player_name', with: 'Ollie'
click_button 'Submit'
end
Loading