Skip to content

rps challenge #1036

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

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
98ba70a
player can add name
m-rcd Sep 21, 2018
28342c3
add options
m-rcd Sep 21, 2018
c162337
add rps game class
m-rcd Sep 22, 2018
1c3ecf5
add player class
m-rcd Sep 22, 2018
a813fb4
add computer class
m-rcd Sep 22, 2018
1a19870
add result page with game result
m-rcd Sep 22, 2018
8a613a9
add web helpers
m-rcd Sep 22, 2018
28ae181
fix play method
m-rcd Sep 22, 2018
0030db3
add player2
m-rcd Sep 22, 2018
777d893
can play solo or multiplayer
m-rcd Sep 22, 2018
4f919da
can start new game when game ends
m-rcd Sep 22, 2018
0814d39
refactor tests
m-rcd Sep 22, 2018
dd799ed
refactor
m-rcd Sep 22, 2018
45cc6c3
add css
m-rcd Sep 22, 2018
8831ed0
fix css
m-rcd Sep 23, 2018
548501d
prevent empty fields
m-rcd Sep 23, 2018
53495db
change pic
m-rcd Sep 23, 2018
76f8942
add README
m-rcd Sep 23, 2018
9bbf44e
ready for deployment
m-rcd Sep 23, 2018
825f8ca
add link to README
m-rcd Sep 23, 2018
abfdf87
capitalize player name
m-rcd Sep 23, 2018
5944dd1
fix name capitalize
m-rcd Sep 23, 2018
ee7f159
add computer choice when game ends
m-rcd Sep 23, 2018
91b2898
refactor
m-rcd Sep 23, 2018
408a829
fix 1 player display bug
m-rcd Sep 23, 2018
0576887
fix message when computer wins
m-rcd Sep 23, 2018
a4b50c1
fix test
m-rcd Sep 23, 2018
96d350c
remove unnecessary code
m-rcd Sep 24, 2018
fc33469
remove computer class and refactor controller to one path
m-rcd Sep 24, 2018
e85290f
refactor based on reviews
m-rcd Sep 25, 2018
dfd35ec
fix bug
m-rcd Sep 25, 2018
b9cf451
remove index.php
m-rcd Sep 27, 2018
c998c3c
Update readme
m-rcd Nov 26, 2018
0e4b2f0
Fix titles in README
m-rcd Nov 26, 2018
1d53bed
Refacto
m-rcd Jan 17, 2019
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
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 '2.5.0'

gem 'rake'
gem 'shotgun'
gem 'sinatra'

group :test do
Expand Down
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ GEM
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.9.0)
shotgun (0.9.2)
rack (>= 1.0)
simplecov (0.15.1)
docile (~> 1.1.0)
json (>= 1.8, < 3)
Expand Down Expand Up @@ -81,6 +83,7 @@ DEPENDENCIES
rake
rspec
rubocop (= 0.56.0)
shotgun
simplecov
simplecov-console
sinatra
Expand All @@ -89,4 +92,4 @@ RUBY VERSION
ruby 2.5.0p0

BUNDLED WITH
1.16.1
1.16.2
121 changes: 69 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
# RPS Challenge

