Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. # Player Class
  2. # You will have a Player class, where you will put all of your Player-specific logic and properties.
  3.  
  4. # Your program will still have to keep track of whose turn it is, and check players' scores and lives.
  5.  
  6. # Methods
  7. # Your Player class should have instance methods for the following tasks:
  8.  
  9. # gain a point
  10. # lose a life
  11. # TIPS!
  12. # You can use attr_accessor for things like Player names.
  13.  
  14. # Continue to use the enhancements from the previous exercise.
  15. # If you didn't get a chance to code them then, do so now. This includes Better Math, and Colourization.
  16.  
  17. puts "Let's play a two player math game!"
  18. puts "Player 1, what is your name"
  19. player1Name = gets.chomp
  20. puts "Player 2, what is your name"
  21. player2Name = gets.chomp
  22.  
  23. class Question
  24. attr_accessor :answer, :print
  25. # def answer
  26. # @answer
  27. # end
  28.  
  29. # def print
  30. # @print
  31. # end
  32. def initialize
  33. @operator = rand(0..3)
  34. @number1 = rand(20) + 1
  35. @number2 = rand(20) + 1
  36.  
  37. case
  38. when 1
  39. @answer = @number1 + @number2
  40. @print = "What does #{@number1} + #{@number2} equal?"
  41. when 2
  42. @answer = @number1 - @number2
  43. @print = "What does #{@number1} - #{@number2} equal?"
  44. when 3
  45. @answer= @number1 * @number2
  46. @print = "What does #{@number1} * #{@number2} equal?"
  47. end
  48. end
  49. end
  50.  
  51. class Player
  52. attr_accessor :lives, :name
  53.  
  54. def initialize(name)
  55. @name = name
  56. @lives = 3
  57. end
  58.  
  59. def alive
  60. @lives > 0
  61. end
  62.  
  63. end
  64.  
  65. players = [Player.new(player1Name), Player.new(player2Name)]
  66. current_player = 0
  67. loop do
  68. break unless (players[0].alive && players[1].alive)
  69. question = Question.new
  70.  
  71. current_player = (current_player+1) % players.length
  72. player = players[current_player]
  73.  
  74.  
  75. puts "#{player.name}, you have #{player.lives} lives, what does #{question.print}"
  76. ans = gets.chomp.to_i
  77. if ans == question.answer
  78. puts "Wow So Good Maths!"
  79. else
  80. player.lives -= 1
  81. puts "Maths Are No Good, #{player1.name} has #{player1.lives} lives and #{player2.name} has #{player2.lives} lives"
  82. end
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement