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

changed the route from test to index #249

Open
wants to merge 13 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
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ ruby '3.0.2'

gem 'sinatra'

# I've added
gem 'webrick'
gem 'capybara'
gem 'launchy'
# END

group :test do
gem 'capybara'
gem 'rspec'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ GEM
xpath (~> 3.2)
diff-lcs (1.4.4)
docile (1.4.0)
launchy (2.5.0)
addressable (~> 2.7)
mini_mime (1.1.1)
mini_portile2 (2.6.1)
mustermann (1.1.1)
Expand Down Expand Up @@ -82,6 +84,7 @@ GEM
unicode-display_width (>= 1.1.1, < 3)
tilt (2.0.10)
unicode-display_width (1.6.1)
webrick (1.7.0)
xpath (3.2.0)
nokogiri (~> 1.8)

Expand All @@ -91,11 +94,13 @@ PLATFORMS

DEPENDENCIES
capybara
launchy
rspec
rubocop (= 1.20)
simplecov
simplecov-console
sinatra
webrick

RUBY VERSION
ruby 3.0.2p107
Expand Down
36 changes: 34 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
require 'sinatra/base'
require './lib/game'
require './lib/player'

# require 'sinatra'
class RockPaperScissors < Sinatra::Base
get '/test' do
'test page'

enable :sessions

# root contains a form that directs to /play via /name
get '/' do
erb(:index)
end

post '/name' do
$player = Player.new(params[:first_name])
$game = Game.new($player)
#session[:first_name] = params[:first_name]
redirect '/play'
end

# /play contains buttons that direct to /result via /played
get '/play' do
# @name = session[:first_name]
erb(:play)
end

post '/played' do
session[:button] = params[:button]
redirect '/result'
end

get '/result' do
#@name = session[:first_name]
@button = session[:button]
erb(:result)
end

run! if app_file == $0
Expand Down
Binary file added assets/Screenshot 2022-07-08 at 17.31.43.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Game
def initialize(player)
@player = player
@guess = ["rock", "paper", "scissors"]
end

def guess
return @guess.sample
end
end
6 changes: 6 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Player
attr_reader :name
def initialize(name)
@name = name
end
end
25 changes: 25 additions & 0 deletions spec/features/enter_name_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# feature 'test page' do
# scenario 'visit test page' do
# visit '/test'
# expect(page).to have_content('test page')
# end
# end

feature 'homepage' do
scenario 'to have a submit button' do
visit '/'
expect(page).to have_button('Submit')
end

scenario 'to fill in the form' do
visit '/'
expect(page.fill_in 'Enter your name', with: 'Miranda')
end

scenario 'form to post content on /play page' do
visit '/'
page.fill_in 'Enter your name', with: 'Miranda'
page.click_on 'Submit'
expect(page).to have_content 'Miranda\'s turn to play'
end
end
40 changes: 40 additions & 0 deletions spec/features/play_page_buttons_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
feature 'rock paper scissors buttons' do
scenario 'rock button exists' do
visit '/play'
expect(page).to have_button('Rock')
end

scenario 'paper button exists' do
visit '/play'
expect(page).to have_button('Paper')
end

scenario 'scissors button exists' do
visit '/play'
expect(page).to have_button('Scissors')
end

scenario 'click on paper button' do
visit '/'
page.fill_in 'Enter your name', with: 'Miranda'
page.click_on 'Submit'
page.click_on 'Paper'
expect(page).to have_content "Miranda played paper"
end

scenario 'click on rock button' do
visit '/'
page.fill_in 'Enter your name', with: 'Miranda'
page.click_on 'Submit'
page.click_on 'Rock'
expect(page).to have_content "Miranda played rock"
end

scenario 'click on scissors button' do
visit '/'
page.fill_in 'Enter your name', with: 'Miranda'
page.click_on 'Submit'
page.click_on 'Scissors'
expect(page).to have_content "Miranda played scissors"
end
end
6 changes: 0 additions & 6 deletions spec/features/test_page_spec.rb

This file was deleted.

12 changes: 12 additions & 0 deletions spec/game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'game'

RSpec.describe Game do
context 'a game has a computor guess' do
it 'returns the value of the computer\'s play' do
game = double(:game, guess: "paper")
expect(game.guess).to eq 'paper'
end


end
end
10 changes: 10 additions & 0 deletions spec/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'player'

RSpec.describe Player do
context 'create a new player with a name' do
it 'returns the name of the player'do
player = Player.new('Miranda')
expect(player.name).to eq 'Miranda'
end
end
end
16 changes: 16 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Ready to play rock, paper, scissors?</h1>
<br>
<br>
<h2>
<form action="/name" method="POST">
<label for="name" method="POST">Enter your name</label><br>
<input type="text" name="first_name" id="name">
<input type="submit" value="Submit">
</h2>
</body>
</html>
13 changes: 13 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1><%= $player.name %>'s turn to play</h1>
<form action='/played' method="post">
<input type="submit" value="Rock" name="button">
<input type="submit" value="Paper" name="button">
<input type="submit" value="Scissors" name="button">
</form>
</body>
</html>
28 changes: 28 additions & 0 deletions views/result.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<%computer_guess = $game.guess%>
<h2><%=$player.name%> played <%[email protected]%></h2><br>
<h2>The computer played <%=computer_guess%></h2>
<br>
<h1>
<%if @button.downcase == 'scissors' && computer_guess == 'rock' %>
Computer wins!
<% elsif @button.downcase == 'scissors' && computer_guess == 'paper' %>
<%= $player.name %> wins!
<% elsif @button.downcase == 'paper' && computer_guess == 'rock' %>
<%= $player.name %> wins!
<% elsif @button.downcase == 'paper' && computer_guess == 'scissors' %>
Computer wins!
<% elsif @button.downcase == 'rock' && computer_guess == 'scissors' %>
<%= $player.name %> wins!
<% elsif @button.downcase == 'rock' && computer_guess == 'paper' %>
Computer wins!
<% else %>
It's a draw!
<% end %>
</h1>
</body>
</html>