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

Returns a new instance of Game.



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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add_player(player) ⇒ Object



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

def add_player(player) 
  @players << player
end

#formatted_entry(player) ⇒ Object

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



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

def formatted_entry(player)
   player.name.ljust(30, ".") + " #{player.score}" 
end

#health_statement(player) ⇒ Object



52
53
54
# File 'lib/studio_game/game.rb', line 52

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

#load_players(filename) ⇒ Object



67
68
69
70
71
72
# File 'lib/studio_game/game.rb', line 67

def load_players(filename) 
  File.readlines(filename).each do |line|
    player = Player.from_csv(line)
    add_player(player)
  end
end

#play(rounds) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/studio_game/game.rb', line 15

def play(rounds)
  puts "There are #{@players.size} players in #{@name}:"
  puts @players
  treasures = TreasureTrove::TREASURES
  puts "\nThere are #{treasures.size} treasures to be found:"
  treasures.each {|t| puts "A #{t.name} is worth #{t.points} points."}
  1.upto(rounds) do |round|
    if block_given?
      break if yield
    end
    puts "\nRound: #{round}:"
      @players.each do |p|
        GameTurn.take_turn(p)
        puts p
      end
  end
end


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

def print_highscores
  puts "\n#{@name} High Scores:"
  @players.sort.each do |player|
    puts formatted_entry(player)
  end
end


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/studio_game/game.rb', line 33

def print_stats
  winners, losers = @players.partition {|player| player.strong?}
  puts "\n\n#{winners.size} strong players"
  winners.each {|winner| puts health_statement winner}
  puts "\n#{losers.size} wimpy players"
  losers.each {|loser| puts health_statement loser}
  @players.each {|player| puts "\n#{player.name}'s point totals: \n#{player.points} grand total points"}
  puts "\n#{total_points} total points from treasures found"
  
  @players.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 "\n#{player.points} grand total points"
  end

end

#save_high_scores(filename = "highscores.txt") ⇒ Object



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

def save_high_scores(filename="highscores.txt")
  File.open(filename, "w") do |file|
    file.puts "Knuckleheads high scores:"
    @players.sort.each do |player|
      file.puts formatted_entry(player)
    end
  end
end

#total_pointsObject



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

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