Class: StudioGame::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Game

Returns a new instance of Game.



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

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

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



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

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

#high_scores_entry(player) ⇒ Object



53
54
55
# File 'lib/studio_game_secor/game.rb', line 53

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

#load_players(from_file) ⇒ Object



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

def load_players(from_file)
  File.readlines(from_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
41
42
43
# File 'lib/studio_game_secor/game.rb', line 23

def play(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 do |t|
    puts "A #{t.name} is worth #{t.points} points."
  end

  1.upto(rounds) do |round|
    if block_given?
      break if yield
    end
    puts "\nRound #{round}:"
    @players.each do |player|
      GameTurn.take_turn(player)
      # puts player
    end
  end
end


45
46
47
# File 'lib/studio_game_secor/game.rb', line 45

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


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/studio_game_secor/game.rb', line 57

def print_stats
  puts "\n#{@title} Statistics:"
  strong, wimpy = @players.partition { |p| p.strong? }
  puts "\n#{strong.length} strong players:"
  strong.each { |p| print_name_and_health(p)}
  puts "\n#{wimpy.length} wimpy players:"
  wimpy.each { |p| print_name_and_health(p)}
  puts "\n#{@title} High Scores:"
  # sorted_players = @players.sort { |a, b| b.score <=> a.score }
  @players.sort.each { |p| puts high_scores_entry(p)}

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

  puts "\nTotal treasure points found: #{total_points}"
end

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



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

def save_high_scores(to_file="high_scores.txt")
  File.open(to_file, 'w') do |file|
  file.puts "\n#{@title} High Scores:"
  @players.sort.each { |p| file.puts high_scores_entry(p)}
  end
end

#total_pointsObject



49
50
51
# File 'lib/studio_game_secor/game.rb', line 49

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