-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
RPS Challenge #2129
Open
shsn1990s
wants to merge
2
commits into
makersacademy:main
Choose a base branch
from
shsn1990s: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
RPS Challenge #2129
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'sinatra/base' | ||
require 'sinatra/reloader' | ||
require './lib/player' | ||
require './lib/computer_player' | ||
require './lib/game' | ||
|
||
class RPS < Sinatra::Base | ||
configure :development do | ||
register Sinatra::Reloader | ||
end | ||
|
||
get '/' do | ||
erb :index | ||
end | ||
|
||
post '/names' do | ||
player_1 = Player.new(params[:player_1_name]) | ||
player_2 = ComputerPlayer.new | ||
$game = Game.new(player_1, player_2) | ||
redirect '/play' | ||
end | ||
|
||
get '/play' do | ||
@game = $game | ||
erb :play | ||
end | ||
|
||
post '/move' do | ||
@game = $game | ||
@game.player_1.current_move(params[:Choice]) | ||
@game.player_2.current_move | ||
$outcome = @game.current_round(@game.player_1.current_move(params[:Choice]), @game.player_2.current_move) | ||
erb :move | ||
end | ||
|
||
post '/continue' do | ||
@game = $game | ||
if (@game.player_1.wins == 3) or (@game.player_2.wins == 3) | ||
redirect '/game-over' | ||
else | ||
redirect '/play' | ||
end | ||
end | ||
|
||
get '/game-over' do | ||
@game = $game | ||
erb :game_over | ||
end | ||
|
||
# start the server if ruby file executed directly | ||
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,2 @@ | ||
require_relative "./app" | ||
run RPS |
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,23 @@ | ||
require_relative 'player' | ||
|
||
class ComputerPlayer < Player | ||
|
||
attr_reader :name, :move, :wins | ||
|
||
def initialize(name = "Computer") | ||
super | ||
end | ||
|
||
def current_move | ||
selection = ["Rock", "Paper", "Scissors"] | ||
@move = selection[rand(0..2)] | ||
end | ||
|
||
def won | ||
@wins += 1 | ||
end | ||
|
||
def computer? | ||
true | ||
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,38 @@ | ||
class Game | ||
|
||
attr_reader :player_1, :player_2, :rounds, :overall_winner | ||
|
||
def initialize(player_1, player_2) | ||
@player_1 = player_1 | ||
@player_2 = player_2 | ||
@overall_winner = "" | ||
end | ||
|
||
def current_round(player_1_move, player_2_move) | ||
winner = "" | ||
return winner = "draw" if player_1_move == player_2_move | ||
case player_1_move | ||
when "Rock" | ||
if player_2_move == "Paper" | ||
winner = @player_2 | ||
else | ||
winner = @player_1 | ||
end | ||
when "Paper" | ||
if player_2_move == "Scissors" | ||
winner = @player_2 | ||
else | ||
winner = @player_1 | ||
end | ||
when "Scissors" | ||
if player_2_move == "Rock" | ||
winner = @player_2 | ||
else | ||
winner = @player_1 | ||
end | ||
end | ||
winner.won | ||
@overall_winner = winner if winner.wins == 3 | ||
return winner | ||
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,22 @@ | ||
class Player | ||
|
||
attr_reader :name, :move, :wins | ||
|
||
def initialize(name) | ||
@name = name | ||
@wins = 0 | ||
end | ||
|
||
def current_move(move) | ||
@move = move | ||
end | ||
|
||
def won | ||
@wins += 1 | ||
end | ||
|
||
def computer? | ||
false | ||
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,17 @@ | ||
require 'computer_player' | ||
|
||
describe ComputerPlayer do | ||
subject(:computer_player) { described_class.new } | ||
|
||
it "Inherits from the player class" do | ||
expect(described_class).to be < Player | ||
end | ||
|
||
it "Checks to see whether it is a computer" do | ||
expect(computer_player.computer?).to eq true | ||
end | ||
|
||
it "Returns the default name of Computer" do | ||
expect(computer_player.name).to eq "Computer" | ||
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,6 @@ | ||
feature 'Enter Names' do | ||
scenario 'Submit player 1 name and displays it on the following page' do | ||
sign_in_and_play | ||
expect(page).to have_content("SH vs. Computer") | ||
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,16 @@ | ||
require 'game' | ||
require 'player' | ||
|
||
describe Game do | ||
subject(:player_1) { Player.new("SH") } | ||
subject(:player_2) { ComputerPlayer.new } | ||
subject(:game) { Game.new(:player_1, :player_2) } | ||
|
||
it "Initializes" do | ||
expect(game.player_1).to eq :player_1 | ||
expect(game.player_2).to eq :player_2 | ||
end | ||
it "Confirms draw if selection is the same" do | ||
expect(game.current_round("Rock", "Rock")).to eq "draw" | ||
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,27 @@ | ||
require 'Player' | ||
|
||
RSpec.describe Player do | ||
subject(:sh) { Player.new("SH") } | ||
|
||
it "Returns the player's name" do | ||
expect(sh.name).to eq "SH" | ||
end | ||
|
||
it "Checks to see if it is a computer player" do | ||
expect(sh.computer?).to eq false | ||
end | ||
|
||
it "Returns the move passed through the current_move method" do | ||
sh.current_move("Rock") | ||
expect(sh.move).to eq "Rock" | ||
end | ||
|
||
it "Calls the won method and checks if it has increased by 1" do | ||
# No. of wins to be initally 0 | ||
expect(sh.wins).to eq 0 | ||
# No. of wins to increase by 1 upon first win | ||
sh.won | ||
expect(sh.wins).to eq 1 | ||
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,5 @@ | ||
def sign_in_and_play | ||
visit('/') | ||
fill_in :player_1_name, with: 'SH' | ||
click_button "Submit" | ||
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,5 @@ | ||
<% if @game.overall_winner.name == "Computer" %> | ||
<h1>Game Over!</h1> | ||
<% else %> | ||
<h1><%= @game.overall_winner.name %> you won!</h1> | ||
<% 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,8 @@ | ||
<h1>Rock, Paper, Scissors!</h1> | ||
<form action="/names" method="post"> | ||
<label for="player_1_name"> | ||
Please enter your name: | ||
<input type="text" name="player_1_name"> | ||
</label> | ||
<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,11 @@ | ||
<h1><%= @game.player_1.name %> chose <%= @game.player_1.move %></h1> | ||
<h1><%= @game.player_2.name %> chose <%= @game.player_2.move %></h1> | ||
<% if $outcome == "draw" %> | ||
<h1>It's a draw!</h1> | ||
<% else %> | ||
<h1><%= $outcome.name %> won this round!</h1> | ||
<% end %> | ||
|
||
<form action="/continue" method="post"> | ||
<input type="submit" value="Continue"> | ||
</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,25 @@ | ||
<style> | ||
.glow { | ||
font-size: 40px; | ||
color: navy; | ||
animation: glow 1s ease-in-out infinite alternate; | ||
} | ||
@-webkit-keyframes glow { | ||
from { | ||
text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 10px #000099, 0 0 10px #000099, 0 0 10px #000099, 0 0 10px #000099, 0 0 10px #000099; | ||
} | ||
|
||
to { | ||
text-shadow: 0 0 10px #fff, 0 0 30px #000099, 0 0 10px #000099, 0 0 10px #000099, 0 0 10px #000099, 0 0 10px #000099, 0 0 10px #000099; | ||
} | ||
} | ||
</style> | ||
|
||
<h1><%= @game.player_1.name %> vs. Computer</h1> | ||
<h2><%= @game.player_1.name %>: <%= @game.player_1.wins %></h2> | ||
<h2><%= @game.player_2.name %>: <%= @game.player_2.wins %></h2> | ||
<form action="/move" method="post"> | ||
<input type="submit" name="Choice" value="Rock"> | ||
<input type="submit" name="Choice" value="Paper"> | ||
<input type="submit" name="Choice" value="Scissors"> | ||
</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.
Nice clean control file, you've extracted the methods well.