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(mylistname) ⇒ Game

Returns a new instance of Game.



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

def initialize (mylistname)
  @name = mylistname.capitalize
  @players = []
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/studio_game/game.rb', line 8

def name
  @name
end

#playersObject (readonly)

Returns the value of attribute players.



9
10
11
# File 'lib/studio_game/game.rb', line 9

def players
  @players
end

Instance Method Details

#add_player(player) ⇒ Object



32
33
34
# File 'lib/studio_game/game.rb', line 32

def add_player(player)
  @players << player
end

#high_score_entry(player) ⇒ Object



66
67
68
69
# File 'lib/studio_game/game.rb', line 66

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

#load_players(from_file) ⇒ Object



16
17
18
19
20
# File 'lib/studio_game/game.rb', line 16

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

#play(rounds) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/studio_game/game.rb', line 36

def play(rounds)
  puts "#{@name}'s game:"
  puts @players.sort
  
  treas = TreasureTrove::TREASURES
  puts "\nThere are #{treas.size} treasures to be found."
  
  treas.each do |t|
    puts "#{t.name} is worth #{t.points} points"
  end
  
  1.upto(rounds) do |round|
    puts "\nRound number #{round}:"
    @players.each do |player|
      GameTurn.take_turn(player)
      puts player
    end
  end
end


56
57
58
# File 'lib/studio_game/game.rb', line 56

def print_name_and_health(pla)
  puts "#{pla.name} has a health of #{pla.health}"
end


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/studio_game/game.rb', line 71

def print_stats
  puts "\n#{@name}'s Stats:"
  
  puts "#{total_points} total points earned"
  @players.sort.each do |p|
    puts "\n#{p.name}'s point totals:"
    p.each_found_treasure do |t|
      puts "#{t.points} total #{t.name} points"
    end
    puts "#{p.points} grand total points"
  end
  
  strong, weak = @players.partition { |player| player.strong? }
  
  puts "\nStrong:"
  strong.each {|p| self.print_name_and_health(p)}
#    puts strong.sort
  
  puts "\nWeak:"
  weak.each {|p| self.print_name_and_health(p)}
#    puts weak.sort
  
  puts "\nHigh Scores"
  @players.sort.each do |p|
    puts high_score_entry(p)
  end
end

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



22
23
24
25
26
27
28
29
30
# File 'lib/studio_game/game.rb', line 22

def save_high_scores(to_file="high_scores.txt")
  File.open(to_file, "w") do |file|
    file.puts "#{@name} High Scores:"

    @players.sort.each do |p|
      file.puts high_score_entry(p)
    end
  end
end

#to_sObject



99
100
101
# File 'lib/studio_game/game.rb', line 99

def to_s
  "There are #{@players.size} players in #{name}."
end

#total_pointsObject



60
61
62
63
64
# File 'lib/studio_game/game.rb', line 60

def total_points
  @players.reduce(0) do |sum, player|
    sum + player.points
  end
end