-
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
Simon Tinsley #2118
base: main
Are you sure you want to change the base?
Simon Tinsley #2118
Changes from 1 commit
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,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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require './app' | ||
run RPS |
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 | ||
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;} | ||
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. Dude - this is a triumph of simple elegant styling - nice work |
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" | ||
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 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") 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. 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 |
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 |
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 |
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> |
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> | ||
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. Lovely work - elegant routing and selfishly it answers the question I was stuck on. Nice |
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> |
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 like how simple this is to follow - it's a nice way to have implemented the logic - nice work.