-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Jennie RPS Challenge solution so far #2131
base: main
Are you sure you want to change the base?
Changes from all commits
7e8acfc
676c795
4c36550
017aed5
875cccb
a89f622
94f3253
0705051
1e886cb
ca6a499
a6b9336
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,41 @@ | ||
require 'sinatra/base' | ||
require 'sinatra/reloader' | ||
require_relative 'lib/computer_choice' | ||
require_relative 'lib/game' | ||
|
||
class RockPaper < Sinatra::Base | ||
configure :development do | ||
register Sinatra::Reloader | ||
end | ||
|
||
enable :sessions | ||
|
||
get '/' do | ||
erb :index | ||
end | ||
|
||
post '/name' do | ||
session[:player_name] = params[:player_name] | ||
redirect '/play' | ||
end | ||
|
||
get '/play' do | ||
@player_name = session[:player_name] | ||
erb :play | ||
end | ||
|
||
post '/choice' do | ||
session[:player_choice] = params[:player_choice] | ||
redirect '/show_choice' | ||
end | ||
|
||
get '/show_choice' do | ||
@player_choice = session[:player_choice] | ||
@computer_choice = ComputerChoice.new.get_choice | ||
@game = Game.new(@player_choice, @computer_choice) | ||
erb :choice | ||
end | ||
|
||
run! if app_file == $0 | ||
end | ||
|
||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>Let's play Joe | ||
</h1> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<h1 style="font-family: Tahoma" > | ||
Let's play Joe | ||
</h1> | ||
|
||
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 change of font |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<h1 style="font-family: Tahoma" > | ||
Let's play | ||
</h1> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require_relative "./app" | ||
|
||
run RockPaper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class ComputerChoice | ||
def initialize | ||
@choices = ["Rock", "Paper", "Scissors"] | ||
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. put @choice object out of initialiser method |
||
end | ||
|
||
def get_choice | ||
@choices.sample | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Game | ||
|
||
attr_reader :player_choice, :computer_choice | ||
|
||
def initialize(player_choice, computer_choice) | ||
@player_choice = player_choice | ||
@computer_choice = computer_choice | ||
end | ||
|
||
def game_result | ||
if @player_choice == "Rock" && @computer_choice == "Scissors" | ||
return :win | ||
elsif @player_choice == "Rock" && @computer_choice == "Paper" | ||
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. Lots of if statements , nice ! |
||
return :lose | ||
elsif @player_choice == "Paper" && @computer_choice == "Rock" | ||
return :win | ||
elsif @player_choice == "Paper" && @computer_choice == "Scissors" | ||
return :lose | ||
elsif @player_choice == "Scissors" && @computer_choice == "Paper" | ||
return :win | ||
elsif @player_choice == "Scissors" && @computer_choice == "Rock" | ||
return :lose | ||
else | ||
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. possibly could do one line if statements , i.e |
||
return :draw | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
feature 'Show computer choice' do | ||
scenario 'computer makes a random choice of rock, paper or scissors' do | ||
allow_any_instance_of(ComputerChoice).to receive(:get_choice).and_return("Scissors") | ||
visit('/show_choice') | ||
expect(page).to have_content("Computer choice: Scissors") | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
feature 'Enter player name' do | ||
scenario 'submit name' do | ||
visit('/') | ||
fill_in :player_name, with: "Joe" | ||
click_button "Submit" | ||
|
||
#save_and_open_page | ||
|
||
expect(page).to have_content "Let's play Joe" | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
feature 'Enter player choice' do | ||
scenario 'submit rock, paper or scissors' do | ||
visit('/play') | ||
fill_in :player_choice, with: "Rock" | ||
click_button "Submit" | ||
expect(page).to have_content "Your choice: Rock" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
feature 'Show the winner' do | ||
scenario 'once both players have played, declare the winner' do | ||
allow_any_instance_of(ComputerChoice).to receive(:get_choice).and_return("Rock") | ||
visit('/') | ||
fill_in :player_name, with: "Joe" | ||
click_button "Submit" | ||
fill_in :player_choice, with: "Paper" | ||
click_button "Submit" | ||
expect(page).to have_content "The result: win" | ||
end | ||
end | ||
|
||
feature 'Show the winner' do | ||
scenario 'once both players have played, declare the winner' do | ||
allow_any_instance_of(ComputerChoice).to receive(:get_choice).and_return("Scissors") | ||
visit('/') | ||
fill_in :player_name, with: "Joe" | ||
click_button "Submit" | ||
fill_in :player_choice, with: "Paper" | ||
click_button "Submit" | ||
expect(page).to have_content "The result: lose" | ||
end | ||
end | ||
|
||
feature 'Show a draw' do | ||
scenario 'when both players have played the same, declare a draw' do | ||
allow_any_instance_of(ComputerChoice).to receive(:get_choice).and_return("Scissors") | ||
visit('/') | ||
fill_in :player_name, with: "Joe" | ||
click_button "Submit" | ||
fill_in :player_choice, with: "Scissors" | ||
click_button "Submit" | ||
expect(page).to have_content "The result: draw" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# feature 'Testing infrastructure' do | ||
# scenario 'Can run app and check page content' do | ||
# visit('/') | ||
# expect(page).to have_content 'Testing infrastructure working!' | ||
# end | ||
# end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'computer_choice' | ||
|
||
RSpec.describe ComputerChoice do | ||
context "give computer's choice for the game" do | ||
it "returns Rock as a string" do | ||
allow_any_instance_of(ComputerChoice).to receive(:get_choice).and_return("Rock") | ||
|
||
expect(ComputerChoice.new.get_choice).to eq "Rock" | ||
end | ||
|
||
it "returns Paper as a string" do | ||
allow_any_instance_of(ComputerChoice).to receive(:get_choice).and_return("Paper") | ||
|
||
expect(ComputerChoice.new.get_choice).to eq "Paper" | ||
end | ||
|
||
it "returns Scissors as a string" do | ||
allow_any_instance_of(ComputerChoice).to receive(:get_choice).and_return("Scissors") | ||
|
||
expect(ComputerChoice.new.get_choice).to eq "Scissors" | ||
end | ||
end | ||
end | ||
|
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.
App file is nice and understandable !