Skip to content
29 changes: 27 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'sinatra/base'
require 'sinatra/reloader'
require './lib/player'
require './lib/game'

class Rps < Sinatra::Base
configure :development do
Expand All @@ -13,14 +15,37 @@ class Rps < Sinatra::Base
enable :sessions

post '/names' do
session[:player_1_name] = params[:player_1_name]

$game = Game.new(Player.new(params[:player]))
redirect to('/play')
end

get '/play' do
@player_1_name = session[:player_1_name]
@game = $game
@message = session[:message]
erb :play
end

post '/rock' do
@game = $game
@game.rock
session[:message] = @game.message
redirect '/play'
end

post '/paper' do
@game = $game
@game.paper
session[:message] = @game.message
redirect '/play'
end

post '/scissors' do
@game = $game
@game.scissors
session[:message] = @game.message
redirect '/play'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Currently, you define a route for each player move option, and each of these routes uses mostly the same code. This is a duplication of code that could be refactored.

Another method of approach this logic is to pass in the player move as a parameter, calling the same route, and passing that param into the @GAMe instance.

end

run! if app_file == $0
end
8 changes: 8 additions & 0 deletions features/enter_names_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@

expect(page).to have_content("Farzan Vs. Computer")
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good, specific content requirements throughout the tests - I need to tighten mine up.

scenario "display homepage" do
visit '/'
expect(page).to have_content("Welcome to: Rock, Paper, Scissors!")
expect(page).to have_content("Please enter your name to start:")
expect(page).to have_button("Let's play!")
expect(page).to have_button("Reset")
end
end
61 changes: 61 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Game

attr_reader :player, :win, :message

def initialize(player)
@player = player
@win = nil
@message = ""
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this feels like neat logic - very easy to read

def rock
case rpc_play
when "rock"
@win = nil
when "paper"
@win = false
when "scissors"
@win = true
end
end

def paper
case rpc_play
when "rock"
@win = true
when "paper"
@win = nil
when "scissors"
@win = false
end
end

def scissors
case rpc_play
when "rock"
@win = false
when "paper"
@win = true
when "scissors"
@win = nil
end
end

def message
case @win
when true
@message = "You've won! Well done!"
when false
@message = "You've lost! Try again!"
when nil
@message = "It's a tie! Try again!"
end
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This approach can be simplified to make this class more concise. Check out the following approach: https://github.com/makersacademy/rps-challenge/blob/main/docs/review.md#use-of-ifelsif-conditionals-for-business-logic


private

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You could consider keeping the computer in a separate class

def rpc_play
["rock", "paper", "scissors"].sample
end

end
9 changes: 9 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Player

attr_reader :name

def initialize(name)
@name = name
end

end
63 changes: 36 additions & 27 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
<h1>Welcome to: Rock, Paper, Scissors!</h1>

<form action="/names" method="post">
<div>
<label>
Please enter your name to start:
<input for="player name" type="text" name="player_1_name" placeholder="Enter your name" required>
</label>
</div>

<div>
<label>
Please enter the weapon of your choice:
<input for="weapon" type="text" name="weapon" placeholder="rock, paper or scissors" required>
</label>
</div>

<div>
<input type="submit">
<input type="reset">
</div>

</form>

<div style='border: 3px dashed blue'>
<img src='https://cdn.vox-cdn.com/thumbor/D_2XiTfAk5x7VXxfYIJ3bZNUByw=/0x0:2429x2396/920x0/filters:focal(0x0:2429x2396):format(webp):no_upscale()/cdn.vox-cdn.com/uploads/chorus_asset/file/3488502/shutterstock_106919999.0.jpg'>
</div>
<!DOCTYPE html>
<html lang='en'>
<head>
<h1 style='text-align:center'>Welcome to: Rock, Paper, Scissors!</h1>
</head>

<body>

<form action="/names" style='text-align:center' method="post">

<label>
Please enter your name to start:
<input type="text" name="player" placeholder="Enter your name" required>
</label>

<input type="submit" value="Let's play!">
<input type="reset">

</form>

<br>
<br>

<style>
img {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>


<img src='https://miro.medium.com/max/612/1*G9UfaUBS_VqtFILMe37fZw.jpeg' style="width:50%;">

</body>
</html>
24 changes: 17 additions & 7 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<h1><%= @player_1_name %> Vs. Computer</h1>
<head>
<h1 style='text-align:center;color:rgb(4, 71, 76)'> Let's Play <%= @game.player.name %>!</h1>
</head>

<form action="/play">
<label>
<%#
<%<input type="weapon" name="Paper"> %> %>
<h3 style='text-align:center;color:rgb(185, 10, 10)'> Rock, Paper, Scissors? </h3>

</label>
<body style='background-image:linear-gradient(rgb(6, 238, 240),rgb(4, 159, 249))'>

</form>

<table style="width:30%;margin-left:auto;margin-right:auto">
<tr style="text-align:center">
<form action="/rock" method="post"><th><button style="background-color:red; border-color:blue; color:white">Rock</button></th></form>
<form action="/paper" method="post"><th><button style="background-color:red; border-color:blue; color:white">Paper</button></th></form>
<form action="/scissors" method="post"><th><button style="background-color:red; border-color:blue; color:white">Scissors</button></th></form>
</tr>
</table>

<h3 style='text-align:center;color:rgb(4, 57, 249)' name='message'><%= @message %></h3>

</body>