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.



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

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

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



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

def add_player(player)
  @players << player
end

#load_players(file) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/studio_game/game.rb', line 19

def load_players(file)
  File.readlines(file).each do |line|
    name, health = line.split(',')
    player = Player.new(name, Integer(health))
    add_player(player)
  end
end

#play(rounds) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/studio_game/game.rb', line 92

def play(rounds)    
  print_game_intro
  print_treasure_menu
  
  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


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

def print_game_intro
  puts "\nThere are #{@players.size} players in #{@title}."
  @players.each do |player|
    puts player
  end
end


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

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


47
48
49
50
51
52
# File 'lib/studio_game/game.rb', line 47

def print_high_scores
  puts "\n#{@title}'s high scores: "    
  @players.sort.each do |p| 
    puts "#{p.name.ljust(20, ".")} #{p.score}"
  end
end


69
70
71
72
73
74
75
# File 'lib/studio_game/game.rb', line 69

def print_stats
  puts "\n#{@title}'s statistics: "
  print_strong_wimpy
  print_game_treasure_totals
  print_treasure_point_totals
  print_high_scores
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/studio_game/game.rb', line 31

def print_strong_wimpy
  strong, wimpy = @players.partition { |p| p.strong? }
  if strong.any?
    puts "\n#{strong.size} strong players"
    strong.each do |p|
      puts "#{p.name} (#{p.health})"
    end
  end
  if wimpy.any? 
    puts "\n#{wimpy.size} wimpy players"
    wimpy.each do |p|
      puts "#{p.name} (#{p.health})"
    end
  end
end


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

def print_treasure_menu
  treasures = TreasureBox::TREASURES
  puts "\nThere are #{treasures.size} treasures in the treasure box."
  treasures.each do |treasure|
    puts "A #{treasure.name} is worth #{treasure.points} points"
  end
end


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

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

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



108
109
110
111
112
113
114
115
# File 'lib/studio_game/game.rb', line 108

def save_high_scores(to_file="high_scores.txt")
  File.open(to_file, "w") do |file|
    file.puts "#{@title} High Scores:"      
    @players.sort.each do |p| 
      file.puts "#{p.name.ljust(20, ".")} #{p.score}"
    end
  end
end

#total_pointsObject



27
28
29
# File 'lib/studio_game/game.rb', line 27

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