From e098e186b6bc728c6b2b77545eef633c9a877af5 Mon Sep 17 00:00:00 2001 From: ChrisCPO Date: Tue, 4 Nov 2014 23:19:07 -0500 Subject: [PATCH 1/2] base push of initial pass at code * one round of play is complete * some puts are in for testing purposes only * hand class is basicaly only for one purpose i know ** i plan on moving addressing that issue * plenty of other issues refactor.each --- rps.rb | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 rps.rb diff --git a/rps.rb b/rps.rb new file mode 100644 index 0000000..86d688b --- /dev/null +++ b/rps.rb @@ -0,0 +1,90 @@ +class Hand + attr_reader :positions + def initialize + @positions = %w(rock paper scissors) + end +end + +class Game + +end + +class ActorController + attr_accessor :win, :score + attr_reader :choice + def initialize + @score = 0 + @choice = "" + end + + def win + puts "#{self.class} wins" + @score =+ 1 + end +end + +class PlayerController < ActorController + def choose_hand + puts "choose your hand" + puts "r/p/s or q to quit" + answer = gets.chomp.to_s + if answer == "r" + @choice = "rock" + elsif answer == "p" + @choice = "paper" + elsif answer == "s" + @choice = "scissors" + end + puts "your choice was #{choice}" + end +end + +class AiController < ActorController + def choose_hand + @choice = Hand.new.positions[rand(0..2)] + puts "the bot choose #{choice}" + end +end + +class Round + attr_reader :player, :bot + def initialize + @player = PlayerController.new + @bot = AiController.new + end + + def start + choose_hand + end + + def choose_hand + player.choose_hand + bot.choose_hand + end + + def determine_victor + puts player.choice + puts bot.choice + if player.choice == "rock" && bot.choice == "scissors" + player.win + elsif player.choice == "scissors" && bot.choice == "paper" + player.win + elsif player.choice == "paper" && bot.choice == "rock" + player.win + elsif player.choice == bot.choice + puts "was a tie" + else + bot.win + end + puts player.score + puts bot.score + end +end + + +testround = Round.new +testround.start +testround.determine_victor + + + From c45e51e560749e94e68b709d272bea56f46ade52 Mon Sep 17 00:00:00 2001 From: ChrisCPO Date: Wed, 5 Nov 2014 09:39:45 -0500 Subject: [PATCH 2/2] added a game class --- rps.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/rps.rb b/rps.rb index 86d688b..14c5e4e 100644 --- a/rps.rb +++ b/rps.rb @@ -10,20 +10,26 @@ class Game end class ActorController - attr_accessor :win, :score + attr_accessor :win, :score, :cheating attr_reader :choice def initialize + @cheating = false @score = 0 @choice = "" end def win - puts "#{self.class} wins" @score =+ 1 + #puts "#{self.class} wins" end end class PlayerController < ActorController + def initialize(game) + super() + @game = game + end + def choose_hand puts "choose your hand" puts "r/p/s or q to quit" @@ -34,6 +40,8 @@ def choose_hand @choice = "paper" elsif answer == "s" @choice = "scissors" + elsif answer == "q" + @game.show_scores end puts "your choice was #{choice}" end @@ -48,9 +56,9 @@ def choose_hand class Round attr_reader :player, :bot - def initialize - @player = PlayerController.new - @bot = AiController.new + def initialize(player,bot) + @player = player + @bot = bot end def start @@ -76,15 +84,42 @@ def determine_victor else bot.win end - puts player.score - puts bot.score end end +class Game + attr_reader :player, :bot + def initialize + @player = PlayerController.new(self) + @bot = AiController.new + end + + def start + @round = Round.new(@player, @bot) + @round.start + @round.determine_victor + end -testround = Round.new -testround.start -testround.determine_victor + def show_scores + puts player.score.class + puts "player points #{player.score}" + puts "bot points #{bot.score}" + puts "---------------------" + if player.cheating == true || bot.cheating == true + #then cheating player wins + elsif player.score > bot.score + puts "player wins #{player.score}" + elsif player.score < bot.score + puts "bot wins #{bot.score}" + elsif player.score == bot.score + puts "game was a tie" + else + end + end +end +game = Game.new +game.start +game.show_scores