diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..aeed8f6 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +# A sample Gemfile +source "https://rubygems.org" + +# gem "rails" +gem 'faker' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..6b241e3 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/RPS/controller.rb b/RPS/controller.rb new file mode 100644 index 0000000..d8af492 --- /dev/null +++ b/RPS/controller.rb @@ -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 + + 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! \ No newline at end of file diff --git a/RPS/model.rb b/RPS/model.rb new file mode 100644 index 0000000..964e373 --- /dev/null +++ b/RPS/model.rb @@ -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 \ No newline at end of file diff --git a/RPS/view.rb b/RPS/view.rb new file mode 100644 index 0000000..4e09e8e --- /dev/null +++ b/RPS/view.rb @@ -0,0 +1,68 @@ +module RPSView + + module Print + + class << self + def title +title = <