Class: StudioGame::Game

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

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.capitalize
  @players = []
end

Instance Method Details

#add_player(player) ⇒ Object



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

def add_player(player)
  @players << player
end

#get_total_treasuresObject



33
34
35
36
37
38
39
40
# File 'lib/studio_game/game.rb', line 33

def get_total_treasures
  @players.each do |player|
    puts "\n#{player.name} found:"
    player.found_treasures.each do |key, value|
      puts "-\tTotal #{key} points: #{value}"
    end
  end
end

#grand_totalObject



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

def grand_total
  puts "\n#{@title} Grand Total:"
  total = 0
  @players.each { |player| total += player.points}
  puts "#{total}"
end

#high_scoresObject



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

def high_scores
  puts "\n#{@title} Scores:\n"
  @players.sort.each { |player| puts "#{player.name.ljust(30,'.')}#{player.points}"}
end

#load_players(filename = 'players.csv') ⇒ Object



42
43
44
45
46
47
48
# File 'lib/studio_game/game.rb', line 42

def load_players(filename='players.csv')
  File.readlines(filename).each do | line |
    # name, health = line.split(",")
    # add_player(Player.new(name, Integer(health)))
    add_player(Player.player_csv(line))
  end
end

#partition_playersObject



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

def partition_players
  @strong, @wimpy = @players.partition { |player| player.strong? }
end

#play(round_number) ⇒ Object



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

def play(round_number)
  puts "#{@title}: #{@players.length} players in this game.\n"
  treasures = TreasureTrove::TREASURES
  puts "There are #{treasures.length} treasures to be found:"
  treasures.each { | treasure | puts "A #{treasure.name} is worth #{treasure.points}"}
  1.upto(round_number) do |number|
    puts "\nGame round:#{number}"
    @players.each { |player| GameTurn.take_turn(player) }
  end
end


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/studio_game/game.rb', line 70

def print_stats
  puts "\n#{@title} game stats:"
  partition_players

  # Prints points by treasure
  puts "\nPoints by treasure type:"
  # get_total_treasures

  # Uncomment to use custom iterator in the Player class 'get_total_treasures'
  @players.sort.each do |player|
    puts "\n#{@title}'s treasure totals:"
    player.get_total_treasures do |treasure|
      puts "#{treasure.points} total #{treasure.name}"
    end
  end

  # Prints strong players
  puts "\n#{@strong.length} strong players:\n"
  @strong.each { |player| puts player.format_name }

  # Prints wimpy players
  puts "\n#{@wimpy.length} weak players:\n"
  @wimpy.each { |player| puts player.format_name }

  # Prints High Scores
  high_scores

  # Prints Grand total
  grand_total
end

#save_high_scores(filename = 'recent_high_scores.txt') ⇒ Object



50
51
52
53
54
55
56
# File 'lib/studio_game/game.rb', line 50

def save_high_scores(filename='recent_high_scores.txt')
  File.open(filename, 'w') do |file|
    @players.sort.each do |player|
      file.puts player.format_name
    end
  end
end