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

Leigh Rose rock paper scissors version 1 #234

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
29 changes: 27 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
require 'sinatra/base'

Choose a reason for hiding this comment

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

This seems a good, slim controller—logic is extracted to the lib files

require './lib/player'
require './lib/game'

class RockPaperScissors < Sinatra::Base
get '/test' do
'test page'
enable :sessions

get '/' do
erb :index
end

post '/names' do
$player_1 = Player.new(params[:player_1_name])

Choose a reason for hiding this comment

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

Perhaps there is another solution to avoid using global variables?

redirect '/play'
end

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

post '/choice' do
redirect '/result'
end

get '/result' do
@player_1 = $player_1
@game = Game.new(@player_1)
@result = @game.determine_winner(params[:choice])
erb :result
end
run! if app_file == $0
end
24 changes: 24 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Game

Choose a reason for hiding this comment

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

Good use of single-responsibility principle in structuring of classes—each has a clear, single purpose

attr_reader :player

def initialize(player)
end

def determine_winner(player_choice)

Choose a reason for hiding this comment

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

This seems quite a long method—perhaps there is a shorter solution?

if (computer_choice == "rock" && player_choice == "scissors") ||
(computer_choice == "scissors" && player_choice == "paper") ||
(computer_choice == "paper" && player_choice == "rock")
return "You lose"
elsif (computer_choice == player_choice)
return "It's a draw"
else
return "You WIN!"
end
end

private

def computer_choice
["rock", "paper", "scissors"].sample
end
end
8 changes: 8 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Player

Choose a reason for hiding this comment

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

Nice short, simple class!

attr_reader :name

def initialize(name)
@name = name
end

end
7 changes: 7 additions & 0 deletions spec/features/display_winner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
xfeature 'Enter names' do
scenario 'display winner' do
sign_in_and_play
click_button 'choice'
expect(page).to have_content 'win'
end
end
6 changes: 6 additions & 0 deletions spec/features/enter_names_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
feature 'Enter names' do
scenario 'submitting names' do
sign_in_and_play
expect(page).to have_content 'Leigh'
end
end
6 changes: 6 additions & 0 deletions spec/features/take_turn_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
feature 'option to pick' do
scenario 'pick option' do
sign_in_and_play
expect(page).to have_content 'Please type Rock, Paper or Scissors'
end
end
6 changes: 0 additions & 6 deletions spec/features/test_page_spec.rb

This file was deleted.

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 sign_in_and_play
visit('/')
fill_in :player_1_name, with: 'Leigh'
click_button 'Submit'
end
15 changes: 15 additions & 0 deletions spec/game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'game'

describe Game do
xit "returns a random rock, paper or scissors for computer choice" do
game_1 = Game.new("Leigh")
expect(game_1.computer_choice).to eq ("rock" || "paper" || "scissors")
end

describe Game do
xit "gives a winner" do
game_1 = Game.new("Leigh")
expect(game_1.computer_choice).to eq ("rock" || "paper" || "scissors")
end
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require 'capybara/rspec'
require 'simplecov'
require 'simplecov-console'
require 'features/web_helpers'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
# Want a nice code coverage website? Uncomment this next line!
# SimpleCov::Formatter::HTMLFormatter
SimpleCov::Formatter::HTMLFormatter
])
SimpleCov.start

Expand Down
9 changes: 9 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1> Rock Paper Scissors! </h1>
<h2>Please sign in to start:<h2>
<form action = "/names" method = "post">
<label for = "player_1_name">
Player 1 name:
<input type = "text" name = "player_1_name">
</label>
<input type= "submit" value = "Submit">
</form>
5 changes: 5 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hello <%= @player_1.name %>! <br>
Please type rock, paper or scissors <form action = "/choice" method = "post">
<input type = "text" name = "choice">
<input type= "submit" value = "Submit">
</form>
1 change: 1 addition & 0 deletions views/result.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= @result %>