Class: StudioGame::Game

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

Overview

game class that includes the title of the game where players then get assigned to that game

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Game

Returns a new instance of Game.



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

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

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



25
26
27
# File 'lib/studio_game/game.rb', line 25

def add_player(player)
  @players << player
end

#high_score_entry(player) ⇒ Object



103
104
105
106
# File 'lib/studio_game/game.rb', line 103

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

#load_players(from_file) ⇒ Object



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

def load_players(from_file)
  File.readlines(from_file).each do |line|
    add_player(Player.from_csv(line))
  end
end

#play(rounds) ⇒ Object



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

def play(rounds)
  puts "\nThere are #{@players.length} players in #{@title} with #{rounds} rounds:"

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

  treasures = TreasureTrove::TREASURES

  puts "\nThere are #{treasures.length} treasures available:"

  treasures.each do |treasure|
    puts "A #{treasure.name} is worth #{treasure.points} points."
  end
end

#player_name_health(player) ⇒ Object



51
52
53
# File 'lib/studio_game/game.rb', line 51

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


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/studio_game/game.rb', line 55

def print_stats
  strong_players, wimpy_players = @players.partition(&:strong?)

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

  puts "\n#{strong_players.length} strong players:"
  strong_players.each do |player|
    player_name_health(player)
  end

  puts "\n#{wimpy_players.length} wimpy players:"
  wimpy_players.each do |player|
    player_name_health(player)
  end

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

  puts "\nPlayer treasure point totals:"
  @players.sort.each do |player|
    puts "\n#{player.name.center(20, '_')}"
    player.each_found_treasure do |treasure|
      formatted_treasure_name = treasure.name.to_s.capitalize.ljust(15)
      puts "#{formatted_treasure_name}: #{treasure.points}"
    end
    formatted_name = 'Total Points'.ljust(15)
    puts '-'.center(20, '-')
    puts "#{formatted_name}: #{player.points}"
  end

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

#sav_high_scores(file_name = 'high_scores.txt') ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/studio_game/game.rb', line 94

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

#total_pointsObject



90
91
92
# File 'lib/studio_game/game.rb', line 90

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