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.



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

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

Instance Attribute Details

#playersObject (readonly)

Returns the value of attribute players.



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

def players
  @players
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
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

#load_players(from_file) ⇒ Object



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

def load_players(from_file)
 File.readlines(from_file, chomp: true).each do |line|
    player = Player.from_csv(line)
    add_player(player)
  end
rescue Errno::ENOENT
  puts "Whoops, #{from_file} not found"
end

#play(rounds = 1) ⇒ Object



67
68
69
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
# File 'lib/studio_game/game.rb', line 67

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

  puts "Treasures available in the game:"
  puts TreasureTrove.treasure_items

  puts "\nBefore playing:"
  puts @players

  1.upto(rounds) do |round|
    puts "\nRound #{round}:"

    @players.each do |player|
      case roll_die
      when 1..2
        player.drain
        puts "#{player.name} got drained 😩"
      when 3..4
        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

  puts "\nAfter playing:"
  puts @players
end


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

def print_stats
  puts "#{@title} Game Stats:"
  puts "-" * 30

  puts "Players' Scores:"
  sorted_players.each do |player|
    puts "#{player.name.ljust(20, '.')} #{player.score.to_s.rjust(5)}"
  end
  

  @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|
    name = player.name.ljust(20, ".")
    score = player.score.to_s.rjust(5)
    puts "#{name}#{score}"
  end
end

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



26
27
28
29
30
31
32
33
34
35
# 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:"
    
    # Sort players by score in descending order
    sorted_players.each do |player|
      file.puts "#{player.name.ljust(20, '.')} #{player.score.to_s.rjust(5)}"
    end
  end
end

#sorted_playersObject



63
64
65
# File 'lib/studio_game/game.rb', line 63

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