Guest User

Untitled

a guest
Apr 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. class Player
  2. attr_accessor :name
  3. NAMES = ['R2D2', 'BOB', 'ELISA']
  4. MOVES = ['rock', 'paper', 'scissors']
  5. def initialize(name)
  6. @name = name
  7. end
  8.  
  9. def choose
  10. puts 'Choose either rock paper or scissors'
  11. end
  12.  
  13. end
  14.  
  15. class Human < Player
  16. attr_accessor :move
  17. def initialize
  18. @name = pick_a_name
  19. end
  20.  
  21. def choose
  22. super
  23. loop do
  24. choice = gets.chomp
  25. if !Player::MOVES.include? choice
  26. puts 'Please choose either rock paper or scissors'
  27. next
  28. else
  29. self.move = choice
  30. puts "#{name} chose #{choice}"
  31. break
  32. end
  33. end
  34. end
  35.  
  36. private
  37.  
  38. def pick_a_name
  39. puts "What is your name"
  40. answer = gets.chomp
  41. end
  42. end
  43.  
  44. class Computer < Player
  45. attr_accessor :move
  46. def initialize
  47. super(Player::NAMES.sample)
  48. end
  49.  
  50. def choose
  51. self.move = Player::MOVES.sample
  52. puts "#{name} chose #{move}"
  53. end
  54.  
  55. end
  56.  
  57. class RPSGame
  58. @@games = nil
  59. attr_accessor :human, :computer, :score
  60.  
  61. def initialize
  62. @human = Human.new
  63. @computer = Computer.new
  64. @score = {human: 0,computer: 0}
  65. end
  66.  
  67. def display_welcome_message
  68. puts 'Welcome to Rock Paper Scissors'
  69. end
  70.  
  71. def display_goodbye_message
  72. puts 'Thanks for playing rock paper scissors'
  73. end
  74.  
  75. def play_again?
  76. puts "Would you like to play again. Type y or n"
  77. answer = nil
  78. loop do
  79. answer = gets.chomp
  80. break if ['y', 'n'].include? answer.downcase
  81. puts 'Sorry you have to choose y or n'
  82. end
  83. if answer == 'y'
  84. @@games += 1
  85. return true
  86. end
  87. return false if answer == 'n'
  88. end
  89.  
  90. def play
  91. display_welcome_message
  92. loop do
  93. human.choose
  94. computer.choose
  95. display_winner
  96. break unless play_again?
  97. end
  98. display_goodbye_message
  99. end
  100. end
Add Comment
Please, Sign In to add comment