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

Friday finish #259

Open
wants to merge 4 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
49 changes: 47 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
require 'sinatra/base'
require './lib/player'
# require "sinatra/reloader" if development?
class RockPaperScissors < Sinatra::Base
get '/test' do
'test page'

enable :sessions

# get '/test' do
# 'test page'
# end

get '/' do
erb(:index)

Choose a reason for hiding this comment

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

I forgot the brackets lol thanks

end

post '/submit-name' do
session["player_name"] = params["player_name"]
erb(:results)
end

# get '/results' do
# @player_name = session["player_name"]

# erb(:results)
# end

get '/rock' do
player = Player.new
$pc_plays = player.pc_plays
$player_plays = "rock"
$result = player.rock
erb(:rock)
end

get '/paper' do
player = Player.new
$pc_plays = player.pc_plays
$player_plays = "paper"
$result = player.paper
erb(:rock)
end

get '/scissors' do
player = Player.new
$pc_plays = player.pc_plays
$player_plays = "scissors"
$result = player.scissors
erb(:rock)
end

run! if app_file == $0

end
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
require_relative './app'

run RockPaperScissors


46 changes: 46 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Player
def pc_plays
@pc_plays = %w(rock paper scissors).sample
# return @pc_plays
end

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

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

def paper
if $pc_plays == "scissors"
$gameplay = "You lose!"
return $gameplay
elsif $pc_plays == "paper"
$gameplay = "It's a tie!"
return $gameplay
elsif $pc_plays == "rock"
$gameplay = "You win!"
return $gameplay
end
end

end
5 changes: 5 additions & 0 deletions spec/feature_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe '#name' do
xit 'returns the name' do
expect(Bob.name).to eq 'Bob'
end
end
9 changes: 9 additions & 0 deletions spec/features/choose_rock_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

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

Great capybara test format

end
8 changes: 8 additions & 0 deletions spec/features/enter_name_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/features/test_page_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
69 changes: 69 additions & 0 deletions spec/player_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

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

Very tidy and easy to follow


9 changes: 9 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- in views/index.erb -->
<h1>ROCK PAPER SCISSORS!</h1>
<h2>Who wishes to challenge me?</h2>
<form action="/submit-name" method="post">
<label for="player_name">
<input type="text" name="player_name">
</label>
<input type="submit" value="Submit">
</form>
15 changes: 15 additions & 0 deletions views/results.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- in views/results.erb -->
<h1>ROCK PAPER SCISSORS!</h1>
<h2> <%=session["player_name"]%>, let's play! Choose your weapon:</h2>

<form action="/rock" method="get">
<input type="submit" value="ROCK">
</form>

<form action="/paper" method="get">
<input type="submit" value="PAPER">
</form>

<form action="/scissors" method="get">
<input type="submit" value="SCISSORS">
</form>
4 changes: 4 additions & 0 deletions views/rock.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- in views/rock.erb -->
<h1>Your <%=$player_plays%> takes on the computer's <%=$pc_plays%>...</h1>

<h2>Result: <%=$result%></h2>