-
Notifications
You must be signed in to change notification settings - Fork 250
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
Jon Clarke - RPS challenge #232
Open
JonClarke84
wants to merge
12
commits into
makersacademy:main
Choose a base branch
from
JonClarke84: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
12 commits
Select commit
Hold shift + click to select a range
4a8998a
sign in working
JonClarke84 90e5553
game testing passing
JonClarke84 0b79457
game tests passing
JonClarke84 9fdc02e
gets to final page
JonClarke84 f52fec3
all is working
JonClarke84 012cb19
added forgotten tests
JonClarke84 d37cef3
two players can enter names
JonClarke84 fb35f1f
Added Play vs AI button to index page
JonClarke84 43bc8d8
written test for starting the pvp game'
JonClarke84 a9ea987
pvp track enabled
JonClarke84 93d0c01
second player choice page appears, params not sorted yet
JonClarke84 59bf27a
finished for the week. need to sort params to encapsulate choices bef…
JonClarke84 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 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 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 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,8 +1,56 @@ | ||
require 'sinatra/base' | ||
require_relative 'lib/player' | ||
require_relative 'lib/game' | ||
|
||
class RockPaperScissors < Sinatra::Base | ||
enable :sessions | ||
|
||
get '/test' do | ||
'test page' | ||
end | ||
|
||
get '/' do | ||
erb :index | ||
end | ||
|
||
get '/names' do | ||
$player_one = Player.new(params[:player_one]) | ||
$player_two = Player.new(params[:player_two]) | ||
redirect to('/play') | ||
end | ||
|
||
get '/pvp-names' do | ||
erb :pvp_names | ||
end | ||
|
||
get '/vs-ai-name' do | ||
erb :vs_ai_name | ||
end | ||
|
||
get '/play' do | ||
@player_one = $player_one | ||
@player_two = $player_two | ||
if @player_two.name == 'computer' | ||
erb :play | ||
else | ||
erb :pvp_play | ||
end | ||
end | ||
|
||
get '/pvp-play-2' do | ||
$player_one.choose(params) | ||
@player_two = $player_two | ||
erb :pvp_play_two | ||
end | ||
|
||
# need to get this working with a second player's choices now | ||
get '/result' do | ||
@player_one = $player_one | ||
@player_one.choose(params) | ||
@game = Game.new(@player_one) | ||
@result = @game.run_vs_ai | ||
erb :result | ||
end | ||
|
||
run! if app_file == $0 | ||
end |
This file contains 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,27 @@ | ||
class Game | ||
attr_reader :player_one, :ai_choice | ||
|
||
def initialize(player_one) | ||
@player_one = player_one | ||
@ai_choice = ["rock", "paper", "scissors"].sample | ||
end | ||
|
||
def self.run_vs_ai(player_one) | ||
new(player_one).run_vs_ai | ||
end | ||
|
||
def run_vs_ai | ||
if @player_one.choice == @ai_choice | ||
return "It's a draw!" | ||
elsif @player_one.choice == "rock" && @ai_choice == "scissors" | ||
return "#{@player_one.name} wins!" | ||
elsif @player_one.choice == "paper" && @ai_choice == "rock" | ||
return "#{@player_one.name} wins!" | ||
elsif @player_one.choice == "scissors" && @ai_choice == "paper" | ||
return "#{@player_one.name} wins!" | ||
else | ||
return "You lose!" | ||
end | ||
end | ||
Comment on lines
+13
to
+25
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. You are mixing presentation (the sentence used) and logic (who wins) here. It would be better to have the model/this file just handle the logic, and let the view handle the presentation. |
||
|
||
end |
This file contains 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 @@ | ||
class Player | ||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
def choose(choice) | ||
choice.each { |_key, value| @choice = value } | ||
end | ||
|
||
attr_reader :name, :choice | ||
end |
This file contains 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,19 @@ | ||
feature 'choose to play vs AI or another player' do | ||
scenario 'the player chooses to play vs AI' do | ||
visit('/') | ||
click_button 'Play vs AI' | ||
fill_in 'player_one', with: 'Homer' | ||
click_button 'Submit' | ||
expect(page.html).to include("<h1>Homer, make your choice</h1>") | ||
end | ||
|
||
scenario 'the player chooses to play vs another player' do | ||
visit('/') | ||
click_button 'Player vs Player' | ||
fill_in 'player_one', with: 'Homer' | ||
fill_in 'player_one', with: 'Marge' | ||
click_button 'Submit' | ||
click_button 'rock' | ||
expect(page.html).to include("<h1>Marge, make your choice</h1>") | ||
end | ||
end |
This file contains 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 @@ | ||
feature 'play the game' do | ||
scenario 'both players can make a choice' do | ||
sign_in_and_play_vs_ai | ||
click_button 'rock' | ||
expect(page.html).to include("<h1>Game result</h1>") | ||
end | ||
|
||
xscenario 'the player can click a button to play again' do | ||
sign_in_and_play_vs_ai | ||
click_button 'rock' | ||
click_button 'Play again' | ||
expect(page.html).to include("<h1>Welcome to the game, Homer</h1>") | ||
end | ||
end |
This file contains 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,9 @@ | ||
feature 'adding player name vs ai' do | ||
scenario 'the player can submit their name' do | ||
visit('/') | ||
click_button 'Play vs AI' | ||
fill_in 'player_one', with: 'Homer' | ||
click_button 'Submit' | ||
expect(page.html).to include("<h1>Homer, make your choice</h1>") | ||
end | ||
end |
This file contains 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 @@ | ||
def sign_in_and_play_vs_ai | ||
visit('/') | ||
click_button 'Play vs AI' | ||
fill_in 'player_one', with: 'Homer' | ||
click_button 'Submit' | ||
end | ||
|
||
def randomise | ||
srand(1) | ||
srand(3) | ||
end |
This file contains 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,35 @@ | ||
require 'game' | ||
|
||
describe Game do | ||
let(:player_one) { double :player, choice: "rock", name: "Homer" } | ||
# let(:player_two) { double :player, choice: "scissors", name: "Marge" } | ||
|
||
describe '#make_ai_choice returns a random choice' do | ||
it 'returns a random choice' do | ||
expect(["rock", "paper", "scissors"]).to include(Game.new(player_one).ai_choice) | ||
end | ||
end | ||
|
||
describe 'player chooses rock, ai chooses scissors, ' do | ||
it 'player wins' do | ||
randomise | ||
expect(Game.run_vs_ai(player_one)).to eq "#{player_one.name} wins!" | ||
end | ||
end | ||
|
||
describe 'player chooses scissors, ai chooses scissors, ' do | ||
let(:player_one) { double :player, choice: "scissors" } | ||
it "it's a draw" do | ||
randomise | ||
expect(Game.run_vs_ai(player_one)).to eq "It's a draw!" | ||
end | ||
end | ||
|
||
describe 'player chooses paper, ai chooses scissors, ' do | ||
let(:player_one) { double :player, choice: "paper" } | ||
it "it's a draw" do | ||
randomise | ||
expect(Game.run_vs_ai(player_one)).to eq "You lose!" | ||
end | ||
end | ||
end |
This file contains 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,20 @@ | ||
require 'player' | ||
|
||
describe Player do | ||
|
||
describe '#name' do | ||
it 'returns the name' do | ||
player = Player.new('Homer') | ||
expect(player.name).to eq 'Homer' | ||
end | ||
end | ||
|
||
describe '#choose' do | ||
it 'returns the choice of the player' do | ||
player = Player.new('Homer') | ||
choice = { rock: 'rock' } | ||
player.choose(choice) | ||
expect(player.choice).to eq "rock" | ||
end | ||
end | ||
end |
This file contains 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 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,7 @@ | ||
<form action="/vs-ai-name"> | ||
<input type="submit" value="Play vs AI"> | ||
</form> | ||
|
||
<form action="/pvp-names"> | ||
<input type="submit" value="Player vs Player"> | ||
</form> |
This file contains 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 @@ | ||
<h1><%= @player_one.name %>, make your choice</h1> | ||
<br /> | ||
<form action='/result'> | ||
<input type='hidden' name='rock' value='rock'> | ||
<input type='submit' value='rock'> | ||
</form> | ||
|
||
<form action='/result'> | ||
<input type='hidden' name='paper' value ='paper'> | ||
<input type='submit' value='paper'> | ||
</form> | ||
|
||
<form action='/result'> | ||
<input type='hidden' name='scissors' value='scissors'> | ||
<input type='submit' value='scissors'> | ||
</form> |
This file contains 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 @@ | ||
<form action="/names"> | ||
<input type="text" name="player_one" placeholder="player one name"> | ||
<input type="text" name="player_two" placeholder="player two name"> | ||
<input type="submit" value="Submit"> | ||
</form> |
This file contains 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 @@ | ||
<h1><%= @player_one.name %>, make your choice</h1> | ||
<br /> | ||
<form action='/pvp-play-2'> | ||
<input type='hidden' name='rock' value='rock'> | ||
<input type='submit' value='rock'> | ||
</form> | ||
|
||
<form action='/pvp-play-2'> | ||
<input type='hidden' name='paper' value ='paper'> | ||
<input type='submit' value='paper'> | ||
</form> | ||
|
||
<form action='/pvp-play-2'> | ||
<input type='hidden' name='scissors' value='scissors'> | ||
<input type='submit' value='scissors'> | ||
</form> |
This file contains 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 @@ | ||
<h1><%= @player_two.name %>, make your choice</h1> | ||
<br /> | ||
<form action='/result'> | ||
<input type='hidden' name='rock' value='rock'> | ||
<input type='submit' value='rock'> | ||
</form> | ||
|
||
<form action='/result'> | ||
<input type='hidden' name='paper' value ='paper'> | ||
<input type='submit' value='paper'> | ||
</form> | ||
|
||
<form action='/result'> | ||
<input type='hidden' name='scissors' value='scissors'> | ||
<input type='submit' value='scissors'> | ||
</form> |
This file contains 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,10 @@ | ||
<h1>Game result</h1> | ||
|
||
<p>You chose <%= @player_one.choice%></p> | ||
<p>The AI chose <%= @game.ai_choice%></p> | ||
|
||
<p><%= @result %></p> | ||
|
||
<form action='/play'> | ||
<input type='submit' value='Play again'> | ||
</form> |
This file contains 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 @@ | ||
<form action="/names"> | ||
<input type="text" name="player_one" placeholder="enter your name"> | ||
<input type="hidden" name="player_two" value="computer"> | ||
<input type="submit" value="Submit"> | ||
</form> |
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.
This logic would be better in the model.
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.
Thanks for the feedback, really useful :)