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 = "Winner Winner Chicken Dinner!") ⇒ Game

Returns a new instance of Game.



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

def initialize(title = "Winner Winner Chicken Dinner!")
  @title = title
  @players = []
end

Instance Attribute Details

#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



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

def add_player(player)
  @players << player
end

#high_score_entry(player) ⇒ Object



107
108
109
110
111
# File 'lib/studio_game/game.rb', line 107

def high_score_entry(player)
  name = player.name.ljust(20, ".")
  score = player.score.round.to_s.rjust(5)
  "#{name}#{score}"
end

#load_players(filename) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/studio_game/game.rb', line 88

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

#play(rounds = 1) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/studio_game/game.rb', line 51

def play(rounds = 1)
  puts "\nLet's play #{@title}!"
  
  puts "\nThe following treasures can be found:"
  puts TreasureTrove.treasure_items
  
  puts "\nBefore playing:"
  puts @players
  
  1.upto(rounds) do |round|
    puts "\nRound #{round}:"

    @players.each do |player| 
      
      number_rolled = roll_dice
      case number_rolled
      when 1..3
        player.drain
        puts "#{player.name} got drained 😩"
      when 4..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
    
    puts "\nAfter playing:"
    puts @players
  end
end


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/studio_game/game.rb', line 26

def print_stats
  puts "\n Game Stats:"
  puts "-" * 30
  puts sorted_players_by_score
  sorted_players_by_treasure_points.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_by_score.each do |player|
    puts high_score_entry(player)
  end
end

#roll_diceObject



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

def roll_dice
  roll = rand(1..6)
  audit(roll)
  roll
end

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



98
99
100
101
102
103
104
105
# File 'lib/studio_game/game.rb', line 98

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

#sorted_players_by_scoreObject



43
44
45
# File 'lib/studio_game/game.rb', line 43

def sorted_players_by_score
  @players.sort_by {|p| p.score}.reverse
end

#sorted_players_by_treasure_pointsObject



47
48
49
# File 'lib/studio_game/game.rb', line 47

def sorted_players_by_treasure_points
  @players.sort_by {|p| p.points}.reverse
end