Advertisement
Guest User

Untitled

a guest
May 30th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. ## got to using a binding.pry
  2.  
  3. class Player
  4. attr_accessor :name, :prompt, :score, :lives
  5.  
  6. def initialize
  7. @score = 0
  8. @lives = 3
  9. end
  10.  
  11. def get_name
  12. puts "Which player are you - 1 or 2?"
  13. self.name = gets.chomp().to_s
  14. end
  15.  
  16. def gain_a_point
  17. self.score += 1
  18. end
  19.  
  20. def lose_a_life
  21. self.lives -= 1
  22. end
  23.  
  24.  
  25. end
  26.  
  27.  
  28.  
  29. class Game
  30. attr_accessor :players
  31. attr_accessor :active_player
  32. attr_accessor :game_question
  33. attr_accessor :answer
  34. attr_accessor :point
  35.  
  36. def initialize
  37. # @turn = ""
  38. # @question = ""
  39. # @answer = ""
  40. self.players = []
  41. 2.times { players << Player.new }
  42. self.active_player = players[0]
  43. end
  44.  
  45. # def game_question
  46. # Question.new.equation
  47. # end
  48.  
  49. def run_the_game
  50. loop do
  51. ask = Question.new
  52. ask.equation
  53. answer = gets.chomp().to_i
  54. if ask.check_answer(answer)
  55. puts "That is correct - switch player"
  56. active_player.gain_a_point
  57. else
  58. puts "Sorry, wrong answer - switch player"
  59. active_player.lose_a_life
  60. end
  61. if game_over
  62. break
  63. end
  64.  
  65.  
  66. end
  67. end
  68.  
  69. def game_over
  70. active_player.lives == 0 || active_player.score == 8
  71. # game has a loser || game has a winner
  72. end
  73.  
  74.  
  75. end
  76.  
  77.  
  78. class Question
  79. attr_accessor :num1
  80. attr_accessor :num2
  81. attr_accessor :equation
  82.  
  83. def initialize
  84. @equation = "i + i"
  85. @ask_question = ""
  86. get_random_numbers
  87. end
  88.  
  89.  
  90. def get_random_numbers
  91. self.num1 = rand(1..20)
  92. self.num2 = rand(1..20)
  93. end
  94.  
  95. def equation
  96. puts "What is #{num1} + #{num2}?"
  97.  
  98. end
  99.  
  100. def check_answer(user_answer)
  101. answer = num1 + num2
  102. user_answer == answer
  103. end
  104.  
  105.  
  106. end
  107.  
  108. Player.new.get_name
  109. Game.new.run_the_game
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement