-
Notifications
You must be signed in to change notification settings - Fork 250
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -1,8 +1,33 @@ | ||
require 'sinatra/base' | ||
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]) | ||
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. 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class Game | ||
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. 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) | ||
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. 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Player | ||
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. Nice short, simple class! |
||
attr_reader :name | ||
|
||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
end |
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 |
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 |
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 |
This file was deleted.
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 |
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 |
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> |
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= @result %> |
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.
This seems a good, slim controller—logic is extracted to the lib files