-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Kay D rps challenge #2135
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
base: main
Are you sure you want to change the base?
Kay D rps challenge #2135
Changes from all commits
2175ffd
58f3bd9
0c2b823
0f290e0
afe0564
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require "sinatra/base" | ||
require "sinatra/reloader" | ||
require "./lib/player" | ||
require "./lib/game" | ||
|
||
class RockPaperScissors < Sinatra::Base | ||
enable :sessions | ||
|
||
configure :development do | ||
register Sinatra::Reloader | ||
end | ||
|
||
get '/test' do | ||
'Testing infrastructure working!' | ||
end | ||
|
||
get '/' do | ||
erb(:index) | ||
end | ||
|
||
post '/play' do | ||
@player1 = Player.new(params[:player1]) | ||
@player2 = Player.new(params[:player2]) | ||
$game = Game.new(@player1, @player2) | ||
erb(:play) | ||
end | ||
|
||
post '/player_1_choice' do | ||
$game.player1.add('scissors') if params[:scissors] | ||
$game.player1.add('rock') if params[:rock] | ||
$game.player1.add('paper') if params[:paper] | ||
redirect ('/round2') | ||
end | ||
|
||
get '/round2' do | ||
erb(:round2) | ||
end | ||
|
||
post '/player_2_choice' do | ||
$game.player2.add('scissors') if params[:scissors] | ||
$game.player2.add('rock') if params[:rock] | ||
$game.player2.add('paper') if params[:paper] | ||
redirect ('/result') | ||
end | ||
|
||
get '/result' do | ||
@result = "#{$game.player1.name} wins!" if $game.winner == true | ||
@result = "#{$game.player2.name} wins!" if $game.winner == false | ||
@result = "It's a draw!" if $game.winner.nil? | ||
erb(:result) | ||
end | ||
|
||
run! if app_file == $0 | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require './app' | ||
run RockPaperScissors |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Game | ||
attr_accessor :player1, :player2 | ||
|
||
def initialize(player1, player2) # user is a string representing the choice of the player | ||
@player1 = player1 # computer is a string, representing the computer choice | ||
@player2 = player2 | ||
end | ||
|
||
def winner | ||
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. I like this solution to the coding of the game of rock, paper, scissors |
||
return nil if @player1.choice == @player2.choice | ||
options = ['rock', 'paper', 'scissors'] | ||
first_play = options.find_index(@player1.choice) | ||
options[first_play - 1] == @player2.choice | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Player | ||
attr_accessor :name, :choice | ||
|
||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
def add(choice) | ||
@choice = choice | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
RSpec.describe 'form for player choice' do | ||
|
||
feature 'it asks player 1 for their choice' do | ||
scenario 'when player 1 is called Annabelle' do | ||
sign_in_and_play | ||
expect(page).to have_content "Annabelle's turn: please make your choice." | ||
end | ||
end | ||
|
||
feature 'player 1 chooses scissors' do | ||
scenario 'returns their choice' do | ||
sign_in_and_play | ||
click_on "Scissors" | ||
click_on "Rock" | ||
expect(page).to have_content "Annabelle chose scissors." | ||
end | ||
end | ||
|
||
feature 'player 1 chooses rock' do | ||
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. Perhaps the second and third tests here are redundant if the first one is working? |
||
scenario 'returns their choice' do | ||
sign_in_and_play | ||
click_on "Rock" | ||
click_on "Paper" | ||
expect(page).to have_content "Annabelle chose rock." | ||
end | ||
end | ||
|
||
feature 'player 1 chooses paper' do | ||
scenario 'returns their choice' do | ||
sign_in_and_play | ||
click_on "Paper" | ||
click_on "Rock" | ||
expect(page).to have_content "Annabelle chose paper." | ||
end | ||
end | ||
|
||
feature 'it asks player 2 for their choice' do | ||
scenario 'when player 2 is called Sandy' do | ||
sign_in_and_play | ||
click_on "Scissors" | ||
expect(page).to have_content "Sandy's turn: please make your choice." | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
RSpec.describe 'player name form' do | ||
feature 'player enters their name' do | ||
scenario 'it returns their name' do | ||
visit ("/") | ||
fill_in "player1", with: "Annabelle" | ||
fill_in "player2", with: "Sandy" | ||
click_on "Submit" | ||
expect(page).to have_content "Welcome Annabelle and Sandy." | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
RSpec.describe 'results' do | ||
feature 'player 1 picks rock and player 2 chooses scissors' do | ||
scenario 'shows player 1 as the winner' do | ||
sign_in_and_play | ||
click_on 'Rock' | ||
click_on 'Scissors' | ||
expect(page).to have_content 'Annabelle wins!' | ||
end | ||
end | ||
|
||
feature 'player 1 picks rock and player 2 picks paper' do | ||
scenario 'shows player 1 as the winner' do | ||
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. I think this line should say "shows player 2 as the winner" |
||
sign_in_and_play | ||
click_on 'Rock' | ||
click_on 'Paper' | ||
expect(page).to have_content 'Sandy wins!' | ||
end | ||
end | ||
|
||
feature 'both players pick paper' do | ||
scenario 'shows a draw' do | ||
sign_in_and_play | ||
click_on 'Paper' | ||
click_on 'Paper' | ||
expect(page).to have_content "It's a draw!" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
RSpec.describe 'infrastructure' do | ||
|
||
feature 'testing infrastructure' do | ||
scenario 'can run app and check page content' do | ||
visit('/test') | ||
expect(page).to have_content 'Testing infrastructure working!' | ||
end | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def sign_in_and_play | ||
visit ("/") | ||
fill_in "player1", with: "Annabelle" | ||
fill_in "player2", with: "Sandy" | ||
click_on "Submit" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'game' | ||
|
||
RSpec.describe Game do | ||
# let(:user) { double :player, name: 'Sandra', choice: 'scissors' } | ||
# let(:computer) { double :computer, name: 'Computer', choice: 'paper' } | ||
# subject(:game) { Game.new(:user, :computer) } | ||
|
||
it 'returns the names of each player' do | ||
player1 = double :player, name: 'Elspeth' | ||
player2 = double :player, name: 'Cuddle' | ||
game = Game.new(player1, player2) | ||
expect(game.player1.name).to eq 'Elspeth' | ||
expect(game.player2.name).to eq 'Cuddle' | ||
end | ||
|
||
it "returns the players' choices" do | ||
player1 = double :player, choice: 'scissors' | ||
player2 = double :player, choice: 'rock' | ||
game = Game.new(player1, player2) | ||
expect(game.player1.choice).to eq 'scissors' | ||
expect(game.player2.choice).to eq 'rock' | ||
end | ||
|
||
it "returns true when player 1 wins" do | ||
player1 = double :player, name: 'Sandra', choice: 'scissors' | ||
player2 = double :player, name: 'Cuddles', choice: 'paper' | ||
game = Game.new(player1, player2) | ||
expect(game.winner).to eq true | ||
end | ||
|
||
it 'returns false when the player 2 wins' do | ||
player1 = double :player, name: 'Sandra', choice: 'scissors' | ||
player2 = double :player, name: 'Cuddles', choice: 'rock' | ||
game = Game.new(player1, player2) | ||
expect(game.winner).to eq false | ||
end | ||
|
||
it 'returns nil when there is a drawer' do | ||
player1 = double :player, name: 'Sandra', choice: 'rock' | ||
player2 = double :player, name: 'Cuddles', choice: 'rock' | ||
game = Game.new(player1, player2) | ||
expect(game.winner).to eq nil | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'player' | ||
|
||
RSpec.describe Player do | ||
it 'returns the player name' do | ||
player = Player.new('Sandra') | ||
expect(player.name).to eq 'Sandra' | ||
end | ||
|
||
it 'add a choice to the player' do | ||
player = Player.new('Sandra') | ||
player.add('scissors') | ||
expect(player.choice).to eq 'scissors' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<h1>Let's play Rock Paper Scissors! <p></p></h1> | ||
Please enter your name to begin. <p> </p> | ||
<form action='/play' method='post'> | ||
<label for="player_1_name"> | ||
Player 1 Name | ||
<input type="text" name="player1" placeholder="Player 1" /> | ||
</label> | ||
<label for="player_2_name"> | ||
Player 2 Name | ||
<input type="text" name="player2" placeholder="Player 1" /> | ||
</label> | ||
<input type="submit" value="Submit" /> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Welcome <%[email protected]%> and <%[email protected]%>. <p></p> | ||
|
||
<%[email protected]%>'s turn: please make your choice. <p></p> | ||
|
||
<form action="/player_1_choice" method="post"> | ||
<input type="submit" name="scissors" value="Scissors"/> | ||
<input type="submit" name="paper" value="Paper"/> | ||
<input type="submit" name="rock" value="Rock"/> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%=$game.player1.name%> chose <%=$game.player1.choice%>. <p></p> | ||
<%=$game.player2.name%> chose <%=$game.player2.choice%>. <p></p> | ||
<%=@result%> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<%=$game.player2.name%>'s turn: please make your choice. <p> </p> | ||
|
||
<form action="/player_2_choice" method="post"> | ||
<input type="submit" name="scissors" value="Scissors"/> | ||
<input type="submit" name="paper" value="Paper"/> | ||
<input type="submit" name="rock" value="Rock"/> | ||
</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.
this is a clear and concise way of displaying who has won the game.