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

Jennie RPS Challenge solution so far #2131

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
source 'https://rubygems.org'

ruby '3.0.2'
#ruby '3.0.2'

gem 'sinatra'
gem 'sinatra-contrib'

group :test do
gem 'capybara'
Expand Down
13 changes: 9 additions & 4 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,6 +77,12 @@ 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)
Expand All @@ -93,9 +100,7 @@ DEPENDENCIES
simplecov
simplecov-console
sinatra

RUBY VERSION
ruby 3.0.2p107
sinatra-contrib

BUNDLED WITH
2.2.26
2.3.13
49 changes: 14 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
# RPS Challenge

Instructions
-------

* Feel free to use google, your notes, books, etc. but work on your own
* If you refer to the solution of another coach or student, please put a link to that in your README
* If you have a partial solution, **still check in a partial solution**
* You must submit a pull request to this repo with your code by 9am Monday morning

Task
----

Knowing how to build web applications is getting us almost there as web developers!

The Makers Academy Marketing Array ( **MAMA** ) have asked us to provide a game for them. Their daily grind is pretty tough and they need time to steam a little.

Your task is to provide a _Rock, Paper, Scissors_ game for them so they can play on the web with the following user stories:
The task was to provide a _Rock, Paper, Scissors_ game for them so they can play on the web with the following user stories:

```
As a marketeer
1) As a marketeer
So that I can see my name in lights
I would like to register my name before playing an online game

As a marketeer
2) As a marketeer
So that I can enjoy myself away from the daily grind
I would like to be able to play rock/paper/scissors
```
Expand All @@ -35,41 +27,28 @@ Hints on functionality
- the game will choose a random option
- a winner will be declared


As usual please start by

* Forking this repo
* TEST driving development of your app

[You may find this guide to setting up a new Ruby web project helpful.](https://github.com/makersacademy/course/blob/main/pills/ruby_web_project_setup_list.md)

## Bonus level 1: Multiplayer

Change the game so that two marketeers can play against each other ( _yes there are two of them_ ).

## Bonus level 2: Rock, Paper, Scissors, Spock, Lizard

Use the _special_ rules ( _you can find them here http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock_ )

## Basic Rules

- Rock beats Scissors
- Scissors beats Paper
- Paper beats Rock

In code review we'll be hoping to see:

* All tests passing
* High [Test coverage](https://github.com/makersacademy/course/blob/main/pills/test_coverage.md) (>95% is good)
* The code is elegant: every class has a clear responsibility, methods are short etc.
## Progress so far

Reviewers will potentially be using this [code review rubric](docs/review.md). Referring to this rubric in advance may make the challenge somewhat easier. You should be the judge of how much challenge you want this at this moment.
User Story 1) is complete. The user is greeted at the start of the game and can submit their name. When they've submitted their name, they are invited to play with a "Let's play <name>" message.

Progress made on User Story 2). The player submits their choice for the game - Rock, Paper or Scissors. The computer's choice is randomly generated and shown on the screen.

The game logic is built using if/elsif statements. I know it would be better to do this using a hash - I need to work on updating the code to use this instead.

I need to build the feature tests for declaring the winner and finish the game.

All tests currently passing.

Notes on test coverage
----------------------

Please ensure you have the following **AT THE TOP** of your spec_helper.rb in order to have test coverage stats generated
on your pull request:
Spec_helper.rb includes the following in order to have test coverage stats generated:

