Class: StudioGame::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/studio_game/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Game

Returns a new instance of Game.



14
15
16
17
# File 'lib/studio_game/game.rb', line 14

def initialize(title)
  @players = []
  @title = title
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/studio_game/game.rb', line 12

def name
  @name
end

Instance Method Details

#add_player(player) ⇒ Object



19
20
21
# File 'lib/studio_game/game.rb', line 19

def add_player(player)
  @players << player
end

#high_score_entry(player) ⇒ Object



96
97
98
99
# File 'lib/studio_game/game.rb', line 96

def high_score_entry(player)
  formatted_name = player.name.ljust(20, '.')
  "#{formatted_name} #{player.score}"
end

#load_players(file) ⇒ Object



81
82
83
84
85
# File 'lib/studio_game/game.rb', line 81

def load_players(file)
  File.readlines(file).each do |line|
    add_player(Player.from_csv(line))
  end
end

#play(rounds) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/studio_game/game.rb', line 23

def play(rounds)
  puts "There are #{@players.size} players in #{@name}:"
  treasures = TreasureTrove::TREASURES

  1.upto(rounds) do |round|
    puts "Round #{round}"
    @players.each do |player|
      GameTurn.take_turn(player)
      puts player
    end
  end

  puts "\nThere are #{treasures.size} treasures to be found:"

  treasures.each do |treasure|
    puts "A #{treasure.name} is worth #{treasure.points} points"
  end
end


42
43
44
# File 'lib/studio_game/game.rb', line 42

def print_name_and_health(player)
  puts "#{player.name} #{player.health}"
end


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/studio_game/game.rb', line 50

def print_stats
  strong, wimpy = @players.partition(&:strong?)

  puts "\n#{@title} Statistics:"

  puts "\n#{strong.size} strong players:"
  strong.each do |s|
    print_name_and_health(s)
  end

  puts "\n#{wimpy.size} wimpy players:"
  wimpy.each do |w|
    print_name_and_health(w)
  end

  puts 'Knuckleheads High Scores:'
  @players.sort.each do |p|
    puts high_score_entry(p)
  end

  puts "\n#{total_points} total points from treasures found"

  @players.sort.each do |player|
    puts "\n#{player.name}'s point totals:"
    player.each_found_treasure do |treasure|
      puts "#{treasure.points} total #{treasure.name} points"
    end
    puts "#{player.points} grand total points"
  end
end

#save_high_scores(to_file = 'high_scores.txt') ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/studio_game/game.rb', line 87

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 |p|
      file.puts high_score_entry(p)
    end
  end
end

#total_pointsObject



46
47
48
# File 'lib/studio_game/game.rb', line 46

def total_points
  @players.each.reduce(0) { |acc, player| acc + player.points }
end