Class: StudioGame::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Auditable

#audit

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

#healthObject

Returns the value of attribute health.



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

def health
  @health
end

#playersObject (readonly)

Returns the value of attribute players.



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

def players
  @players
end

#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



36
37
38
# File 'lib/studio_game/game.rb', line 36

def add_player(player)
  @players << player
end

#load_players(file_name) ⇒ Object



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

def load_players(file_name)
  File.readlines(file_name, chomp: true).each do |line|
    player = Player.from_csv(line)
    add_player(player)
  end
rescue Errno::ENOENT
  puts "No file named '#{file_name}' found!"
  exit 1
end

#play(rounds = 1) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/studio_game/game.rb', line 46

def play(rounds = 1)
  puts "Let's play #{@title}!\n"

  puts "\nPossible treasures to collect:"
  puts TreasureTrove.treasure_items

  puts "\nBefore playing:"
  puts @players

  1.upto(rounds) do |round|
    puts "\nRound #{round}:"
    @players.each do |player|
      number_rolled = roll_die

      if number_rolled < 3
        player.drain
        puts "#{player.name} got drained 😩"
      elsif number_rolled < 5
        puts "#{player.name} got skipped"
      else
        player.boost
        puts "#{player.name} got boosted 😁"
      end

      treasure = TreasureTrove.random_treasure
      player.found_treasure(treasure.name, treasure.points)
      puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points"
    end
  end
end


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/studio_game/game.rb', line 81

def print_stats
  puts "\nWinner Takes All Game Stats:"
  puts "-" * 30
  puts sorted_players
  print "\n"
  @players.each do |player|
    puts "#{player.name}'s treasure point totals:"
    player.found_treasures.each do |name, points|
      puts "#{name}: #{points}"
    end
    puts "Total: #{player.points}\n\n"
  end
  puts "High scores:"
  sorted_players.each do |player|
    print "#{player.name}".ljust(15, ".")
    puts "   #{player.score}"
  end
end

#roll_dieObject



40
41
42
43
44
# File 'lib/studio_game/game.rb', line 40

def roll_die
  number = rand(1..6)
  audit(number)
  number
end

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



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

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.print "#{player.name}".ljust(15, ".")
      file.puts "   #{player.score}"
    end
  end
end

#sorted_playersObject



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

def sorted_players
  @players.sort_by { |player| player.score}.reverse
end