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

Simon Tinsley #2118

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ end

group :development, :test do
gem 'rubocop', '1.20'
gem 'sinatra-reloader'
gem 'puma'
gem 'rspec-expectations'
end
17 changes: 16 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ 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)
nio4r (2.5.8)
nokogiri (1.12.3)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
parallel (1.20.1)
parser (3.0.2.0)
ast (~> 2.4.1)
public_suffix (4.0.6)
puma (5.6.4)
nio4r (~> 2.0)
racc (1.5.2)
rack (2.2.3)
rack-protection (2.1.0)
Expand Down Expand Up @@ -76,6 +80,14 @@ 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)
sinatra-reloader (1.0)
sinatra-contrib
terminal-table (3.0.1)
unicode-display_width (>= 1.1.1, < 3)
tilt (2.0.10)
Expand All @@ -88,14 +100,17 @@ PLATFORMS

DEPENDENCIES
capybara
puma
rspec
rspec-expectations
rubocop (= 1.20)
simplecov
simplecov-console
sinatra
sinatra-reloader

RUBY VERSION
ruby 3.0.2p107

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

class RPS < Sinatra::Base
enable :sessions

configure :development do
register Sinatra::Reloader
end

get "/" do
erb :index
end

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

get '/play' do
@player_name = $player_name
erb :play
end

get '/rock' do
@player_name = $player_name
$result = Game.new.rock
redirect '/result'
end

get '/paper' do
@player_name = $player_name
$result = Game.new.paper
redirect '/result'
end

get '/scissors' do
@player_name = $player_name
$result = Game.new.scissors
redirect '/result'
end

get '/result' do
@result = $result
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 RPS
36 changes: 36 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Game

def initialize(move = rand(3))
@comp_move = move
end

def rock
if @comp_move.zero?
"It's a draw! You picked rock and so did the computer."
elsif @comp_move == 1
"Sorry, you lose! You picked rock and the computer picked paper."
else
"Congratulations, you won! You picked rock and the computer picked scissors."
end
end

def paper
if @comp_move == 1
"It's a draw! You picked paper and so did the computer."
elsif @comp_move == 2
"Sorry, you lose! You picked paper and the computer picked scissors."
else
"Congratulations, you won! You picked paper and the computer picked rock."
end
end

def scissors
if @comp_move == 2
"It's a draw! You picked scissors and so did the computer."
elsif @comp_move == 1
"Congratulations, you won! You picked scissors and the computer picked paper."
else
"Sorry, you lose! You picked scissors and the computer picked rock."
end
end
end

Choose a reason for hiding this comment

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

I like how simple this is to follow - it's a nice way to have implemented the logic - nice work.

