-
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 8 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,40 @@ | ||
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 | ||
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,24 @@ | ||
class Game | ||
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,9 @@ | ||
feature 'Show computer choice' do | ||
scenario 'computer makes a random choice of rock, paper or scissors' do | ||
allow_any_instance_of(Array).to receive(:get_choice).and_return('Scissors') | ||
visit('/show_choice') | ||
expect(page).to have_content("Computer choice: Scissors") | ||
end | ||
end | ||
|
||
# is not working with the or options for random sample |
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,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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'game' | ||
|
||
RSpec.describe Game do | ||
context "when the player chooses rock, and the computer choice is paper" do | ||
it "shows computer as the winner" do | ||
game = Game.new("Rock", "Paper") | ||
expect(game.game_result).to eq "Lose" | ||
end | ||
end | ||
|
||
context "when the player chooses rock, and the computer choice is scissors" do | ||
it "shows player as the winner" do | ||
game = Game.new("Rock", "Scissors") | ||
expect(game.game_result).to eq "Win" | ||
end | ||
end | ||
|
||
context "when the player chooses paper, and the computer choice is scissors" do | ||
it "shows computer as the winner" do | ||
game = Game.new("Paper", "Scissors") | ||
expect(game.game_result).to eq "Lose" | ||
end | ||
end | ||
|
||
context "when the player chooses paper, and the computer choice is rock" do | ||
it "shows player as the winner" do | ||
game = Game.new("Paper", "Rock") | ||
expect(game.game_result).to eq "Win" | ||
end | ||
end | ||
|
||
context "when the player chooses scissors, and the computer choice is rock" do | ||
it "shows computer as the winner" do | ||
game = Game.new("Scissors", "Rock") | ||
expect(game.game_result).to eq "Lose" | ||
end | ||
end | ||
|
||
context "when the player chooses scissors, and the computer choice is paper" do | ||
it "shows player as the winner" do | ||
game = Game.new("Scissors", "Paper") | ||
expect(game.game_result).to eq "Win" | ||
end | ||
end | ||
|
||
context "when the player chooses rock, and the computer choice is rock" do | ||
it "is a draw" do | ||
game = Game.new("Scissors", "Scissors") | ||
expect(game.game_result).to eq "Draw" | ||
end | ||
end | ||
end | ||
|
||
|
||
|
||
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. very precise code ! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<h2 style="font-family: Tahoma" > | ||
Your choice: <%= @player_choice %><br> | ||
|
||
Computer choice: <%= @computer_choice %><br> | ||
|
||
|
||
</h2> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<h1 style="font-family: Tahoma"> | ||
Welcome to Rock, Paper, Scissors! | ||
</h1> | ||
<h2 style="font-family: Tahoma"> | ||
Ready to play? | ||
Enter your name below | ||
</h2> | ||
<form action="/name" method="post"> | ||
<input type="text" name="player_name"> | ||
<input type="submit" value="Submit"> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<h1 style="font-family: Tahoma" > | ||
Let's play <%= @player_name %> | ||
</h1> | ||
|
||
<h2 style="font-family: Tahoma"> | ||
Make your choice, rock, paper or scissors? | ||
Enter below. | ||
</h2> | ||
<form action="/choice" method="post"> | ||
<input type="text" name="player_choice"> | ||
<input type="submit" value="Submit"> | ||
</form> |
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 !