Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Includes:
- Auditable
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
- #high_score_entry(player) ⇒ Object
- #high_scores ⇒ Object
-
#initialize(name) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file = 'players.csv') ⇒ Object
- #play(rounds = 1) ⇒ Object
- #players_status ⇒ Object
- #print_next_round(round) ⇒ Object
- #print_separator ⇒ Object
- #print_stats ⇒ Object
- #process_roll(roll, player) ⇒ Object
- #roll_die ⇒ Object
- #save_high_scores(to_file = 'high_scores.txt') ⇒ Object
- #sorted_players ⇒ Object
Methods included from Auditable
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/studio_game/game.rb', line 9 def name @name end |
#players ⇒ Object (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_scores ⇒ Object
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_status ⇒ Object
60 61 62 |
# File 'lib/studio_game/game.rb', line 60 def players_status @players.each { |player| puts player } end |
#print_next_round(round) ⇒ Object
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 |
#print_separator ⇒ Object
48 49 50 51 52 |
# File 'lib/studio_game/game.rb', line 48 def print_separator puts 30.times { print '*' } print "\n\n" end |
#print_stats ⇒ Object
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_die ⇒ Object
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_players ⇒ Object
64 65 66 |
# File 'lib/studio_game/game.rb', line 64 def sorted_players @players.sort_by(&:score).reverse end |