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

idris' rps #235

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -14,3 +14,5 @@ end
group :development, :test do
gem 'rubocop', '1.20'
end

gem "webrick", "~> 1.7"
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -82,6 +82,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 @@ -96,6 +97,7 @@ DEPENDENCIES
simplecov
simplecov-console
sinatra
webrick (~> 1.7)

RUBY VERSION
ruby 3.0.2p107
Expand Down
26 changes: 23 additions & 3 deletions app.rb
@@ -1,8 +1,28 @@
require 'sinatra/base'
class RockPaperScissors < Sinatra::Base
get '/test' do
'test page'
require './lib/game'
class App < Sinatra::Base


get '/' do
erb :form
end

post '/player_weapon' do
p params
@player_name = params[:player_name]
erb :player_weapon
end



post '/result' do
$game = Game.new(:player_weapon)
@score = $game.result
erb :result
end



run! if app_file == $0
end

2 changes: 1 addition & 1 deletion config.ru
@@ -1,2 +1,2 @@
require_relative './app'
run RockPaperScissors
run App
8 changes: 4 additions & 4 deletions docs/review.md
Expand Up @@ -383,7 +383,7 @@ end

class RPSWeb < Sinatra::Application
get '/choose' do
@player1_choice = params[:choice]
@@player_weapon_choice = params[:choice]
@game = Game.current_game(session[:game_id])
erb :result
end
Expand All @@ -392,7 +392,7 @@ end

```html
<h1>
<% @game.player1_choice(@player1_choice) %>
<% @game.@player_weapon_choice(@@player_weapon_choice) %>
<% if @game.result == :win %>
Congratulations - you won
<% else %>
Expand All @@ -412,7 +412,7 @@ end

class RPSWeb < Sinatra::Application
get '/choose' do
@game.player1_choice(params[:choice])
@game.@player_weapon_choice(params[:choice])
erb @game.result
end
end
Expand Down Expand Up @@ -464,7 +464,7 @@ end

class RPSWeb < Sinatra::Application
get '/play' do
@game.player1_choice = params[:choice]
@game.@player_weapon_choice = params[:choice]
erb @game.result
end
end
Expand Down
36 changes: 36 additions & 0 deletions lib/game.rb
@@ -0,0 +1,36 @@
class Game

attr_reader :player_weapon, :computer_weapon

def initialize(weapon)
@player_weapon = weapon
computer_weapon
end

def computer_weapon
weapons = ["rock", "paper", "scissors"]
computer_choice = weapons.sample
end

def result
if (@player_weapon == "rock" && computer_weapon == "scissors")
return "You wins"
elsif (@player_weapon == "scissors" && computer_weapon == "paper")
return "You wins"
elsif (@player_weapon == "paper" && computer_weapon == "rock")
return "You wins"
elsif (computer_weapon == "rock" && @player_weapon == "scissors")
return "Computer wins"
elsif (computer_weapon == "scissors" && @player_weapon == "paper")
return "Computer wins"
elsif (computer_weapon == "paper" && @player_weapon == "rock")
return "Computer wins"
else
return "It's a draw"
end
end

end

# rps = Game.new("rock")
# rps.result
1 change: 1 addition & 0 deletions lib/weapon.rb
@@ -0,0 +1 @@
class Weapon
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -14,7 +14,7 @@
ENV['RACK_ENV'] = 'test'

require File.join(File.dirname(__FILE__), '..', 'app.rb')
Capybara.app = RockPaperScissors
Capybara.app = App

RSpec.configure do |config|
config.after(:suite) do
Expand Down
10 changes: 10 additions & 0 deletions views/form.erb
@@ -0,0 +1,10 @@

<h1>Welcome to Rock Paper Scissors!<h1>
<h2>Sign in to play<h2>
<form action="/player_weapon" method="post">
<label for="player_name">
Player Name:</label>
<input type="text" id="player_name" name="player_name" placeholder="Enter your name">
<label>
<input type="submit" value="Submit">
</form>
15 changes: 15 additions & 0 deletions views/player_weapon.erb
@@ -0,0 +1,15 @@
<p1> Choose your weapon <%= @player_name.capitalize %> </p1>
<br>
<br>
<br>
<form action="/result" method="post">
<input id="rock" type="submit" name="player_weapon" value="rock">
</form>
<br>
<form action="/result" method="post">
<input id="paper" type="submit" name="player_weapon" value="paper">
</form>
<br>
<form action="/result" method="post">
<input id="scissors" type="submit" name="player_weapon" value="scissors">
</form>
3 changes: 3 additions & 0 deletions views/result.erb
@@ -0,0 +1,3 @@

<h1> <%= @score %> </h1>