```ruby
require 'simplecov'
Expand Down
40 changes: 40 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'sinatra/base'
require 'sinatra/reloader'
require_relative 'lib/computer_choice'
require_relative 'lib/game'

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

enable :sessions

get '/' do
erb :index
end

post '/name' do
session[:player_name] = params[:player_name]
redirect '/play'
end

get '/play' do
@player_name = session[:player_name]
erb :play
end

post '/choice' do
session[:player_choice] = params[:player_choice]
redirect '/show_choice'
end

get '/show_choice' do
@player_choice = session[:player_choice]
@computer_choice = ComputerChoice.new.get_choice
erb :choice
end

run! if app_file == $0
end

Choose a reason for hiding this comment

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

App file is nice and understandable !

3 changes: 3 additions & 0 deletions capybara-202206051331115181734402.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Let's play Joe
</h1>

4 changes: 4 additions & 0 deletions capybara-202206051334496713312562.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1 style="font-family: Tahoma" >
Let's play Joe
</h1>

Choose a reason for hiding this comment

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

good change of font

4 changes: 4 additions & 0 deletions capybara-202206051354056527606077.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1 style="font-family: Tahoma" >
Let's play
</h1>

3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative "./app"

run RockPaper
10 changes: 10 additions & 0 deletions lib/computer_choice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ComputerChoice
def initialize
@choices = ["Rock", "Paper", "Scissors"]

Choose a reason for hiding this comment

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

put @choice object out of initialiser method

end

def get_choice
@choices.sample
end
end

24 changes: 24 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Game
def initialize(player_choice, computer_choice)
@player_choice = player_choice
@computer_choice = computer_choice
end

def game_result
if @player_choice == "Rock" && @computer_choice == "Scissors"
return "Win"
elsif @player_choice == "Rock" && @computer_choice == "Paper"

Choose a reason for hiding this comment

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

Lots of if statements , nice !

return "Lose"
elsif @player_choice == "Paper" && @computer_choice == "Rock"
return "Win"
elsif @player_choice == "Paper" && @computer_choice == "Scissors"
return "Lose"
elsif @player_choice == "Scissors" && @computer_choice == "Paper"
return "Win"
elsif @player_choice == "Scissors" && @computer_choice == "Rock"
return "Lose"
else

Choose a reason for hiding this comment

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

possibly could do one line if statements , i.e
if @player_choice == "Rock" && @computer_choice == "Scissors" || @player_choice == "Paper" && @computer_choice == "Rock"
return "Win"

return "Draw"
end
end
end
9 changes: 9 additions & 0 deletions spec/features/computer_choice_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
feature 'Show computer choice' do
scenario 'computer makes a random choice of rock, paper or scissors' do
allow_any_instance_of(Array).to receive(:get_choice).and_return('Scissors')
visit('/show_choice')
expect(page).to have_content("Computer choice: Scissors")
end
end

# is not working with the or options for random sample
12 changes: 12 additions & 0 deletions spec/features/enter_name_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
feature 'Enter player name' do
scenario 'submit name' do
visit('/')
fill_in :player_name, with: "Joe"
click_button "Submit"

#save_and_open_page

expect(page).to have_content "Let's play Joe"
end
end

8 changes: 8 additions & 0 deletions spec/features/rps_choice_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
feature 'Enter player choice' do
scenario 'submit rock, paper or scissors' do
visit('/play')
fill_in :player_choice, with: "Rock"
click_button "Submit"
expect(page).to have_content "Your choice: Rock"
end
end
Empty file.
6 changes: 6 additions & 0 deletions spec/features/test_infrastructure_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# feature 'Testing infrastructure' do
# scenario 'Can run app and check page content' do
# visit('/')
# expect(page).to have_content 'Testing infrastructure working!'
# end
# end
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# Set the environment to "test"
ENV['RACK_ENV'] = 'test'

# Bring in the contents of the `app.rb` file. The below is equivalent to: require_relative '../app.rb'
require File.join(File.dirname(__FILE__), '..', 'app.rb')

require 'capybara'
require 'capybara/rspec'
require 'rspec'
require 'simplecov'
require 'simplecov-console'

Capybara.app = RockPaper

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
# Want a nice code coverage website? Uncomment this next line!
Expand Down
24 changes: 24 additions & 0 deletions spec/units/computer_choice_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'computer_choice'

RSpec.describe ComputerChoice do
context "give computer's choice for the game" do
it "returns Rock as a string" do
allow_any_instance_of(ComputerChoice).to receive(:get_choice).and_return("Rock")

expect(ComputerChoice.new.get_choice).to eq "Rock"
end

it "returns Paper as a string" do
allow_any_instance_of(ComputerChoice).to receive(:get_choice).and_return("Paper")

expect(ComputerChoice.new.get_choice).to eq "Paper"
end

it "returns Scissors as a string" do
allow_any_instance_of(ComputerChoice).to receive(:get_choice).and_return("Scissors")

expect(ComputerChoice.new.get_choice).to eq "Scissors"
end
end
end

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

RSpec.describe Game do
context "when the player chooses rock, and the computer choice is paper" do
it "shows computer as the winner" do
game = Game.new("Rock", "Paper")
expect(game.game_result).to eq "Lose"
end
end

context "when the player chooses rock, and the computer choice is scissors" do
it "shows player as the winner" do
game = Game.new("Rock", "Scissors")
expect(game.game_result).to eq "Win"
end
end

context "when the player chooses paper, and the computer choice is scissors" do
it "shows computer as the winner" do
game = Game.new("Paper", "Scissors")
expect(game.game_result).to eq "Lose"
end
end

context "when the player chooses paper, and the computer choice is rock" do
it "shows player as the winner" do
game = Game.new("Paper", "Rock")
expect(game.game_result).to eq "Win"
end
end

context "when the player chooses scissors, and the computer choice is rock" do
it "shows computer as the winner" do
game = Game.new("Scissors", "Rock")
expect(game.game_result).to eq "Lose"
end
end

context "when the player chooses scissors, and the computer choice is paper" do
it "shows player as the winner" do
game = Game.new("Scissors", "Paper")
expect(game.game_result).to eq "Win"
end
end

context "when the player chooses rock, and the computer choice is rock" do
it "is a draw" do
game = Game.new("Scissors", "Scissors")
expect(game.game_result).to eq "Draw"
end
end
end



Choose a reason for hiding this comment

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

very precise code !

7 changes: 7 additions & 0 deletions views/choice.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h2 style="font-family: Tahoma" >
Your choice: <%= @player_choice %><br>

Computer choice: <%= @computer_choice %><br>


</h2>
11 changes: 11 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1 style="font-family: Tahoma">
Welcome to Rock, Paper, Scissors!
</h1>
<h2 style="font-family: Tahoma">
Ready to play?
Enter your name below
</h2>
<form action="/name" method="post">
<input type="text" name="player_name">
<input type="submit" value="Submit">
</form>
12 changes: 12 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h1 style="font-family: Tahoma" >
Let's play <%= @player_name %>
</h1>

<h2 style="font-family: Tahoma">
Make your choice, rock, paper or scissors?
Enter below.
</h2>
<form action="/choice" method="post">
<input type="text" name="player_choice">
<input type="submit" value="Submit">
</form>
Empty file added views/you_chose.erb
Empty file.