From 414bbb3f7332d2738c3afa556c13c94ef8887e97 Mon Sep 17 00:00:00 2001 From: erb16 Date: Fri, 8 Jul 2022 13:36:11 +0100 Subject: [PATCH 1/4] player able to submit name --- app.rb | 29 +++++++++++++++++++++++++++-- spec/feature_spec.rb | 6 ++++++ views/index.erb | 9 +++++++++ views/results.erb | 10 ++++++++++ views/submitted-name.erb | 4 ++++ 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 spec/feature_spec.rb create mode 100644 views/index.erb create mode 100644 views/results.erb create mode 100644 views/submitted-name.erb diff --git a/app.rb b/app.rb index 4abb71ec..d9bd9b02 100644 --- a/app.rb +++ b/app.rb @@ -1,8 +1,33 @@ require 'sinatra/base' +# require "sinatra/reloader" if development? class RockPaperScissors < Sinatra::Base - get '/test' do - 'test page' + + enable :sessions + + # get '/test' do + # 'test page' + # end + + get '/' do + # session["player_name"] = params["player_name"] + erb(:index) + # redirect '/submit-name' + end + + post '/submit-name' do + + session["player_name"] = params["player_name"] + @player_name = session["player_name"] + # erb (:submitted-name) + erb(:results) + end + + get '/results' do + @player_name = session["player_name"] + + erb(:results) end run! if app_file == $0 + end diff --git a/spec/feature_spec.rb b/spec/feature_spec.rb new file mode 100644 index 00000000..7f684eeb --- /dev/null +++ b/spec/feature_spec.rb @@ -0,0 +1,6 @@ +describe '#name' do + it 'returns the name' do + expect(Bob.name).to eq 'Bob' + end +end + diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..4ffb0c29 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,9 @@ + +

ROCK PAPER SCISSORS!

+

Who wishes to challenge me?

+
+ + +
\ No newline at end of file diff --git a/views/results.erb b/views/results.erb new file mode 100644 index 00000000..8967fa07 --- /dev/null +++ b/views/results.erb @@ -0,0 +1,10 @@ + +

ROCK PAPER SCISSORS!

+

<%=session["player_name"]%>, let's play! Choose your weapon:

+
+ + + +
+ diff --git a/views/submitted-name.erb b/views/submitted-name.erb new file mode 100644 index 00000000..744c392b --- /dev/null +++ b/views/submitted-name.erb @@ -0,0 +1,4 @@ +
+ + +
\ No newline at end of file From d1cdfd890b540069e050005d6fd7dd8d9cf70726 Mon Sep 17 00:00:00 2001 From: erb16 Date: Fri, 8 Jul 2022 14:54:07 +0100 Subject: [PATCH 2/4] pc makes random selection --- app.rb | 29 +++++++++++++++++++++-------- config.ru | 3 +++ lib/player.rb | 17 +++++++++++++++++ views/paper.erb | 1 + views/results.erb | 15 ++++++++++----- views/rock.erb | 2 ++ views/scissors.erb | 1 + views/submitted-name.erb | 4 ---- 8 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 lib/player.rb create mode 100644 views/paper.erb create mode 100644 views/rock.erb create mode 100644 views/scissors.erb delete mode 100644 views/submitted-name.erb diff --git a/app.rb b/app.rb index d9bd9b02..085f94e9 100644 --- a/app.rb +++ b/app.rb @@ -1,4 +1,5 @@ require 'sinatra/base' +require './lib/player' # require "sinatra/reloader" if development? class RockPaperScissors < Sinatra::Base @@ -9,23 +10,35 @@ class RockPaperScissors < Sinatra::Base # end get '/' do - # session["player_name"] = params["player_name"] erb(:index) - # redirect '/submit-name' end post '/submit-name' do - session["player_name"] = params["player_name"] - @player_name = session["player_name"] - # erb (:submitted-name) erb(:results) end - get '/results' do - @player_name = session["player_name"] + # get '/results' do + # @player_name = session["player_name"] - erb(:results) + # erb(:results) + # end + + get '/rock' do + 'You chose rock' + player = Player.new + $pc_plays = player.pc_plays + erb(:rock) + end + + get '/paper' do + "you chose paper" + # erb(:paper) + end + + get '/scissors' do + "you chose scissors" + # erb(:scissors) end run! if app_file == $0 diff --git a/config.ru b/config.ru index a33b6bb0..f7cfbf79 100644 --- a/config.ru +++ b/config.ru @@ -1,2 +1,5 @@ require_relative './app' + run RockPaperScissors + + diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 00000000..6b56346d --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,17 @@ +class Player + def pc_plays + @pc_plays = %w(rock paper scissors).sample + return @pc_plays + end + + # def game(rps) + + # if rps == "rock" && @computer_plays == "paper" + + # elsif + + # elsif + + # elsif + +end diff --git a/views/paper.erb b/views/paper.erb new file mode 100644 index 00000000..4d270dae --- /dev/null +++ b/views/paper.erb @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/views/results.erb b/views/results.erb index 8967fa07..b35f5b5d 100644 --- a/views/results.erb +++ b/views/results.erb @@ -1,10 +1,15 @@

