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

Returns a new instance of Game.



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

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#playersObject (readonly)

Returns the value of attribute players.



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

def players
  @players
end

Instance Method Details

#add_player(player) ⇒ Object



38
39
40
# File 'lib/studio_game/game.rb', line 38

def add_player(player)
 @players << player
end

#high_score_entry(player) ⇒ Object



23
24
25
26
# File 'lib/studio_game/game.rb', line 23

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

#load_players(from_file) ⇒ Object



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

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

#play(rounds) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/studio_game/game.rb', line 42

def play(rounds)
 puts "#{@name}'s game:"
 puts @players.sort

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

 treasures.each do |t|
  puts "#{t.name} has #{t.points} points"
 end

 1.upto(rounds) do |round|
  puts "\nRound number #{round}:"
  @players.each do |player|
   GameTurn.take_turn(player)
   player.found_treasure(TreasureTrove.random)
   puts player
  end
 end
end


103
104
105
# File 'lib/studio_game/game.rb', line 103

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


69
70
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 69

def print_stats
 puts "\n#{@name}'s status:"

 puts "#{total_points} total points earned"

 @players.sort.each do |p|
  puts "\n#{p.name}'s total points:"
  p.each_found_treasure do |treasure|
    puts "#{treasure.points} total #{treasure.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 "\n#{@name} High Scores:"
 @players.sort.each do |player|
  puts high_score_entry(player)
 end
end

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



29
30
31
32
33
34
35
36
# File 'lib/studio_game/game.rb', line 29

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

#to_sObject



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

def to_s
 puts "There are #{@players.length} players in Knuckleheads:"  
end

#total_pointsObject



63
64
65
66
67
# File 'lib/studio_game/game.rb', line 63

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