diff --git a/app.rb b/app.rb index 4abb71ec..5b71f375 100644 --- a/app.rb +++ b/app.rb @@ -1,8 +1,57 @@ require 'sinatra/base' + class RockPaperScissors < Sinatra::Base + + get '/' do + erb(:index) + end + get '/test' do 'test page' end + post '/game' do + @name = params[:name] + erb(:game) + end + + post '/result' do + @user_choice = params[:choice] + @opponent_choice = [:rock, :paper, :scissors].sample + @result = pick_winner(@user_choice, @opponent_choice) + erb(:result) + end + + def pick_winner(player, opponent) + case player + when "rock" + if(opponent == :scissors) + return "You win!" + elsif(opponent == :rock) + return "It's a tie!" + else + return "You lose :(" + end + when "paper" + if(opponent == :rock) + return "You win!" + elsif(opponent == :paper) + return "It's a tie!" + else + return "You lose :(" + end + when "scissors" + if(opponent == :paper) + return "You win!" + elsif(opponent == :scissors) + return "It's a tie!" + else + return "You lose :(" + end + else + return "Invalid input." + end + end + run! if app_file == $0 -end +end \ No newline at end of file diff --git a/spec/features/game_page_spec.rb b/spec/features/game_page_spec.rb new file mode 100644 index 00000000..677e80db --- /dev/null +++ b/spec/features/game_page_spec.rb @@ -0,0 +1,10 @@ +feature 'Playing RPS' do + scenario "When I select 'Rock', I will lose if the machine selects 'Paper'" do + allow_any_instance_of(@opponent_choice).to receive(:sample).and_return(:paper) + visit '/' + fill_in('name', with: 'Jordan') + click_button('Submit') + click_button('Rock') + expect(page).to have_content "You lose :(" + end +end \ No newline at end of file diff --git a/spec/features/name_page_spec.rb b/spec/features/name_page_spec.rb new file mode 100644 index 00000000..a4c59200 --- /dev/null +++ b/spec/features/name_page_spec.rb @@ -0,0 +1,8 @@ +feature 'Entering username' do + scenario "When I enter Jordan, the next page will include my name" do + visit '/' + fill_in('name', with: 'Jordan') + click_button('Submit') + expect(page).to have_content "Hello Jordan. Choose one of the options below to play!" + end +end \ No newline at end of file diff --git a/views/game.erb b/views/game.erb new file mode 100644 index 00000000..28e4bc79 --- /dev/null +++ b/views/game.erb @@ -0,0 +1,8 @@ +