class JkimsGame::Game
Attributes
title[R]
Public Class Methods
new(title)
click to toggle source
# File lib/jkims_game/game.rb, line 10 def initialize(title) @title = title @players = [] end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/jkims_game/game.rb, line 31 def add_player(player) @players << player end
loop_players(from_file)
click to toggle source
# File lib/jkims_game/game.rb, line 15 def loop_players(from_file) File.readlines(from_file).each do |line| add_player (Player.from_csv(line)) end end
play(rounds)
click to toggle source
# File lib/jkims_game/game.rb, line 35 def play(rounds) puts "There are #{@players.length} players in #{@title}:" @players.each do |player| puts player end treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures to be found:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end 1.upto(rounds) do |round| puts "\nRound: #{round}:" @players.each do |player| GameTurn.take_turn(player) puts player end end end
print_name_and_health(player)
click to toggle source
# File lib/jkims_game/game.rb, line 54 def print_name_and_health(player) "#{player.name} (#{player.health})" end
print_stats()
click to toggle source
# File lib/jkims_game/game.rb, line 62 def print_stats @players.sort.each do |player| puts "#{player.name.ljust(20, ".")}#{player.score.to_s.rjust(3)}" end puts "\n#{@title} Statistics:" strong, wimpy = @players.partition { |player| player.strong? } puts "\n#{strong.length} strong players:" strong.each { |s| puts print_name_and_health(s)} puts "\n#{wimpy.length} wimpy players:" wimpy.each { |w| puts print_name_and_health(w)} @players.each do |player| puts "#{player.name}'s total points:" player.each_found_treasure { |treasure| puts "#{treasure.points} total #{treasure.name} points"} puts "#{player.points} grand total points" end puts "#{total_points} total points from treasures found" end
save_high_scores(filename = "high_scores.txt")
click to toggle source
# File lib/jkims_game/game.rb, line 21 def save_high_scores(filename = "high_scores.txt") File.open(filename, "w") do |file| file.puts "#{@title} High Scores:" @players.sort.each do |player| file.puts "#{player.name.ljust(20, ".")}#{player.score.to_s.rjust(3)}" end end end
total_points()
click to toggle source
# File lib/jkims_game/game.rb, line 58 def total_points @players.reduce(0) {|sum, player| sum + player.points} end