-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Farzan's RPS game (2 user stories complete) #2109
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?
Changes from all commits
a0e133e
7e55055
e974b68
249bc4e
d56a0a1
3f1b2de
27d86c4
2004c63
1698a37
88120fa
f03f401
b039cb0
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 |
|---|---|---|
|
|
@@ -13,4 +13,7 @@ end | |
|
|
||
| group :development, :test do | ||
| gem 'rubocop', '1.20' | ||
| gem 'sinatra-contrib' | ||
| gem 'puma' | ||
| gem 'rack' | ||
| end | ||
| 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/game' | ||
|
|
||
| class Rps < Sinatra::Base | ||
|
|
||
| configure :development do | ||
| register Sinatra::Reloader | ||
| end | ||
|
|
||
| get '/' do | ||
| erb :index | ||
| end | ||
|
|
||
| enable :sessions | ||
|
|
||
| post '/names' do | ||
| $game = Game.new(Player.new(params[:player])) | ||
| redirect to('/play') | ||
| end | ||
|
|
||
| get '/play' do | ||
| @game = $game | ||
| @message = session[:message] | ||
| erb :play | ||
| end | ||
|
|
||
| post '/rock' do | ||
| @game = $game | ||
| @game.rock | ||
| session[:message] = @game.message | ||
| redirect '/play' | ||
| end | ||
|
|
||
| post '/paper' do | ||
| @game = $game | ||
| @game.paper | ||
| session[:message] = @game.message | ||
| redirect '/play' | ||
| end | ||
|
|
||
| post '/scissors' do | ||
| @game = $game | ||
| @game.scissors | ||
| session[:message] = @game.message | ||
| redirect '/play' | ||
| end | ||
|
|
||
| run! if app_file == $0 | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| require_relative './app' | ||
| run Rps |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| class Game | ||
|
|
||
| attr_reader :player, :win | ||
|
|
||
| def initialize(player) | ||
| @player = player | ||
| @win = nil | ||
| @message = "" | ||
| 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. this feels like neat logic - very easy to read |
||
| def rock | ||
| case rpc_play | ||
| when "rock" | ||
| @win = nil | ||
| when "paper" | ||
| @win = false | ||
| when "scissors" | ||
| @win = true | ||
| end | ||
| end | ||
|
|
||
| def paper | ||
| case rpc_play | ||
| when "rock" | ||
| @win = true | ||
| when "paper" | ||
| @win = nil | ||
| when "scissors" | ||
| @win = false | ||
| end | ||
| end | ||
|
|
||
| def scissors | ||
| case rpc_play | ||
| when "rock" | ||
| @win = false | ||
| when "paper" | ||
| @win = true | ||
| when "scissors" | ||
| @win = nil | ||
| end | ||
| end | ||
|
|
||
| def message | ||
| case @win | ||
| when true | ||
| @message = "You've won! Well done!" | ||
| when false | ||
| @message = "You've lost! Try again!" | ||
| when nil | ||
| @message = "It's a tie! Try again!" | ||
| 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. This approach can be simplified to make this class more concise. Check out the following approach: https://github.com/makersacademy/rps-challenge/blob/main/docs/review.md#use-of-ifelsif-conditionals-for-business-logic |
||
|
|
||
| private | ||
|
|
||
|
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 could consider keeping the computer in a separate class |
||
| def rpc_play | ||
| ["rock", "paper", "scissors"].sample | ||
| 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,19 @@ | ||
| feature "Enter names" do | ||
| scenario "submitting names" do | ||
| visit("/") | ||
| fill_in :player, with: "Farzan" | ||
| click_button "Submit" | ||
|
|
||
| save_and_open_page | ||
|
|
||
| expect(page).to have_content("Let's Play Farzan!\nRock, Paper, Scissors?\nRock\nPaper\nScissors") | ||
| end | ||
|
|
||
| scenario "display homepage" do | ||
| visit '/' | ||
| expect(page).to have_content("Welcome to: Rock, Paper, Scissors!") | ||
| expect(page).to have_content("Please enter your name to start:") | ||
| expect(page).to have_button("Let's play!") | ||
| end | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| feature "play" do | ||
|
|
||
| before do | ||
| visit ('/') | ||
| fill_in("player", with: "Farzan") | ||
| click_on("Let's play!") | ||
| end | ||
|
|
||
| scenario "displays rock paper scissor buttons" do | ||
| expect(page).to have_button("Rock") | ||
| expect(page).to have_button("Paper") | ||
| expect(page).to have_button("Scissors") | ||
| end | ||
|
|
||
| end |
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.
Currently, you define a route for each player move option, and each of these routes uses mostly the same code. This is a duplication of code that could be refactored.
Another method of approach this logic is to pass in the player move as a parameter, calling the same route, and passing that param into the @GAMe instance.