Skip to content

Instantly share code, notes, and snippets.

@AJFaraday
Created March 25, 2017 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AJFaraday/bc88960fe759d3812b3dd7c2135d4da1 to your computer and use it in GitHub Desktop.
Save AJFaraday/bc88960fe759d3812b3dd7c2135d4da1 to your computer and use it in GitHub Desktop.
game_stats.rb
class Game
def initialize
@characters = []
@characters << Character.new("Alice", 5, 2, 90, 10)
@characters << Character.new("Bob", 5, 2, 50, 50)
@characters << Character.new("Carol", 5, 2, 10, 90)
end
def play
while play_turn
puts "----------------------------"
print_status
puts "----------------------------"
#sleep 1
end
living_characters[0].name
end
def play_turn
living_characters.permutation(2) do |a, b|
puts "#{a.name} attacks #{b.name}"
if a.hits?
blocked = b.blocks?
damage = blocked ? a.block_damage : a.damage
puts "\t#{b.name} blocks" if blocked
puts "\t#{a.name} inflicts #{damage} points of damage"
b.take_damage(damage)
puts "\t#{b.name} has been defeated" if b.dead?
else
puts "\t#{a.name} misses"
end
if living_characters.size == 1
puts "Game over, #{a.name} wins"
return false
end
end
true
end
def print_status
living_characters.each do |player|
puts "#{player.name}: #{player.hit_points} hit points"
end
end
private
def living_characters
@characters.reject(&:dead?)
end
end
require_relative 'character.rb'
require_relative 'game.rb'
wins = 100.times.map {Game.new.play}
counts = Hash.new 0
wins.each { |word| counts[word] += 1 }
puts counts.inspect
# {"Alice"=>10, "Carol"=>64, "Bob"=>26}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment