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

Gawain #2112

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Gawain #2112

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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source 'https://rubygems.org'
ruby '3.0.2'

gem 'sinatra'
gem 'sinatra-contrib'

group :test do
gem 'capybara'
Expand Down
8 changes: 8 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,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,6 +100,7 @@ DEPENDENCIES
simplecov
simplecov-console
sinatra
sinatra-contrib

RUBY VERSION
ruby 3.0.2p107
Expand Down
88 changes: 37 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
# RPS Challenge

Instructions
-------
Notes for review Monday morning. I have got to the stage where I have a working model, but I am struggling with the view and controller to interact with it. Any help glady recieved!

* 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
This is a weekend challenge for the end of week 3 of the 12 week bootcamp I am doing with [Makers Academy](https://www.makers.tech/?utm_source=adwords&utm_medium=ppc&utm_campaign=B2C%20Hybrid&utm_term=makers%20academy&hsa_acc=7172166340&hsa_cam=13568953605&hsa_grp=123027501759&hsa_ad=528554003929&hsa_src=g&hsa_tgt=aud-1330588356932%3Akwd-315575993965&hsa_kw=makers%20academy&hsa_mt=e&hsa_net=adwords&hsa_ver=3&gclid=EAIaIQobChMI65HU3dXR9wIViK3tCh032gfLEAAYASAAEgL3xfD_BwE). This week we have been learning how to make simple web apps using Ruby and Sinatra. Testing is being handled with Rspec and Capybara.

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:
This weeks task is to make a _Rock, Paper, Scissors_ game for them so they can play on the web with the following user stories:

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

As a marketeer
As a player
So that I can enjoy myself away from the daily grind
I would like to be able to play rock/paper/scissors
```
Expand All @@ -36,51 +26,47 @@ Hints on functionality
- 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.

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.

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:
## Instruction

```ruby
require 'simplecov'
require 'simplecov-console'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
# Want a nice code coverage website? Uncomment this next line!
# SimpleCov::Formatter::HTMLFormatter
])
SimpleCov.start
```mermaid
journey
title User Story
Enter Name: 5: User
Presented with a choice: 5: User
Choose Rock, Paper or Scissors: 5: User, Computer
Winner declared: 5
```

You can see your test coverage when you run your tests. If you want this in a graphical form, uncomment the `HTMLFormatter` line and see what happens!
## Class Diagram for model
<br>

```mermaid
classDiagram
direction RL
Game <.. Player
Game <.. Computer
class Game{
+String result
+play(Player1, Player2) result
-calculate_winner()
}
class Player{
+String : name
+String : move
+play() move
}
class Computer{
+String : move
+play() move
-calculate_move()
}
```
28 changes: 28 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

require 'sinatra/base'
require 'sinatra/reloader'
require_relative 'lib/player'

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

get '/' do
erb :index
end

post '/names' do
$player = Player.new(params[:player_name])

redirect "/play"
end

get '/play' do
@player = $player
@move = "You haven't played yet"
erb :play
end

run! if app_file == $0
end
5 changes: 5 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# in config.ru

require_relative "./app"

run RockPaperScissors
17 changes: 17 additions & 0 deletions lib/computer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Computer
attr_reader :move, :name

def initialize
@name = "Computer"
end

def play
@move = calculate_move
end

private

def calculate_move
[:rock, :paper, :scissors].sample
end
end
34 changes: 34 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Game
attr_reader :result

def play(player1, player2)
@result = calculate_winner(player1, player2)
end

private

def calculate_winner(player1, player2)
# "#{player1.name} wins!"
if player1.move == player2.move
return "It's a draw!"
elsif player1.move == :rock
if player2.move == :paper
return "#{player2.name} wins!"
elsif player2.move == :scissors
return"#{player1.name} wins!"
end
elsif player1.move == :paper
if player2.move == :rock
return "#{player2.name} wins!"
elsif player2.move == :scissors
return"#{player1.name} wins!"
end
else
if player2.move == :rock
return"#{player2.name} wins!"
elsif player2.move == :paper
return"#{player1.name} wins!"
end
end
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_reader :name, :move

def initialize(name)
@name = name
end

def play(move)
@move = move
end

end
33 changes: 33 additions & 0 deletions spec/features/feature_test_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "capybara/rspec"
require_relative "../../app"

Capybara.app = RockPaperScissors

feature 'Feature tests' do
scenario 'submitting name' do
visit('/')
fill_in :player_name, with: 'Gawain'
click_button 'Submit'
# save_and_open_page
expect(page).to have_content "Gawain"
end

scenario 'there is a choice of rock, paper, scissors' do
visit('/')
fill_in :player_name, with: 'Gawain'
click_button 'Submit'
# save_and_open_page
find_by_id("Rock").click
find_by_id("Paper").click
find_by_id("Scissors").click
end

scenario 'user chooses rock' do
visit('/')
fill_in :player_name, with: 'Gawain'
click_button 'Submit'
# save_and_open_page
find_by_id("Rock").click
end

end
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
require 'simplecov'
require 'simplecov-console'

# 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 all the testing gems
require 'capybara'
require 'capybara/rspec'
require 'rspec'

# Tell Capybara to talk to BookmarkManager
Capybara.app = RockPaperScissors

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

describe Computer do
subject(:computer) { Computer.new}

describe '#name' do
it 'returns name' do
expect(computer.name).to eq "Computer"
end
end

describe '#play' do
it 'returns move' do
computer.play
expect([:rock, :paper, :scissors]).to include computer.move
end
end
end
33 changes: 33 additions & 0 deletions spec/unit/game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'game'

describe Game do

it 'rock beats scissors' do
computer = instance_double('Computer', name: 'Computer', move: :rock)
user = instance_double('Player', name: "Gawain", move: :scissors)
subject.play(computer, user)
expect(subject.result).to eq "Computer wins!"
end

it 'scissors beats paper' do
computer = instance_double('Computer', name: 'Computer', move: :scissors)
user = instance_double('Player', name: "Gawain", move: :paper)
subject.play(computer, user)
expect(subject.result).to eq "Computer wins!"
end

it 'paper beats rock' do
computer = instance_double('Computer', name: 'Computer', move: :rock)
user = instance_double('Player', name: "Gawain", move: :paper)
subject.play(computer, user)
expect(subject.result).to eq "Gawain wins!"
end

it 'rock v rock is a draw' do
computer = instance_double('Computer', name: 'Computer', move: :rock)
user = instance_double('Player', name: "Gawain", move: :rock)
subject.play(computer, user)
expect(subject.result).to eq "It's a draw!"
end

end
19 changes: 19 additions & 0 deletions spec/unit/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'player'

describe Player do
subject(:gawain) { Player.new('Gawain')}

describe '#name' do
it 'returns name' do
expect(gawain.name).to eq "Gawain"
end
end

describe '#move' do
it 'returns move' do
move = 'rock'
gawain.play(move)
expect(gawain.move).to eq move
end
end
end
6 changes: 6 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Welcome to Rock, Paper, Scissors.</h1>
<form action="/names" method="post">
<label for="player_name">Please enter your name</label>
<input type="text" name="player_name">
<input type="submit" value="Submit">
</form>