Class: StudioGame::Game

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

Overview

Provides methods for working with the Game object

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Auditable

#audit

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

#playersObject (readonly)

Returns the value of attribute players.



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

def players
  @players
end

#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



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

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

#load_players(from_file) ⇒ Object

rubocop:enable Metrics/AbcSize



65
66
67
68
69
70
71
72
73
# File 'lib/studio_game/game.rb', line 65

def load_players(from_file)
  CSV.foreach(from_file) do |row|
    player = Player.from_csv(row)
    add_player(player)
  end
rescue Errno::ENOENT
  puts "Whoops, #{from_file} not found!"
  exit 1
end

#play(rounds = 1) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/studio_game/game.rb', line 29

def play(rounds = 1)
  display_game_start_message

  display_players_before_playing

  play_rounds(rounds)

  display_players_after_playing
end

rubocop:disable Metrics/AbcSize



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/studio_game/game.rb', line 44

def print_stats
  puts "\n Game Stats:"
  puts '-' * 30

  puts sorted_players

  @players.each do |player|
    puts "\n#{player.name}'s treasure point totals:"
    player.found_treasures.each do |name, points|
      puts "#{name}: #{points}"
    end
    puts "total: #{player.points}"
  end

  puts "\nHigh Scores:"
  sorted_players.each do |player|
    puts high_score_entry(player)
  end
end

#roll_dieObject



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

def roll_die
  number = [1, 1, 2, 5, 6, 6].sample
  audit(number)
  number
end

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



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

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

#sorted_playersObject



39
40
41
# File 'lib/studio_game/game.rb', line 39

def sorted_players
  @players.sort_by(&:score).reverse
end