Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#players ⇒ Object
Returns the value of attribute players.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
- #high_score_entry(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #print_stats_sub_title(pls, str_or_wmp_text) ⇒ Object
- #save_high_scores(to_file = "high_scores.txt") ⇒ Object
- #to_s ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
13 14 15 16 |
# File 'lib/studio_game/game.rb', line 13 def initialize(title) @title = title.capitalize @players = [] end |
Instance Attribute Details
#players ⇒ Object
Returns the value of attribute players.
12 13 14 |
# File 'lib/studio_game/game.rb', line 12 def players @players end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
11 12 13 |
# File 'lib/studio_game/game.rb', line 11 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
43 44 45 |
# File 'lib/studio_game/game.rb', line 43 def add_player(player) @players << player end |
#high_score_entry(player) ⇒ Object
27 28 29 30 |
# File 'lib/studio_game/game.rb', line 27 def high_score_entry(player) formatted_name = player.name.ljust(20, '.') "#{formatted_name} #{player.score}" end |
#load_players(from_file) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/studio_game/game.rb', line 32 def load_players(from_file) CSV.foreach(from_file) do |row| player = Player.new(row[0], row[1].to_i) add_player(player) end end |
#play(rounds) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/studio_game/game.rb', line 47 def play(rounds) puts self @players.each {|pl| puts pl} TreasureTrove.print_treasures 1.upto(rounds) do |round| if block_given? #yielded = yield total_points # longer #break if yielded break if yield total_points # more elegent end puts "\nRound: #{round}:" @players.each do |pl| GameTurn.take_turn(pl) puts pl end end end |
#print_name_and_health(player) ⇒ Object
77 78 79 |
# File 'lib/studio_game/game.rb', line 77 def print_name_and_health(player) puts "#{player.name} (#{player.health})\n" end |
#print_stats ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/studio_game/game.rb', line 81 def print_stats puts "\n#{title} Statistics:\n" strong_players, wimpy_players = @players.partition {|pl| pl.strong?} print_stats_sub_title(strong_players, 'strong') strong_players.sort! {|x, y| y.health <=> x.health} strong_players.each { |player| print_name_and_health(player)} print_stats_sub_title(wimpy_players, 'wimpy') wimpy_players.sort! {|x, y| y.health <=> x.health} wimpy_players.each { |player| print_name_and_health(player)} puts "\n#{title} High Scores:\n" @players.sort.each do |pl| puts(high_score_entry(pl)) end puts "\n#{total_points} total points from treasures found\n" @players.each do |pl| puts "\n#{pl.name}'s point totals:" puts "#{pl.points} grand total points" end puts "\npoint totals" @players.sort.each do |pl| puts "\n#{pl.name}'s point totals per treasure:" pl.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end puts "#{pl.points} grand total points" end end |
#print_stats_sub_title(pls, str_or_wmp_text) ⇒ Object
71 72 73 74 75 |
# File 'lib/studio_game/game.rb', line 71 def print_stats_sub_title(pls, str_or_wmp_text) pls.size == 1 ? (plural_text = "player") : (plural_text = "players") pls.size == 0 ? (colon_text = ".") : (colon_text = ":") puts "\n#{pls.size} #{str_or_wmp_text} #{plural_text}#{colon_text}" end |
#save_high_scores(to_file = "high_scores.txt") ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/studio_game/game.rb', line 18 def save_high_scores(to_file="high_scores.txt") File.open(to_file, "w") do |file| file.puts "#{@title} High Scores" @players.sort.each do |pl| file.puts(high_score_entry(pl)) end end end |
#to_s ⇒ Object
39 40 41 |
# File 'lib/studio_game/game.rb', line 39 def to_s "\n\n**** There are #{@players.size} players in #{@title} ****\n" end |
#total_points ⇒ Object
67 68 69 |
# File 'lib/studio_game/game.rb', line 67 def total_points @players.reduce(0){ |sum, pl| sum + pl.points } end |