ROCK PAPER SCISSORS!

<%=session["player_name"]%>, let's play! Choose your weapon:

-
- - - + + + +
+ +
+
+
+ +
\ No newline at end of file diff --git a/views/rock.erb b/views/rock.erb new file mode 100644 index 00000000..2c0b6b9e --- /dev/null +++ b/views/rock.erb @@ -0,0 +1,2 @@ + +

Computer plays <%= $pc_plays%>

\ No newline at end of file diff --git a/views/scissors.erb b/views/scissors.erb new file mode 100644 index 00000000..70da01c1 --- /dev/null +++ b/views/scissors.erb @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/views/submitted-name.erb b/views/submitted-name.erb deleted file mode 100644 index 744c392b..00000000 --- a/views/submitted-name.erb +++ /dev/null @@ -1,4 +0,0 @@ -
- - -
\ No newline at end of file From c40e4042da943ecd8cc9606da8266dd12021b129 Mon Sep 17 00:00:00 2001 From: erb16 Date: Fri, 8 Jul 2022 16:10:30 +0100 Subject: [PATCH 3/4] complete with global variables --- app.rb | 17 ++++++++++++----- lib/player.rb | 43 ++++++++++++++++++++++++++++++++++++------- views/paper.erb | 4 +++- views/rock.erb | 4 +++- views/scissors.erb | 3 ++- 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/app.rb b/app.rb index 085f94e9..b7a66f92 100644 --- a/app.rb +++ b/app.rb @@ -25,20 +25,27 @@ class RockPaperScissors < Sinatra::Base # end get '/rock' do - 'You chose rock' player = Player.new $pc_plays = player.pc_plays + $player_plays = "rock" + $result = player.rock erb(:rock) end get '/paper' do - "you chose paper" - # erb(:paper) + player = Player.new + $pc_plays = player.pc_plays + $player_plays = "paper" + $result = player.paper + erb(:rock) end get '/scissors' do - "you chose scissors" - # erb(:scissors) + player = Player.new + $pc_plays = player.pc_plays + $player_plays = "scissors" + $result = player.scissors + erb(:rock) end run! if app_file == $0 diff --git a/lib/player.rb b/lib/player.rb index 6b56346d..37c39dd9 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -4,14 +4,43 @@ def pc_plays return @pc_plays end - # def game(rps) - - # if rps == "rock" && @computer_plays == "paper" - - # elsif + def rock + if $pc_plays == "scissors" + $gameplay = "You win!" + return $gameplay + elsif $pc_plays == "paper" + $gameplay = "You lose!" + return $gameplay + elsif $pc_plays == "rock" + $gameplay = "It's a tie!" + return $gameplay + end + end - # elsif + def scissors + if $pc_plays == "scissors" + $gameplay = "It's a tie!" + return $gameplay + elsif $pc_plays == "paper" + $gameplay = "You win!" + return $gameplay + elsif $pc_plays == "rock" + $gameplay = "You lose! :(" + return $gameplay + end + end - # elsif + def paper + if $pc_plays == "scissors" + $gameplay = "You lose!" + return $gameplay + elsif $pc_plays == "paper" + $gameplay = "You win!" + return $gameplay + elsif $pc_plays == "rock" + $gameplay = "You lose! :(" + return $gameplay + end + end end diff --git a/views/paper.erb b/views/paper.erb index 4d270dae..30043128 100644 --- a/views/paper.erb +++ b/views/paper.erb @@ -1 +1,3 @@ - \ No newline at end of file + +

