From 02de88790cd77a30d7c35e0e9d8a548a7ea48722 Mon Sep 17 00:00:00 2001 From: Christian Choi Date: Thu, 14 Jan 2016 09:52:00 -0500 Subject: [PATCH 1/2] Added faker gem to Gemfile --- Gemfile | 5 +++++ Gemfile.lock | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock 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 From f7ee7f80ebb2da2424ba3607935190c735d6a671 Mon Sep 17 00:00:00 2001 From: Christian Choi Date: Thu, 14 Jan 2016 14:10:56 -0500 Subject: [PATCH 2/2] Basic RockPaperScissors game. Needs refactoring but should be functional --- .DS_Store | Bin 0 -> 6148 bytes RPS/controller.rb | 121 ++++++++++++++++++++++++++++++++++++++++++++++ RPS/model.rb | 51 +++++++++++++++++++ RPS/view.rb | 68 ++++++++++++++++++++++++++ 4 files changed, 240 insertions(+) create mode 100644 .DS_Store create mode 100644 RPS/controller.rb create mode 100644 RPS/model.rb create mode 100644 RPS/view.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 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 = <