Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# A sample Gemfile
source "https://rubygems.org"

# gem "rails"
gem 'faker'
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
GEM
remote: https://rubygems.org/
specs:
faker (1.6.1)
i18n (~> 0.5)
i18n (0.7.0)

PLATFORMS
ruby

DEPENDENCIES
faker

BUNDLED WITH
1.11.2
121 changes: 121 additions & 0 deletions RPS/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
require_relative 'view'
require_relative 'model'

class RPSController
include RPSView

attr_reader :choices

def initialize
@choices = {"rock" => 1, "paper" => 2, "scissors" => 3}
end

def run!
# choices = Choices.new
player = Player.new
Print::title

loop do
Print::menu
case get_input.to_i
when 1
num_rounds = 0
loop do
num_rounds = get_input(Print::request_rounds).to_i
if num_rounds > 0 and num_rounds < 6
break
else
Print::error_message
end
end
num_rounds.times { |x| play(x + 1, player) }
print_player_results(player)
update_score(player)
player.reset
when 2
puts "Records"
print_player_records(player)
when 3
puts "Thanks for playing. Goodbye!"
exit
else
Print::error_message
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think generally we should prefer structured loops (until ..., while ...), where the termination clause is explicitly defined at the beginning of the loop, rather than loop do patterns with a break tucked away somewhere in the body.


end

def play(number, player)
comp_value = rand(1..3)
comp_choice = choices.key(comp_value)
loop do
print "Round #{number}! "
case get_input(Print::request_rps)
when "rock"
Print::run_spinner
puts "PLAYER: rock\t\tCOMPUTER: #{comp_choice}"
determine_outcome("rock", comp_value, player) #DRY. need to refactor
break
when "paper"
Print::run_spinner
puts "PLAYER: paper\t\tCOMPUTER: #{comp_choice}"
determine_outcome("paper", comp_value, player)
break
when "scissors"
Print::run_spinner
puts "PLAYER: scissors\tCOMPUTER: #{comp_choice}"
determine_outcome("scissors", comp_value, player)
break
else
Print::error_message
end
end
end

def print_player_records(player)
puts "Games won: #{player.game_wins}"
puts "Games lost: #{player.game_losses}"
puts "Games tied: #{player.game_ties}"
end

def update_score(player)
if player.round_wins > player.round_losses
player.win_game
elsif player.round_wins < player.round_losses
player.lose_game
else
player.tie
end
end

def print_player_results(player)
puts "Wins: #{player.round_wins} Losses: #{player.round_losses}"
end

def get_input(question=nil)
puts question if question
print "> "
gets.chomp.downcase
end

def play_again?
['y', 'yes'].include? get_input
end

def determine_outcome(player_choice, computer_value, player)
player_value = choices[player_choice]
difference = player_value - computer_value
if difference == 0
Print::player_ties
elsif difference == 1 or difference == -2
Print::player_wins
player.win_round
else
Print::player_loses
player.lose_round
end
end

end

RPSController.new.run!
51 changes: 51 additions & 0 deletions RPS/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Player
attr_reader :round_wins, :round_losses, :game_wins, :game_losses, :game_ties

def initialize
@round_wins = 0
@round_losses = 0
@game_wins = 0
@game_losses = 0
@game_ties = 0
end

def win_round
@round_wins += 1
end

def lose_round
@round_losses += 1
end

def win_game
@game_wins += 1
end

def lose_game
@game_losses += 1
end

def tie
@game_ties += 1
end

def reset
@round_wins = 0
@round_losses = 0
end

def update(player)
@game_wins = player.game_wins
@game_losses = player.game_losses
end

end

# class Choices
# attr_reader :hash

# def initialize
# @hash = {"rock" => 1, "paper" => 2, "scissors" => 3}
# end

# end
68 changes: 68 additions & 0 deletions RPS/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module RPSView

module Print

class << self
def title
title = <<TITLE

+=+=+=+=+=+=+=+=+=+=+=+
| ROCK PAPER SCISSORS |
+=+=+=+=+=+=+=+=+=+=+=+

TITLE

puts title
end

def menu
menu = <<MENU

*---Welcome---*
| (1) Play |
| (2) Records |
| (3) Quit |
*-------------*

MENU
puts menu
end

def request_rounds
puts "Enter how many rounds you'd like to play (1-5)?"
end

def request_rps
puts "Choose rock, paper, or scissors."
end

def run_spinner
print "Rock..."; sleep 0.5;
print "Paper..."; sleep 0.5;
print "Scissors..."; sleep 0.5;
puts "SHOOT!";
end

def player_wins
puts "You WIN!"
end

def player_ties
puts "You TIED!"
end

def player_loses
puts "You LOSE"
end

def play_again?
puts "Play again? Y"
end

def error_message
puts "Invalid input. Please try again!"
end

end
end
end