-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Tom O'Neill rps challenge #2120
base: main
Are you sure you want to change the base?
Changes from 1 commit
8229ced
c0bab7c
e79a111
cba1f2a
aaa4829
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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-* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'sinatra/base' | ||
require 'sinatra/reloader' | ||
require './lib/player.rb' | ||
require './lib/computer.rb' | ||
require './lib/game.rb' | ||
|
||
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 | ||
$game.calculate_result(params[:choice],$computer.random_move) | ||
redirect '/results' | ||
end | ||
|
||
get '/results' do | ||
@result = $game.result | ||
@player_1_name = $player_1.name | ||
erb :results | ||
end | ||
|
||
run! if app_file == $0 | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require './app' | ||
run Rps |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Computer | ||
|
||
attr_reader :name | ||
|
||
ALLOWED_MOVES = ["rock", "paper", "scissors"] | ||
|
||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
def random_move | ||
ALLOWED_MOVES.sample | ||
end | ||
|
||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Game | ||
|
||
attr_reader :result | ||
|
||
ALLOWED_MOVES = ["rock", "paper", "scissors"] | ||
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, | ||
} | ||
|
||
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. Ha much cleaner than mine. I saw some smart ones on codewars I was meaning to implement. |
||
def initialize | ||
@result = 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' if !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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Player | ||
|
||
attr_reader :name | ||
|
||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
end |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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 | ||
|
||
end |
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 |
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 |
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 | ||
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. All tests are comprehensive, v nice |
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<h1>ROCK PAPER SCISSORS</h1> | ||
|
||
<h2>Please enter your name(s):</h2> | ||
|
||
<form action="/begin_single_player" method="post"> | ||
Player 1: <input type="text" name="player_1" /><br> | ||
<br> | ||
    <input type="submit" value="Single Player Mode"> | ||
</form> | ||
|
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.