-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Tom O'Neill rps challenge #2120
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
tomoneill32
wants to merge
5
commits into
makersacademy:main
Choose a base branch
from
tomoneill32:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8229ced
First commit
tomoneill32 c0bab7c
Basic game framework complete
tomoneill32 e79a111
Replay button added, more information in the results screen, some ref…
tomoneill32 cba1f2a
Readme updated
tomoneill32 aaa4829
Readme updated again
tomoneill32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| /**/.DS_Store | ||
| /coverage | ||
| Gemfile.lock | ||
|
|
||
| # Local cache of Rubocop remote config | ||
| .rubocop-* | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| require 'sinatra/base' | ||
| require 'sinatra/reloader' | ||
| require './lib/player' | ||
| require './lib/computer' | ||
| require './lib/game' | ||
|
|
||
| class Rps < Sinatra::Base | ||
| enable :sessions | ||
|
|
||
| configure :development do | ||
| register Sinatra::Reloader | ||
| end | ||
|
|
||
| get '/' do | ||
| erb :home | ||
| end | ||
|
|
||
| post '/begin_single_player' do | ||
| p params | ||
| $player_1 = Player.new(params[:player_1]) | ||
| $computer = Computer.new("Dwayne") | ||
| redirect '/single_player_game' | ||
| end | ||
|
|
||
| get '/single_player_game' do | ||
| @player_1_name = $player_1.name | ||
| @computer_name = $computer.name | ||
| erb :single_player | ||
| end | ||
|
|
||
| post '/calculate_results' do | ||
| $game = Game.new | ||
| $player_1.choose(params[:choice]) | ||
| $computer.choose | ||
| $game.calculate_result($player_1.choice,$computer.choice) | ||
| redirect '/results' | ||
| end | ||
|
|
||
| get '/results' do | ||
| @result = $game.result | ||
| @player_1_name = $player_1.name | ||
| @computer_name = $computer.name | ||
| @player_1_choice = $player_1.choice | ||
| @computer_choice = $computer.choice | ||
| erb :results | ||
| end | ||
|
|
||
| run! if app_file == $0 | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| require './app' | ||
| run Rps |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Computer | ||
|
|
||
| attr_reader :name, :choice | ||
|
|
||
| ALLOWED_MOVES = ["rock", "paper", "scissors"] | ||
|
|
||
| def initialize(name) | ||
| @name = name | ||
| @choice = nil | ||
| end | ||
|
|
||
| def choose | ||
| @choice = ALLOWED_MOVES.sample | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| class Game | ||
|
|
||
| attr_reader :result | ||
|
|
||
| ALLOWED_MOVES = ["rock", "paper", "scissors"] | ||
|
|
||
| def initialize | ||
| @result = nil | ||
| @outcomes = { | ||
| "rock rock" => nil, | ||
| "rock scissors" => true, | ||
| "rock paper" => false, | ||
| "scissors rock" => false, | ||
| "scissors scissors" => nil, | ||
| "scissors paper" => true, | ||
| "paper rock" => true, | ||
| "paper scissors" => false, | ||
| "paper paper" => nil, | ||
| } | ||
| end | ||
|
|
||
| def calculate_result(move1,move2) | ||
| check_move_allowed(move1) | ||
| check_move_allowed(move2) | ||
| @result = output_winner(move1,move2) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def check_move_allowed(move) | ||
| fail 'Incorrect move' unless ALLOWED_MOVES.include?(move) | ||
| end | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So have you implemented this in such a way as it requires the player enters another choice? |
||
| def output_winner(move1,move2) | ||
| @outcomes["#{move1} #{move2}"] | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class Player | ||
|
|
||
| attr_reader :name, :choice | ||
|
|
||
| def initialize(name) | ||
| @name = name | ||
| @choice = nil | ||
| end | ||
|
|
||
| def choose(choice) | ||
| @choice = choice | ||
| end | ||
|
|
||
| end |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| feature "Going to the homepage, entering a player name" do | ||
|
|
||
| scenario "the home page returns a successful status code" do | ||
| visit("/") | ||
| expect(page.status_code).to eq(200) | ||
| end | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh nice to see an example of a test for this |
||
| scenario "page contains 'Please enter your name(s)'" do | ||
| visit("/") | ||
| expect(page).to have_content('Please enter your name(s)') | ||
| end | ||
|
|
||
| scenario "Page contains an option for single player mode" do | ||
| visit("/") | ||
| expect(page).to have_selector(:link_or_button, 'Single Player Mode') | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| feature "Game outcome screen" do | ||
|
|
||
| scenario "When the player picks rock, they are taken to a results page" do | ||
| sign_in_and_play_1p | ||
| click_on 'Rock' | ||
| expect(page).to have_content('The results are in') | ||
| end | ||
|
|
||
| scenario "The player picks rock, the computer picks scissors, the player wins" do | ||
| srand(3) | ||
| sign_in_and_play_1p | ||
| click_on 'Rock' | ||
| expect(page).to have_content('Mario wins') | ||
| end | ||
|
|
||
| scenario "The player picks scissors, the computer picks scissors, it's a draw" do | ||
| srand(3) | ||
| sign_in_and_play_1p | ||
| click_on 'Scissors' | ||
| expect(page).to have_content('draw') | ||
| end | ||
|
|
||
| scenario "The player picks paper, the computer picks scissors, the player loses" do | ||
| srand(3) | ||
| sign_in_and_play_1p | ||
| click_on 'Paper' | ||
| expect(page).to have_content('Mario loses') | ||
| end | ||
|
|
||
| scenario "There is an option to play again at the end of the game" do | ||
| sign_in_and_play_1p | ||
| click_on 'Rock' | ||
| expect(page).to have_selector(:link_or_button,'Play again?') | ||
| end | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| feature "Playing the game" do | ||
|
|
||
| scenario "When player names have been entered, they appear in the game" do | ||
| sign_in_and_play_1p | ||
| expect(page).to have_content("Mario vs. Dwayne") | ||
| end | ||
|
|
||
| scenario "Player can choose rock" do | ||
| sign_in_and_play_1p | ||
| expect(page).to have_selector(:link_or_button, 'Rock') | ||
| end | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| require 'computer' | ||
|
|
||
| describe Computer do | ||
| subject(:ai) { Player.new('HAL 9000') } | ||
|
|
||
| describe '#name' do | ||
| it 'returns the name' do | ||
| expect(ai.name).to eq 'HAL 9000' | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| require 'game' | ||
|
|
||
| describe Game do | ||
| subject(:game) { Game.new } | ||
|
|
||
| describe '#calculate_result' do | ||
| it 'responds to two arguments' do | ||
| expect(game).to respond_to(:calculate_result).with(2) | ||
| end | ||
|
|
||
| it 'fails if the first move is not rock, paper or scissors' do | ||
| expect { game.calculate_result('elephant','rock') }.to raise_error 'Incorrect move' | ||
| end | ||
|
|
||
| it 'fails if the first move is not rock, paper or scissors' do | ||
| expect { game.calculate_result('rock','octopus') }.to raise_error 'Incorrect move' | ||
| end | ||
|
|
||
| it 'outputs true for a winning game' do | ||
| expect(game.calculate_result('rock','scissors')).to eq true | ||
| expect(game.calculate_result('paper','rock')).to eq true | ||
| expect(game.calculate_result('scissors','paper')).to eq true | ||
| end | ||
|
|
||
| it 'outputs nil for a drawn game' do | ||
| expect(game.calculate_result('rock','rock')).to eq nil | ||
| expect(game.calculate_result('scissors','scissors')).to eq nil | ||
| expect(game.calculate_result('paper','paper')).to eq nil | ||
| end | ||
|
|
||
| it 'outputs false for a losing game' do | ||
| expect(game.calculate_result('rock','paper')).to eq false | ||
| expect(game.calculate_result('scissors','rock')).to eq false | ||
| expect(game.calculate_result('paper','scissors')).to eq false | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| require 'player' | ||
|
|
||
| describe Player do | ||
| subject(:mario) { Player.new('Mario') } | ||
|
|
||
| describe '#name' do | ||
| it 'returns the name' do | ||
| expect(mario.name).to eq 'Mario' | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| def sign_in_and_play_1p | ||
| visit("/") | ||
| fill_in 'player_1', with: 'Mario' | ||
| click_on 'Single Player Mode' | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these .rb's are so CLEAN, so TIGHT, every function has a clear and distinct role. V tasty.