Guest User

Untitled

a guest
Feb 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. class Move
  2. attr_accessor :value
  3.  
  4. VALUES = ['rock', 'paper', 'scissors', 'spock', 'lizard']
  5.  
  6. WINNING_MOVES = {
  7. 'rock' => ['scissors', 'lizard'],
  8. 'paper' => ['rock', 'spock'],
  9. 'scissors' => ['paper', 'lizard'],
  10. 'lizard' => ['paper', 'spock'],
  11. 'spock' => ['rock', 'scissors']
  12. }
  13.  
  14. def initialize(value)
  15. @value = value
  16. end
  17.  
  18. def >(other_move)
  19. WINNING_MOVES[@value].include?(other_move.to_s)
  20. end
  21.  
  22. def <(other_move)
  23. WINNING_MOVES[other_move.to_s].include?(@value)
  24. end
  25.  
  26. def to_s
  27. @value
  28. end
  29. end
  30.  
  31. # a Player has a score, has a move, has a name, has a move_history
  32. class Player
  33. attr_accessor :score, :move, :name, :move_history
  34.  
  35. def initialize
  36. set_name
  37. @score = 0
  38. @move_history = []
  39. end
  40.  
  41. def increment_score
  42. @score += 1
  43. end
  44.  
  45. def reset_series
  46. @score = 0
  47. @move_history = []
  48. end
  49. end
  50.  
  51. # Human player
  52. class Human < Player
  53. system('clear') || system('cls')
  54. def set_name
  55. input_name = ""
  56. loop do
  57. puts "Welcome to Rock - Paper - Scissors - Lizard - Spock!"
  58. puts "Please enter your name to begin:"
  59. input_name = gets.chomp
  60. break unless input_name.strip == ""
  61. puts "Sorry, must enter a value."
  62. end
  63. self.name = input_name
  64. end
  65.  
  66. def choose_move
  67. choice = nil
  68. loop do
  69. puts ''
  70. puts "Please choose between: rock, paper, scissors, spock, lizard:"
  71. choice = gets.chomp.downcase
  72. break if Move::VALUES.include?(choice)
  73. puts "Sorry, invalid choice"
  74. end
  75. self.move = Move.new(choice)
  76. move_history.push(move)
  77. end
  78. end
  79.  
  80. # Computer players that are not R2D2 or Hal
  81. class Computer < Player
  82. def set_name
  83. self.name = ['Chappie', 'Astro-Boy', 'WALL-E'].sample
  84. end
  85.  
  86. def choose_move
  87. self.move = Move.new(Move::VALUES.sample)
  88. move_history.push(move)
  89. end
  90. end
  91.  
  92. # Hal usually chooses scissors and sometimes paper
  93. class Hal < Computer
  94. def set_name
  95. self.name = 'Hal'
  96. end
  97.  
  98. def choose_move
  99. self.move = Move.new(['scissors', 'scissors', 'scissors', 'paper'].sample)
  100. move_history.push(move)
  101. end
  102. end
  103.  
  104. # R2D2 always chooses rock
  105. class R2D2 < Computer
  106. def set_name
  107. self.name = 'R2D2'
  108. end
  109.  
  110. def choose_move
  111. self.move = Move.new(['rock'].sample)
  112. move_history.push(move)
  113. end
  114. end
  115.  
  116. # Game Orchestration Engine
  117. class RPSGame
  118. MAX_SCORE = 3
  119.  
  120. attr_accessor :human, :computer
  121.  
  122. def initialize
  123. @human = Human.new
  124. @computer = [Computer.new, Hal.new, R2D2.new].sample
  125. end
  126.  
  127. def clear
  128. system('clear') || system('cls')
  129. end
  130.  
  131. def display_welcome_message
  132. puts ''
  133. puts "Welcome #{human.name}, to RPSLS. It's human versus computer!"
  134. puts "The first to win #{MAX_SCORE} rounds is the Series Winner!"
  135. end
  136.  
  137. def display_goodbye_message
  138. puts ""
  139. puts "Thanks for playing Rock, Paper, Scissors, Lizard, Spock. Good bye!"
  140. end
  141.  
  142. def display_moves
  143. puts ''
  144. puts "#{human.name} chose: #{human.move}."
  145. puts "#{computer.name} chose: #{computer.move}."
  146. end
  147.  
  148. def display_current_score
  149. puts ''
  150. puts "#{computer.name}: #{computer.score}"
  151. puts "#{human.name}: #{human.score}"
  152. end
  153.  
  154. def display_human_champion?
  155. if human_series_winner?
  156. puts ''
  157. puts "*** #{human.name} wins the series! ***"
  158. puts "*** Final Score: #{human.score} rounds to #{computer.score} ***"
  159. end
  160. false
  161. end
  162.  
  163. def display_computer_champion?
  164. if computer_series_winner?
  165. puts ''
  166. puts "*** #{computer.name} wins the series! ***"
  167. puts "*** Final Score: #{computer.score} rounds to #{human.score} ***"
  168. end
  169. false
  170. end
  171.  
  172. def human_series_winner?
  173. return false unless human.score >= MAX_SCORE
  174. true
  175. end
  176.  
  177. def computer_series_winner?
  178. return false unless computer.score >= MAX_SCORE
  179. true
  180. end
  181.  
  182. def display_round_winner
  183. puts "**No winner this round! It's a tie!**" if tied_round?
  184. puts "**#{human.name} won this round!**" if human_win_round?
  185. puts "**#{computer.name} won this round!**" if computer_win_round?
  186. end
  187.  
  188. def tied_round?
  189. return false if human.move > computer.move || human.move < computer.move
  190. true
  191. end
  192.  
  193. def human_win_round?
  194. return false unless human.move > computer.move
  195. human.increment_score
  196. true
  197. end
  198.  
  199. def computer_win_round?
  200. return false unless human.move < computer.move
  201. computer.increment_score
  202. true
  203. end
  204.  
  205. def display_move_history
  206. puts ''
  207. puts "#{human.name} history: #{human.move_history.join(' - ')}"
  208. puts "#{computer.name} history: #{computer.move_history.join(' - ')}"
  209. end
  210.  
  211. def play_again?
  212. answer = nil
  213. loop do
  214. puts ''
  215. puts "Would you like to play again? (y/n)"
  216. answer = gets.chomp.downcase
  217. break if ['y', 'n'].include?(answer)
  218. puts "Sorry, must be y or n."
  219. end
  220. answer == 'y'
  221. end
  222.  
  223. def series_winner?
  224. human.score == MAX_SCORE || computer.score == MAX_SCORE
  225. end
  226.  
  227. def game_display
  228. display_moves
  229. display_round_winner
  230. display_current_score
  231. display_move_history
  232. display_human_champion?
  233. display_computer_champion?
  234. end
  235.  
  236. def play
  237. clear
  238. display_welcome_message
  239.  
  240. loop do
  241. human.choose_move
  242. computer.choose_move
  243. # created game_display method to appease Rubocop
  244. game_display
  245.  
  246. break unless play_again?
  247. computer.reset_series && human.reset_series if series_winner?
  248. clear
  249. end
  250. display_goodbye_message
  251. end
  252. end
  253.  
  254. RPSGame.new.play
Add Comment
Please, Sign In to add comment