Skip to content
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
Empty file added Design
Empty file.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ source 'https://rubygems.org'
ruby '3.0.2'

gem 'sinatra'
gem 'sinatra-contrib'
gem 'webrick'

group :test do
gem 'capybara'
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GEM
docile (1.4.0)
mini_mime (1.1.1)
mini_portile2 (2.6.1)
multi_json (1.15.0)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nokogiri (1.12.3)
Expand Down Expand Up @@ -76,10 +77,17 @@ GEM
rack (~> 2.2)
rack-protection (= 2.1.0)
tilt (~> 2.0)
sinatra-contrib (2.1.0)
multi_json
mustermann (~> 1.0)
rack-protection (= 2.1.0)
sinatra (= 2.1.0)
tilt (~> 2.0)
terminal-table (3.0.1)
unicode-display_width (>= 1.1.1, < 3)
tilt (2.0.10)
unicode-display_width (2.0.0)
webrick (1.7.0)
xpath (3.2.0)
nokogiri (~> 1.8)

Expand All @@ -93,6 +101,8 @@ DEPENDENCIES
simplecov
simplecov-console
sinatra
sinatra-contrib
webrick

RUBY VERSION
ruby 3.0.2p107
Expand Down
54 changes: 54 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require "sinatra/base"
require "sinatra/reloader"
require "./lib/player"
require "./lib/game"

class RockPaperScissors < Sinatra::Base
enable :sessions

configure :development do
register Sinatra::Reloader
end

get '/test' do
'Testing infrastructure working!'
end

get '/' do
erb(:index)
end

post '/play' do
@player1 = Player.new(params[:player1])
@player2 = Player.new(params[:player2])
$game = Game.new(@player1, @player2)
erb(:play)
end

post '/player_1_choice' do
$game.player1.add('scissors') if params[:scissors]
$game.player1.add('rock') if params[:rock]
$game.player1.add('paper') if params[:paper]
redirect ('/round2')
end

get '/round2' do
erb(:round2)
end

post '/player_2_choice' do
$game.player2.add('scissors') if params[:scissors]
$game.player2.add('rock') if params[:rock]
$game.player2.add('paper') if params[:paper]
redirect ('/result')
end

get '/result' do

Choose a reason for hiding this comment

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

this is a clear and concise way of displaying who has won the game.

@result = "#{$game.player1.name} wins!" if $game.winner == true
@result = "#{$game.player2.name} wins!" if $game.winner == false
@result = "It's a draw!" if $game.winner.nil?
erb(:result)
end

run! if app_file == $0
end
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require './app'
run RockPaperScissors
15 changes: 15 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Game
attr_accessor :player1, :player2

def initialize(player1, player2) # user is a string representing the choice of the player
@player1 = player1 # computer is a string, representing the computer choice
@player2 = player2
end

def winner

Choose a reason for hiding this comment

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

I like this solution to the coding of the game of rock, paper, scissors

return nil if @player1.choice == @player2.choice
options = ['rock', 'paper', 'scissors']
first_play = options.find_index(@player1.choice)
options[first_play - 1] == @player2.choice
end
end
12 changes: 12 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Player
attr_accessor :name, :choice

def initialize(name)
@name = name
end

def add(choice)
@choice = choice
end

end
45 changes: 45 additions & 0 deletions spec/features/choose_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
RSpec.describe 'form for player choice' do

feature 'it asks player 1 for their choice' do
scenario 'when player 1 is called Annabelle' do
sign_in_and_play
expect(page).to have_content "Annabelle's turn: please make your choice."
end
end

feature 'player 1 chooses scissors' do
scenario 'returns their choice' do
sign_in_and_play
click_on "Scissors"
click_on "Rock"
expect(page).to have_content "Annabelle chose scissors."
end
end

feature 'player 1 chooses rock' do

Choose a reason for hiding this comment

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

Perhaps the second and third tests here are redundant if the first one is working?

scenario 'returns their choice' do
sign_in_and_play
click_on "Rock"
click_on "Paper"
expect(page).to have_content "Annabelle chose rock."
end
end

feature 'player 1 chooses paper' do
scenario 'returns their choice' do
sign_in_and_play
click_on "Paper"
click_on "Rock"
expect(page).to have_content "Annabelle chose paper."
end
end

feature 'it asks player 2 for their choice' do
scenario 'when player 2 is called Sandy' do
sign_in_and_play
click_on "Scissors"
expect(page).to have_content "Sandy's turn: please make your choice."
end
end
end

11 changes: 11 additions & 0 deletions spec/features/name_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RSpec.describe 'player name form' do
feature 'player enters their name' do
scenario 'it returns their name' do
visit ("/")
fill_in "player1", with: "Annabelle"
fill_in "player2", with: "Sandy"
click_on "Submit"
expect(page).to have_content "Welcome Annabelle and Sandy."
end
end
end
28 changes: 28 additions & 0 deletions spec/features/result_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
RSpec.describe 'results' do
feature 'player 1 picks rock and player 2 chooses scissors' do
scenario 'shows player 1 as the winner' do
sign_in_and_play
click_on 'Rock'
click_on 'Scissors'
expect(page).to have_content 'Annabelle wins!'
end
end

