Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Player Class
- # You will have a Player class, where you will put all of your Player-specific logic and properties.
- # Your program will still have to keep track of whose turn it is, and check players' scores and lives.
- # Methods
- # Your Player class should have instance methods for the following tasks:
- # gain a point
- # lose a life
- # TIPS!
- # You can use attr_accessor for things like Player names.
- # Continue to use the enhancements from the previous exercise.
- # If you didn't get a chance to code them then, do so now. This includes Better Math, and Colourization.
- puts "Let's play a two player math game!"
- puts "Player 1, what is your name"
- player1Name = gets.chomp
- puts "Player 2, what is your name"
- player2Name = gets.chomp
- class Question
- attr_accessor :answer, :print
- # def answer
- # @answer
- # end
- # def print
- # @print
- # end
- def initialize
- @operator = rand(0..3)
- @number1 = rand(20) + 1
- @number2 = rand(20) + 1
- case
- when 1
- @answer = @number1 + @number2
- @print = "What does #{@number1} + #{@number2} equal?"
- when 2
- @answer = @number1 - @number2
- @print = "What does #{@number1} - #{@number2} equal?"
- when 3
- @answer= @number1 * @number2
- @print = "What does #{@number1} * #{@number2} equal?"
- end
- end
- end
- class Player
- attr_accessor :lives, :name
- def initialize(name)
- @name = name
- @lives = 3
- end
- def alive
- @lives > 0
- end
- end
- players = [Player.new(player1Name), Player.new(player2Name)]
- current_player = 0
- loop do
- break unless (players[0].alive && players[1].alive)
- question = Question.new
- current_player = (current_player+1) % players.length
- player = players[current_player]
- puts "#{player.name}, you have #{player.lives} lives, what does #{question.print}"
- ans = gets.chomp.to_i
- if ans == question.answer
- puts "Wow So Good Maths!"
- else
- player.lives -= 1
- puts "Maths Are No Good, #{player1.name} has #{player1.lives} lives and #{player2.name} has #{player2.lives} lives"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement