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.



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

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

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



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

def add_player(player)
  @players.push(player)
end

#high_score_entry(player) ⇒ Object



94
95
96
97
# File 'lib/studio_game/game.rb', line 94

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

#load_players(filename) ⇒ Object



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

def load_players(filename)
  CSV.foreach(filename) { |line|
    add_player( Player.new(line[0],line[1].to_i) )
  }
end

#play(num_rounds) ⇒ Object



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

def play(num_rounds)
  puts "There are #{@players.size} players in #{@title}:"
  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(num_rounds) { |n|
    if block_given?
      break if yield
    end
    puts "\nRound #{n}"
    @players.each do |player|
      GameTurn.take_turn(player)
    end
  }
end


75
76
77
# File 'lib/studio_game/game.rb', line 75

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


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/studio_game/game.rb', line 46

def print_stats
  strong_players, wimpy_players = @players.partition {|p| p.strong?}

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

  puts "\n#{strong_players.size} strong players:"
  strong_players.each { |player|
    print_name_and_health(player)
  }

  puts "\n#{wimpy_players.size} wimpy players:"
  wimpy_players.each { |player|
    print_name_and_health(player)
  }

  puts "\n#{@title} High Scores:"
  @players.sort.each { |player|
    puts high_score_entry(player)
  }

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

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

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



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

def save_high_scores(filename="high_scores.txt")
  File.open(filename, "w") { |file|
    file.puts "#{@title} High Scores:"
    @players.sort.each { |player|
      file.puts high_score_entry(player)
    }
  }
end

#total_pointsObject



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

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