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.



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

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

Instance Attribute Details

#playersObject (readonly)

Returns the value of attribute players.



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

def players
  @players
end

#titleObject

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



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

def add_player(player)
  @players << player
end

#high_score_entry(player) ⇒ Object



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

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

#high_score_titleObject



61
62
63
# File 'lib/studio_game/game.rb', line 61

def high_score_title
  "\n#{title} High Scores:"
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



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

def play(rounds)
  puts "There are #{@players.size} players in #{@title}:"


  @players.each do |player|
    puts player
  end

  treasures = TreasureTrove::TREASURES
  puts "\nThere are #{treasures.length} treasures to be found:"
  treasures.each { |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" }

  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


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

def print_high_scores
  puts high_score_title
  players.sort.each do |player|
    puts high_score_entry(player)
  end
end


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

def print_players
  players.each do |player|
    puts player
  end
end


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/studio_game/game.rb', line 86

def print_stats
  print_high_scores
  save_high_scores

  strong_players, wimpy_players = @players.partition {|player| player.strong? }
   
  puts "\n#{@title} Statistics:"

  # print out strong players
  puts "\n#{strong_players.size} strong players:"
  strong_players.each { |player| puts player.print_name_and_health }
   
  # print out wimpy players
  puts "\n#{wimpy_players.size} wimpy players:"
  wimpy_players.each { |player| puts player.print_name_and_health }

  players.sort.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 "#{player.points} grand total points"
  end

  puts "\n#{total_points} collected from treasure found."

end

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



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

def save_high_scores(file_name="high_scores.txt")
  File.open(file_name, "w") do |file|
    file.puts high_score_title
    players.sort.each do |player|
      file.puts high_score_entry(player)
    end
  end
end

#total_pointsObject



33
34
35
# File 'lib/studio_game/game.rb', line 33

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