Instructions
-------
[User Stories](#user-stories) | [Game Rules](#rules) | [Technologies](#technologies) | [User Interaction and Interaction](#experience) | [Running the App](#running-app) | [Running tests](#tests) | [Approach](#approach) | [Contributing](#contributing)

* Challenge time: rest of the day and weekend, until Monday 9am
* 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
<a name="user-stories">User stories</a>
----

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:

```sh
As a marketeer
So that I can see my name in lights
Expand All @@ -28,59 +15,89 @@ So that I can enjoy myself away from the daily grind
I would like to be able to play rock/paper/scissors
```

Hints on functionality
<a name="rules">Game Rules</a>
---

- the marketeer should be able to enter their name before the game
- the marketeer will be presented the choices (rock, paper and scissors)
- the marketeer can choose one option
- the game will choose a random option
- a winner will be declared
- Rock beats Scissors
- Scissors beats Paper
- Paper beats Rock
- Lizard beats Paper
- Lizard beats Spock
- Spock beats Rock
- Spock beats Scissors
- Rock beats Lizard
- Scissors beats Lizard
- Paper beats Spock

<a name="technologies"> Technologies</a>
---

As usual please start by
- Ruby
- RSpec
- Capybara
- CSS

* Forking this repo
* TEST driving development of your app
<a name="experience"> User Interaction and Experience</a>
---

![1](./assets/1.png)

## Bonus level 1: Multiplayer
![2](./assets/2.png)

Change the game so that two marketeers can play against each other ( _yes there are two of them_ ).
![3](./assets/3.png)

## Bonus level 2: Rock, Paper, Scissors, Spock, Lizard
<a name="running-app"> Running the app</a>
---

Use the _special_ rules ( _you can find them here http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock_ )
- Clone this repository
```
$ git clone https://github.com/m-rcd/rps-challenge
$ cd rps-challenge
```
- Install dependencies:
```
$ gem install
```
- Run the app
```
$ rackup
```
- In your browser, go to
```
localhost:9292
```

## Basic Rules
<a name="tests">Running the tests</a>
---
- Run
```
$ rspec
```

- Rock beats Scissors
- Scissors beats Paper
- Paper beats Rock
![test](./assets/test.png)

In code review we'll be hoping to see:
<a name="approach">Approach</a>
---

* All tests passing
* High [Test coverage](https://github.com/makersacademy/course/blob/master/pills/test_coverage.md) (>95% is good)
* The code is elegant: every class has a clear responsibility, methods are short etc.
- Using TDD, I started by creating a web app that allow the user to enter their name and choose rock, paper or scissors.
- I then created a file called game.rb which contained the game logic.
- I also added a player class to allow the user to have a name
- I added a computer class which choose an option randomly.
- I then added a result page which would declare the winner
- I added a button that allows the user to play again
- I added the option to play a multiplayer game: 2 players can add their names, each choose an option and then see the winner
- I then added 2 options to the game: lizard and Spock
- I added the option to start a new game after a game ends
- Having finished implementing its functionality ,I used css to make it look nice

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 weekend.
<a name="contributing"> Contributing</a>
---

Notes on test coverage
----------------------
Pull Requests are always welcome.

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:
When you edit the code, please run `rspec` to check all the tests pass. Also run `rubocop` before you git commit.

```ruby
require 'simplecov'
require 'simplecov-console'
Ensure the PR description clearly describes the problem and solution. It should include the relevant issue number, if applicable.

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
# Want a nice code coverage website? Uncomment this next line!
# SimpleCov::Formatter::HTMLFormatter
])
SimpleCov.start
```

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!
[Play now!](https://stark-journey-20582.herokuapp.com/)

Choose a reason for hiding this comment

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

Great that you got this hosted!

68 changes: 68 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'sinatra/base'
require './lib/game'
require './lib/player'

class RPS < Sinatra::Base
enable :sessions

get '/' do
erb :index
end

post '/game_mode' do
session[:game_mode] = params[:game_mode].to_sym
@gamemode = session[:game_mode]
erb :name_input
end

post '/name' do
player1 = Player.new(params[:player1])
if @gamemode == :singleplayer
player2 = Player.new('Skynet')
else
player2 = Player.new(params[:player2])
end
session[:game] = Game.new(player1, player2)
redirect '/play'
end

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

post '/optionp1' do
@gamemode = session[:game_mode]
session[:player1_choice] = params[:commit]
if @gamemode == :singleplayer
redirect '/result'
else
redirect '/playp2'
end
end

get '/playp2' do
@game = session[:game]
erb :optionp2
end

post '/optionp2' do
session[:player2_choice] = params[:option]
redirect '/result'
end

get '/result' do
@game = session[:game]
@gamemode = session[:game_mode]
@game.player1.choice(session[:player1_choice])
if @gamemode == :singleplayer
@game.player2.choice(@game.computer_choice)
else
@game.player2.choice(session[:player2_choice])
end
@result = @game.play
erb :result
end

run! if app_file == $0
end
Binary file added assets/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/lizard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/paper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/rock.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/scissors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require_relative "./app"
run RPS
27 changes: 27 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Game
attr_reader :player1, :result, :player2

def initialize(player1, player2)
@player1 = player1
@player2 = player2
@key_beats_value = { :Rock => [:Scissors, :Lizard], :Paper =>
[:Rock, :Spock], :Scissors => [:Paper, :Lizard],
:Spock => [:Scissors, :Rock], :Lizard => [:Spock, :Paper] }
end

def play
p1 = player1.player_choice.to_sym
p2 = player2.player_choice.to_sym
if @key_beats_value[p1].include?(p2)
'player1'
elsif @key_beats_value[p2].include?(p1)
'player2'
else
'draw'
end
end

def computer_choice
['Rock', 'Paper', 'Scissors', 'Spock', 'Lizard'].sample
end
end
11 changes: 11 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Player
attr_reader :name, :player_choice

def initialize(name)
@name = name
end

def choice(choice)
@player_choice = choice
end
end
75 changes: 75 additions & 0 deletions public/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
.button {
display: inline-block;
width: 120px;
background-color: #8A2BE2 ;
border: 2px solid white;
color: white;
text-decoration: none;
text-align: center;
border-radius: 4px;
margin-left: 5px;
margin-right: 5px;
margin-bottom: 20px;
padding-bottom: 2px;
padding-top: 2px;
font-size: 16px;
font-family: Times, Georgia, Serif;
cursor: pointer;
}

body {
margin-top: 100px;
background-color: black;
color: white;
font-family: "Courier New", Courier, monospace;
font-size: 20px;
}


body a {
color: white;
text-decoration: none;
font-family: "Courier New", Courier, monospace;
font-size: 65px;
text-align: center;
margin: 60px;
display: block;
}

h1 {
text-align: center;
font-size: 60px;

}

.im {
position: relative;
top: -140px;
right: -100px;
}


form {
text-align: center;
}

p {
text-align: center;
font-size: 70px;
}
h2 {
text-align: center;
font-size: 50px;
}

form input[type='text'] {
border: 2px solid #8A2BE2 ;
font-family: "Courier New", Courier, monospace;
font-size: 16px;
text-align: center;
width: 200px;
padding: 3px;
margin-top: 40px;
background-color: white;

}
Loading