5 changes: 5 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {background-color: #f0f0f0; font-family: Arial, Helvetica, sans-serif;}
h1 {font-size: 48px; margin-bottom: 16px;}
.container {max-width: 1200px; text-align: center; background-color: #fff; margin: 40px auto 0 auto; padding: 20px; border-radius: 10px}
button {background-color: #072a6c; color: #fff; border-radius: 10px; padding: 10px 30px; margin: 10px; border: 0}
p {margin-bottom: 16px;}

Choose a reason for hiding this comment

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

Dude - this is a triumph of simple elegant styling - nice work

28 changes: 28 additions & 0 deletions spec/feature_tests/game_feature_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
feature "Player can play RPS" do
scenario "Player enters their name" do
enter_name
expect(page).to have_text 'Simon'
end

scenario "I select rock and I'm told I've won" do
allow_any_instance_of(Game).to receive(:name).and_return(2)
enter_name
click_button "Rock"

Choose a reason for hiding this comment

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

I think the problem with these tests is in how you are clicking the buttons. My understanding is that Cabybara will only find a click form button with this syntax. Instead try click_link("Rock")

Choose a reason for hiding this comment

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

However I put that in and theres a new problem, but at least we're kicking down the TDD path...

expect(page).to have_content("Congratulations, you won! You picked rock and the computer picked scissors.")
end

scenario "I select scissors and I'm told it's a draw" do
allow_any_instance_of(Game).to receive(:name).and_return(2)
enter_name
click_button "Paper"
expect(page).to have_content("It's a draw! You picked scissors and so did the computer.")
end

scenario "I select paper and I'm told it's a loss" do
allow_any_instance_of(Game).to receive(:name).and_return(2)
enter_name
click_button "Scissors"
expect(page).to have_text("Sorry, you lose! You picked paper and the computer picked scissors.")
end

end
5 changes: 5 additions & 0 deletions spec/feature_tests/web_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def enter_name
visit("/")
fill_in 'name', with: 'Simon'
click_on "I'm ready"
end
69 changes: 69 additions & 0 deletions spec/game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'game'

describe Game do

let(:computer_scissors) { Game.new(2) }
let(:computer_rock) { Game.new(0) }
let(:computer_paper) { Game.new(1) }

describe '#rock' do
context 'computer move is paper' do
it 'returns a losing message' do
expect(computer_paper.rock).to eq "Sorry, you lose! You picked rock and the computer picked paper."
end
end

context 'computer move is rock' do
it 'returns a drawing message' do
expect(computer_rock.rock).to eq "It's a draw! You picked rock and so did the computer."
end
end

context 'computer move is scissors' do
it 'returns a winning message' do
expect(computer_scissors.rock).to eq "Congratulations, you won! You picked rock and the computer picked scissors."
end
end
end

describe '#paper' do
context 'computer move is paper' do
it 'returns a drawing message' do
expect(computer_paper.paper).to eq "It's a draw! You picked paper and so did the computer."
end
end

context 'computer move is rock' do
it 'returns a winning message' do
expect(computer_rock.paper).to eq "Congratulations, you won! You picked paper and the computer picked rock."
end
end

context 'computer move is scissors' do
it 'returns a losing message' do
expect(computer_scissors.paper).to eq "Sorry, you lose! You picked paper and the computer picked scissors."
end
end
end

describe '#scissors' do
context 'computer move is paper' do
it 'returns a winning message' do
expect(computer_paper.scissors).to eq "Congratulations, you won! You picked scissors and the computer picked paper."
end
end

context 'computer move is rock' do
it 'returns a losing message' do
expect(computer_rock.scissors).to eq "Sorry, you lose! You picked scissors and the computer picked rock."
end
end

context 'computer move is scissors' do
it 'returns a drawing message' do
expect(computer_scissors.scissors).to eq "It's a draw! You picked scissors and so did the computer."
end
end
end

end
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
require 'capybara/rspec'
require 'simplecov'
require 'simplecov-console'
require 'rspec'
require 'rack/test'
require 'capybara'
require 'sinatra'
require File.join(File.dirname(__FILE__), '..', 'app.rb')
require File.join(File.dirname(__FILE__), 'feature_tests', 'web_helpers.rb' )
require 'rspec/expectations'

ENV['RACK_ENV'] = 'test'

Capybara.app = RPS

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
Expand Down
14 changes: 14 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<head>
<link rel="stylesheet" href="/style.css">
</head>
<div class="container">
<h1>Welcome to Rock, Paper, Scissors!</h1>

<h2>Enjoy some hard earned down time after your strenuous marketing activities.</h2>

<form action="/name" method="post">
<p><label for="name">Please enter your name:</label></p>
<input id="name" type="text" name="name">
<input type="submit" value="I'm ready!">
</form>
</div>
14 changes: 14 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<head>
<link rel="stylesheet" href="/style.css">
</head>

<body>
<div class="container"><h1> Welcome <%= @player_name %> </h1>

<h2> Please choose your move </h2>

<a href="/rock"><button id="rock">Rock</button></a>
<a href="/paper"><button id="paper">Paper</button></a>
<a href="/scissors"><button id="scissors">Scissors</button></a>
</div>
</body>

Choose a reason for hiding this comment

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

Lovely work - elegant routing and selfishly it answers the question I was stuck on. Nice

12 changes: 12 additions & 0 deletions views/result.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<head>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<div class="container">
<%= @result %>

<a href ="/play">
<button>Play again</button>
</a>
</div>
</body>