diff --git a/Design b/Design new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Gemfile b/Gemfile index 63415039a7..f7fea9a210 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,8 @@ source 'https://rubygems.org' ruby '3.0.2' gem 'sinatra' +gem 'sinatra-contrib' +gem 'webrick' group :test do gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index 5afab6d697..2aa400aceb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -93,6 +101,8 @@ DEPENDENCIES simplecov simplecov-console sinatra + sinatra-contrib + webrick RUBY VERSION ruby 3.0.2p107 diff --git a/app.rb b/app.rb new file mode 100644 index 0000000000..37ce408b58 --- /dev/null +++ b/app.rb @@ -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 + @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 diff --git a/config.ru b/config.ru new file mode 100644 index 0000000000..5c2d500c02 --- /dev/null +++ b/config.ru @@ -0,0 +1,2 @@ +require './app' +run RockPaperScissors diff --git a/lib/game.rb b/lib/game.rb new file mode 100644 index 0000000000..58caed9462 --- /dev/null +++ b/lib/game.rb @@ -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 + 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 diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 0000000000..d090499819 --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,12 @@ +class Player + attr_accessor :name, :choice + + def initialize(name) + @name = name + end + + def add(choice) + @choice = choice + end + +end diff --git a/spec/features/choose_spec.rb b/spec/features/choose_spec.rb new file mode 100644 index 0000000000..1d061f0f9b --- /dev/null +++ b/spec/features/choose_spec.rb @@ -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 + 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 + \ No newline at end of file diff --git a/spec/features/name_form_spec.rb b/spec/features/name_form_spec.rb new file mode 100644 index 0000000000..ab9797d20e --- /dev/null +++ b/spec/features/name_form_spec.rb @@ -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 diff --git a/spec/features/result_spec.rb b/spec/features/result_spec.rb new file mode 100644 index 0000000000..9b131e6675 --- /dev/null +++ b/spec/features/result_spec.rb @@ -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 + 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 diff --git a/spec/features/testing_infrastructure_spec.rb b/spec/features/testing_infrastructure_spec.rb new file mode 100644 index 0000000000..81a1630024 --- /dev/null +++ b/spec/features/testing_infrastructure_spec.rb @@ -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 diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb new file mode 100644 index 0000000000..b20c84d27c --- /dev/null +++ b/spec/features/web_helpers.rb @@ -0,0 +1,6 @@ +def sign_in_and_play + visit ("/") + fill_in "player1", with: "Annabelle" + fill_in "player2", with: "Sandy" + click_on "Submit" +end diff --git a/spec/game_spec.rb b/spec/game_spec.rb new file mode 100644 index 0000000000..2357ea01ff --- /dev/null +++ b/spec/game_spec.rb @@ -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 diff --git a/spec/player_spec.rb b/spec/player_spec.rb new file mode 100644 index 0000000000..adfd5c701c --- /dev/null +++ b/spec/player_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 11a50a94e5..d53931e4f6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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' diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000000..5c1e08f9c3 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,13 @@ +
+ diff --git a/views/play.erb b/views/play.erb new file mode 100644 index 0000000000..b3a6d57482 --- /dev/null +++ b/views/play.erb @@ -0,0 +1,9 @@ +Welcome <%=@player1.name%> and <%=@player2.name%>. + +<%=@player1.name%>'s turn: please make your choice. + + diff --git a/views/result.erb b/views/result.erb new file mode 100644 index 0000000000..b427d55262 --- /dev/null +++ b/views/result.erb @@ -0,0 +1,3 @@ +<%=$game.player1.name%> chose <%=$game.player1.choice%>. +<%=$game.player2.name%> chose <%=$game.player2.choice%>. +<%=@result%> \ No newline at end of file diff --git a/views/round2.erb b/views/round2.erb new file mode 100644 index 0000000000..d73abcdde2 --- /dev/null +++ b/views/round2.erb @@ -0,0 +1,7 @@ +<%=$game.player2.name%>'s turn: please make your choice.
+ +