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

Shannon RPS #238

Open
wants to merge 2 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
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ gem 'sinatra'
group :test do
gem 'capybara'
gem 'rspec'
gem 'thin'
gem 'puma'
gem 'reel'
gem 'http'
gem 'webrick'
gem 'simplecov', require: false
gem 'simplecov-console', require: false
end
Expand Down
25 changes: 25 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
require 'sinatra/base'
require 'sinatra/reloader'
require_relative 'lib/game'

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

get '/test' do
'test page'
end

get '/' do
erb(:index)
end

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

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

end

run! if app_file == $0
end
33 changes: 33 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Game
def initialize(answer)
@answer = answer
@answers = ["Rock", "Paper", "Scissors"]
@computers_answer = computers_answer
end

def player
@answer
end

def computer
@computers_answer
end

def computers_answer
@answers.sample
end

def result
get_result(computer)
end

def get_result(computer)
if @answer == computer
return "Draw"
elsif @answers[@answers.index(@answer) - 1] == computer #computer answer is my answer -1, then I win
return "You win!"
else
"You lose!"
end
end
end
20 changes: 20 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

body {

background-image: url("https://wallpapercave.com/wp/wp8121936.jpg");
background-position: center;
}

h1 {
color: white;
text-align: center;
padding: 50px;

}
label {
text-align:right;
padding-right:20px;
display:inline-block;
min-width:150px;
}

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

RSpec.describe 'Game' do
it 'returns the users answer' do
game = Game.new("Rock")
expect(game.player).to eq "Rock"
end
end
9 changes: 9 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<head>
<link rel="stylesheet" href="<%= url('/style.css') %>">
</head>

<h1>Welcome to Rock, Paper, Scissors!</h1>
<form action="/play" method="post">
<input type="text" name="player" placeholder="Enter your name here">
<input type="submit" value="Play">
</form>
15 changes: 15 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<head>
<link rel="stylesheet" href="<%= url('/style.css') %>">
</head>

<h1>Welcome <%= @player %>!</h1>

<form action="/result" method="post">
<label for="game">Make your choice:</label>
<select name="answer" id="answer">
<option value="Rock">Rock</option>
<option value="Paper">Paper</option>
<option value="Scissors">Scissors</option>
</select>
<input type="submit" value="Go">
</form>
7 changes: 7 additions & 0 deletions views/result.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<head>
<link rel="stylesheet" href="<%= url('/style.css') %>">
</head>

You chose <%= @answer %> and
Computer chose <%= @computer %> =
<%= @result %>