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

Pull Request for Rock, Paper, Scissors challenge #229

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added 5777371.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Gemfile
Expand Up @@ -14,3 +14,9 @@ end
group :development, :test do
gem 'rubocop', '1.20'
end

# gem 'thin'
gem 'puma'
gem 'reel'
gem 'http'
gem 'webrick'
110 changes: 79 additions & 31 deletions Gemfile.lock
Expand Up @@ -5,51 +5,87 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
ansi (1.5.0)
ast (2.4.2)
capybara (3.35.3)
capybara (3.36.0)
addressable
matrix
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
diff-lcs (1.4.4)
celluloid (0.18.0)
timers (~> 4)
celluloid-io (0.17.3)
celluloid (>= 0.17.2)
nio4r (>= 1.1)
timers (>= 4.1.1)
diff-lcs (1.5.0)
docile (1.4.0)
mini_mime (1.1.1)
mini_portile2 (2.6.1)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
ffi (1.15.5)
ffi-compiler (1.0.1)
ffi (>= 1.0.0)
rake
http (5.0.4)
addressable (~> 2.8)
http-cookie (~> 1.0)
http-form_data (~> 2.2)
llhttp-ffi (~> 0.4.0)
http-cookie (1.0.4)
domain_name (~> 0.5)
http-form_data (2.3.0)
http_parser.rb (0.8.0)
llhttp-ffi (0.4.0)
ffi-compiler (~> 1.0)
rake (~> 13.0)
matrix (0.4.2)
mini_mime (1.1.2)
mini_portile2 (2.8.0)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nokogiri (1.12.3)
mini_portile2 (~> 2.6.1)
nio4r (2.5.8)
nokogiri (1.13.3)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.12.3-x86_64-darwin)
nokogiri (1.13.3-x86_64-darwin)
racc (~> 1.4)
parallel (1.20.1)
parser (3.0.2.0)
parallel (1.22.0)
parser (3.1.1.0)
ast (~> 2.4.1)
public_suffix (4.0.6)
racc (1.5.2)
puma (5.6.2)
nio4r (~> 2.0)
racc (1.6.0)
rack (2.2.3)
rack-protection (2.1.0)
rack-protection (2.2.0)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rainbow (3.0.0)
regexp_parser (2.1.1)
rainbow (3.1.1)
rake (13.0.6)
reel (0.6.1)
celluloid (>= 0.15.1)
celluloid-io (>= 0.15.0)
http (>= 0.6.0.pre)
http_parser.rb (>= 0.6.0)
websocket-driver (>= 0.5.1)
regexp_parser (2.2.1)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rubocop (1.20.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
Expand All @@ -59,8 +95,8 @@ GEM
rubocop-ast (>= 1.9.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.11.0)
parser (>= 3.0.1.1)
rubocop-ast (1.16.0)
parser (>= 3.1.1.0)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
simplecov (0.21.2)
Expand All @@ -72,16 +108,24 @@ GEM
simplecov
terminal-table
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
sinatra (2.1.0)
simplecov_json_formatter (0.1.4)
sinatra (2.2.0)
mustermann (~> 1.0)
rack (~> 2.2)
rack-protection (= 2.1.0)
rack-protection (= 2.2.0)
tilt (~> 2.0)
terminal-table (3.0.1)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
tilt (2.0.10)
unicode-display_width (1.6.1)
timers (4.3.3)
unf (0.1.4)
unf_ext
unf_ext (0.0.8.1)
unicode-display_width (2.1.0)
webrick (1.7.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)

Expand All @@ -91,11 +135,15 @@ PLATFORMS

DEPENDENCIES
capybara
http
puma
reel
rspec
rubocop (= 1.20)
simplecov
simplecov-console
sinatra
webrick

RUBY VERSION
ruby 3.0.2p107
Expand Down
24 changes: 24 additions & 0 deletions app.rb
@@ -1,8 +1,32 @@
require 'sinatra/base'
require 'sinatra/reloader'
require_relative 'lib/game'

class RockPaperScissors < Sinatra::Base
enable :sessions
configure :development do
register Sinatra::Reloader
end

get '/test' do
'test page'
end

get '/' do
erb :index
end

post '/play' do
@player = params[:player_name]
erb :play
end

post '/result' do
@input = params[:input]
@game = Game.new(@input)
erb :result
end


run! if app_file == $0
end
29 changes: 29 additions & 0 deletions lib/game.rb
@@ -0,0 +1,29 @@
class Game
def initialize(input)
@input = input
@results = ["Rock", "Paper", "Scissors"]
Copy link
Contributor

Choose a reason for hiding this comment

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

I would make this a constant rather than a variable.

@computer_answer = get_computer_answer
end

def computer_answer
@computer_answer
end

def get_computer_answer
@results.sample
end

def user_answer
@input
end

def get_result
if user_answer == computer_answer
"Draw"
elsif @results[@results.index(user_answer) -1] == computer_answer
"Win"
else
"Lose"
end
end
end
11 changes: 11 additions & 0 deletions public/style.css
@@ -0,0 +1,11 @@
.background_1 {
background-image:url(https://wallpaperaccess.com/full/5580296.png)
}

.background_2 {
background-image:url(https://cdn.dribbble.com/users/976907/screenshots/4879640/media/bf71d0fd9b5bd1d5558eb66b7f9a7fcb.gif)
}

.contents {
text-align: center;
}
35 changes: 35 additions & 0 deletions spec/unit/game_spec.rb
@@ -0,0 +1,35 @@
require 'game'

RSpec.describe Game do
it 'returns the user input' do
game = Game.new("Rock")
result = game.user_answer
expect(result).to eq "Rock"
end

context 'when user chooses rock and opponent chooses scissors' do
it 'returns Win as a string' do
game = Game.new("Rock")
allow(game).to receive(:computer_answer).and_return("Scissors")

expect(game.get_result).to eq "Win"
end
end

context 'when user chooses rock and opponent chooses paper' do
it 'returns Lose as a string' do
game = Game.new("Rock")
allow(game).to receive(:computer_answer).and_return("Paper")
expect(game.get_result).to eq "Lose"
end
end

context 'when user chooses rock and opponent chooses rock' do
it 'returns Draw as a string' do
game = Game.new("Rock")
allow(game).to receive(:computer_answer).and_return("Rock")
expect(game.get_result).to eq "Draw"
end
end

end
16 changes: 16 additions & 0 deletions views/index.erb
@@ -0,0 +1,16 @@
<head>
<link rel="stylesheet" href="<%= url('/style.css') %>">
</head>
<body class='background_1'>
<div class=contents>
<h1>Let's play!</h1>
<h2>Rock, Paper, Scissors</h2>
<form action="/play" method="post">
<label for="player_name">
Player name
<input type="text" name="player_name">
</label>
<input type="submit" value="Submit">
</form>
</div>
</body>
18 changes: 18 additions & 0 deletions views/play.erb
@@ -0,0 +1,18 @@
<head>
<link rel="stylesheet" href="<%= url('/style.css') %>">
</head>
<body class='background_2'>
<div class=Contents>
<h1>Welcome, <%= @player %>! </h1>
<h2>Please select an option</h2>
<form action='/result' method='post'>
<label for="input">Choose your fighter:</label>
<select name="input" id="input">
<option value="Rock">Rock</option>
<option value="Paper">Paper</option>
<option value="Scissors">Scissors</option>
</select>
<input type="submit" value="GO!"/>
</form>
</div>
</body>
7 changes: 7 additions & 0 deletions views/result.erb
@@ -0,0 +1,7 @@
<head>
<link rel="stylesheet" href="<%= url('/style.css') %>">
</head>

You chose <%= @input %> as your fighter
Computer chose <%= @game.computer_answer %> as their fighter
You <%= @game.get_result %>!