feature 'player 1 picks rock and player 2 picks paper' do
scenario 'shows player 1 as the winner' do

Choose a reason for hiding this comment

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

I think this line should say "shows player 2 as the winner"

sign_in_and_play
click_on 'Rock'
click_on 'Paper'
expect(page).to have_content 'Sandy wins!'
end
end

feature 'both players pick paper' do
scenario 'shows a draw' do
sign_in_and_play
click_on 'Paper'
click_on 'Paper'
expect(page).to have_content "It's a draw!"
end
end
end
10 changes: 10 additions & 0 deletions spec/features/testing_infrastructure_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RSpec.describe 'infrastructure' do

feature 'testing infrastructure' do
scenario 'can run app and check page content' do
visit('/test')
expect(page).to have_content 'Testing infrastructure working!'
end
end

end
6 changes: 6 additions & 0 deletions spec/features/web_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def sign_in_and_play
visit ("/")
fill_in "player1", with: "Annabelle"
fill_in "player2", with: "Sandy"
click_on "Submit"
end
44 changes: 44 additions & 0 deletions spec/game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'game'

RSpec.describe Game do
# let(:user) { double :player, name: 'Sandra', choice: 'scissors' }
# let(:computer) { double :computer, name: 'Computer', choice: 'paper' }
# subject(:game) { Game.new(:user, :computer) }

it 'returns the names of each player' do
player1 = double :player, name: 'Elspeth'
player2 = double :player, name: 'Cuddle'
game = Game.new(player1, player2)
expect(game.player1.name).to eq 'Elspeth'
expect(game.player2.name).to eq 'Cuddle'
end

it "returns the players' choices" do
player1 = double :player, choice: 'scissors'
player2 = double :player, choice: 'rock'
game = Game.new(player1, player2)
expect(game.player1.choice).to eq 'scissors'
expect(game.player2.choice).to eq 'rock'
end

it "returns true when player 1 wins" do
player1 = double :player, name: 'Sandra', choice: 'scissors'
player2 = double :player, name: 'Cuddles', choice: 'paper'
game = Game.new(player1, player2)
expect(game.winner).to eq true
end

it 'returns false when the player 2 wins' do
player1 = double :player, name: 'Sandra', choice: 'scissors'
player2 = double :player, name: 'Cuddles', choice: 'rock'
game = Game.new(player1, player2)
expect(game.winner).to eq false
end

it 'returns nil when there is a drawer' do
player1 = double :player, name: 'Sandra', choice: 'rock'
player2 = double :player, name: 'Cuddles', choice: 'rock'
game = Game.new(player1, player2)
expect(game.winner).to eq nil
end
end
14 changes: 14 additions & 0 deletions spec/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'player'

RSpec.describe Player do
it 'returns the player name' do
player = Player.new('Sandra')
expect(player.name).to eq 'Sandra'
end

it 'add a choice to the player' do
player = Player.new('Sandra')
player.add('scissors')
expect(player.choice).to eq 'scissors'
end
end
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
require File.join(File.dirname(__FILE__), '..', 'app.rb')
require 'capybara'
require 'capybara/rspec'
require 'rspec'
require './spec/features/web_helpers'
ENV['RACK_ENV'] = 'test'
Capybara.app = RockPaperScissors

require 'simplecov'
require 'simplecov-console'

Expand Down
13 changes: 13 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>Let's play Rock Paper Scissors! <p></p></h1>
Please enter your name to begin. <p> </p>
<form action='/play' method='post'>
<label for="player_1_name">
Player 1 Name
<input type="text" name="player1" placeholder="Player 1" />
</label>
<label for="player_2_name">
Player 2 Name
<input type="text" name="player2" placeholder="Player 1" />
</label>
<input type="submit" value="Submit" />
</form>
9 changes: 9 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Welcome <%[email protected]%> and <%[email protected]%>. <p></p>

<%[email protected]%>'s turn: please make your choice. <p></p>

<form action="/player_1_choice" method="post">
<input type="submit" name="scissors" value="Scissors"/>
<input type="submit" name="paper" value="Paper"/>
<input type="submit" name="rock" value="Rock"/>
</form>
3 changes: 3 additions & 0 deletions views/result.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%=$game.player1.name%> chose <%=$game.player1.choice%>. <p></p>
<%=$game.player2.name%> chose <%=$game.player2.choice%>. <p></p>
<%=@result%>
7 changes: 7 additions & 0 deletions views/round2.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%=$game.player2.name%>'s turn: please make your choice. <p> </p>

<form action="/player_2_choice" method="post">
<input type="submit" name="scissors" value="Scissors"/>
<input type="submit" name="paper" value="Paper"/>
<input type="submit" name="rock" value="Rock"/>
</form>