Computer plays <%= $pc_plays%>

+

You play <%= $player_plays&>

\ No newline at end of file diff --git a/views/rock.erb b/views/rock.erb index 2c0b6b9e..65701820 100644 --- a/views/rock.erb +++ b/views/rock.erb @@ -1,2 +1,4 @@ -

Computer plays <%= $pc_plays%>

\ No newline at end of file +

Your <%=$player_plays%> takes on the computer's <%=$pc_plays%>...

+ +

Result: <%=$result%>

diff --git a/views/scissors.erb b/views/scissors.erb index 70da01c1..62a464c3 100644 --- a/views/scissors.erb +++ b/views/scissors.erb @@ -1 +1,2 @@ - \ No newline at end of file + +

Computer plays <%= $pc_plays%>

From 754a647d8f4c7054fb07c07c3d6cd80bebb1f8ce Mon Sep 17 00:00:00 2001 From: erb16 Date: Mon, 11 Jul 2022 10:18:08 +0100 Subject: [PATCH 4/4] Friday finish --- lib/player.rb | 18 ++++---- spec/feature_spec.rb | 3 +- spec/features/choose_rock_spec.rb | 9 ++++ spec/features/enter_name_spec.rb | 8 ++++ spec/features/test_page_spec.rb | 2 +- spec/player_spec.rb | 69 +++++++++++++++++++++++++++++++ views/paper.erb | 3 -- views/scissors.erb | 2 - 8 files changed, 97 insertions(+), 17 deletions(-) create mode 100644 spec/features/choose_rock_spec.rb create mode 100644 spec/features/enter_name_spec.rb create mode 100644 spec/player_spec.rb delete mode 100644 views/paper.erb delete mode 100644 views/scissors.erb diff --git a/lib/player.rb b/lib/player.rb index 37c39dd9..38508c2c 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,32 +1,32 @@ class Player def pc_plays @pc_plays = %w(rock paper scissors).sample - return @pc_plays + # return @pc_plays end def rock if $pc_plays == "scissors" $gameplay = "You win!" - return $gameplay + # return $gameplay elsif $pc_plays == "paper" $gameplay = "You lose!" - return $gameplay + # return $gameplay elsif $pc_plays == "rock" $gameplay = "It's a tie!" - return $gameplay + # return $gameplay end end def scissors if $pc_plays == "scissors" $gameplay = "It's a tie!" - return $gameplay + # return $gameplay elsif $pc_plays == "paper" $gameplay = "You win!" - return $gameplay + # return $gameplay elsif $pc_plays == "rock" $gameplay = "You lose! :(" - return $gameplay + # return $gameplay end end @@ -35,10 +35,10 @@ def paper $gameplay = "You lose!" return $gameplay elsif $pc_plays == "paper" - $gameplay = "You win!" + $gameplay = "It's a tie!" return $gameplay elsif $pc_plays == "rock" - $gameplay = "You lose! :(" + $gameplay = "You win!" return $gameplay end end diff --git a/spec/feature_spec.rb b/spec/feature_spec.rb index 7f684eeb..4cc14832 100644 --- a/spec/feature_spec.rb +++ b/spec/feature_spec.rb @@ -1,6 +1,5 @@ describe '#name' do - it 'returns the name' do + xit 'returns the name' do expect(Bob.name).to eq 'Bob' end end - diff --git a/spec/features/choose_rock_spec.rb b/spec/features/choose_rock_spec.rb new file mode 100644 index 00000000..439c7d94 --- /dev/null +++ b/spec/features/choose_rock_spec.rb @@ -0,0 +1,9 @@ +feature 'Choose rock weapon' do + scenario 'clicking on rock button' do + visit('/submit-name') + fill_in :player_1_name, with: 'Bob' + click_button 'Submit' + click_button 'ROCK' + expect(page).to have_content 'rock' + end +end \ No newline at end of file diff --git a/spec/features/enter_name_spec.rb b/spec/features/enter_name_spec.rb new file mode 100644 index 00000000..c19ae029 --- /dev/null +++ b/spec/features/enter_name_spec.rb @@ -0,0 +1,8 @@ +feature 'Enter names' do + scenario 'submitting name' do + visit('/') + fill_in :player_name, with: 'Bob' + click_button 'Submit' + expect(page).to have_content 'Bob' + end +end \ No newline at end of file diff --git a/spec/features/test_page_spec.rb b/spec/features/test_page_spec.rb index 5d6e8919..6f061e87 100644 --- a/spec/features/test_page_spec.rb +++ b/spec/features/test_page_spec.rb @@ -1,5 +1,5 @@ feature 'test page' do - scenario 'visit test page' do + xscenario 'visit test page' do visit '/test' expect(page).to have_content('test page') end diff --git a/spec/player_spec.rb b/spec/player_spec.rb new file mode 100644 index 00000000..3a00ff76 --- /dev/null +++ b/spec/player_spec.rb @@ -0,0 +1,69 @@ +describe Player do + it "chooses a pc weapon at random" do + player = Player.new + expect(['rock', 'paper', 'scissors']).to include(player.pc_plays) + end + + + context "when player chooses rock" do + it "correctly identifies win" do + player = Player.new + $pc_plays = "scissors" + expect(player.rock).to eq "You win!" + end + + it "correctly identifies lost" do + player = Player.new + $pc_plays = "paper" + expect(player.rock).to eq "You lose!" + end + + it "correctly identifies draw" do + player = Player.new + $pc_plays = "rock" + expect(player.rock).to eq "It's a tie!" + end + end + + context "when player chooses scissors" do + it "correctly identifies win" do + player = Player.new + $pc_plays = "paper" + expect(player.scissors).to eq "You win!" + end + + it "correctly identifies lost" do + player = Player.new + $pc_plays = "rock" + expect(player.scissors).to eq "You lose! :(" + end + + it "correctly identifies draw" do + player = Player.new + $pc_plays = "scissors" + expect(player.scissors).to eq "It's a tie!" + end + end + + context "when player chooses paper" do + it "correctly identifies win" do + player = Player.new + $pc_plays = "rock" + expect(player.paper).to eq "You win!" + end + + it "correctly identifies lost" do + player = Player.new + $pc_plays = "scissors" + expect(player.paper).to eq "You lose!" + end + + it "correctly identifies draw" do + player = Player.new + $pc_plays = "paper" + expect(player.paper).to eq "It's a tie!" + end + end +end + + diff --git a/views/paper.erb b/views/paper.erb deleted file mode 100644 index 30043128..00000000 --- a/views/paper.erb +++ /dev/null @@ -1,3 +0,0 @@ - -

Computer plays <%= $pc_plays%>

-

You play <%= $player_plays&>

\ No newline at end of file diff --git a/views/scissors.erb b/views/scissors.erb deleted file mode 100644 index 62a464c3..00000000 --- a/views/scissors.erb +++ /dev/null @@ -1,2 +0,0 @@ - -

Computer plays <%= $pc_plays%>