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(name) ⇒ Game

Returns a new instance of Game.



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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#playersObject (readonly)

Returns the value of attribute players.



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

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



68
69
70
71
72
73
74
75
76
# File 'lib/studio_game/game.rb', line 68

def high_score_entry(player)
  # Count emojis (they take 2 visual columns but count as 1 char)
  emoji_count = player.name.scan(/\p{Emoji}/).size
  # Adjust padding to account for emoji visual width
  padding = 30 - emoji_count
  name = player.name.ljust(padding, '.')
  score = player.score.round.to_s.rjust(5)
  "#{name}#{score}"
end

#high_scoresObject



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

def high_scores
  puts "\n   #{@title} HIGH SCORES    "
  sorted_players.each do |player|
    puts high_score_entry(player)
  end
end

#load_players(from_file = 'players.csv') ⇒ Object



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

def load_players(from_file = 'players.csv')
  CSV.foreach(from_file) do |line|
    add_player(Player.from_csv(line))
  end
rescue StandardError
  puts "The file #{from_file} doesn't exist"
  exit 1
end

#play(rounds = 1) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/studio_game/game.rb', line 108

def play(rounds = 1)
  1.upto(rounds) do |round|
    print_next_round(round)
    puts "๐Ÿ”ฅ๐Ÿ”ฅ Let The Game of #{@name} Begin! ๐Ÿ“ฏ๐Ÿ“ฏ\n\n"

    puts TreasureTrove.treasure_items

    puts "\nPlayers, present yourselves!"
    players_status
    puts

    @players.each do |player|
      roll = roll_die
      process_roll(roll, player)
      treasure = TreasureTrove.random_treasure
      puts "#{player.name} found a #{treasure.name}" \
      " worth #{treasure.points} points"
      player.found_treasure(treasure.name, treasure.points)
    end

    puts "\nPlayers, state your status!!"
    players_status
  end
  print_stats
end

#players_statusObject



60
61
62
# File 'lib/studio_game/game.rb', line 60

def players_status
  @players.each { |player| puts player }
end


54
55
56
57
58
# File 'lib/studio_game/game.rb', line 54

def print_next_round(round)
  print_separator
  puts "BEGIN ROUND #{round}"
  print_separator
end


48
49
50
51
52
# File 'lib/studio_game/game.rb', line 48

def print_separator
  puts
  30.times { print '*' }
  print "\n\n"
end


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/studio_game/game.rb', line 94

def print_stats
  puts "\n -.-.- #{@name}: FINAL GAME STATS -.-.-"
  puts sorted_players
  players.each do |player|
    puts "#{player.name} found:"
    player.found_treasures.each do |treasure, points|
      print "#{treasure} -> #{points}, "
    end
    print "Total points -> #{player.found_treasures.values.sum}."
    puts
  end
  high_scores
end

#process_roll(roll, player) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/studio_game/game.rb', line 35

def process_roll(roll, player)
  case roll
  when (1..2)
    player.drain
    puts "#{player.name} got drained โฌ"
  when (3..4)
    puts "#{player.name} got skipped ๐Ÿคจ"
  when (5..6)
    player.boost
    puts "#{player.name} got boosted โซ"
  end
end

#roll_dieObject



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

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

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



85
86
87
88
89
90
91
92
# File 'lib/studio_game/game.rb', line 85

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



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

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