Class: Studio_game::Game
- Inherits:
-
Object
- Object
- Studio_game::Game
- Includes:
- Auditable
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#game_name ⇒ Object
readonly
Returns the value of attribute game_name.
-
#players_ ⇒ Object
Returns the value of attribute players_.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
- #game_round_count ⇒ Object
- #high_score_entry(player_obj, b = "yes") ⇒ Object
-
#initialize(game_name) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file) ⇒ Object
- #play_the_game(rounds = 1) ⇒ Object
- #print_score ⇒ Object
- #print_stats ⇒ Object
-
#roll ⇒ Object
not needed.
- #roll_die ⇒ Object
- #round_count ⇒ Object
- #save_high_scores(to_file = "high_scores.txt") ⇒ Object
- #sorted_players ⇒ Object
- #to_s ⇒ Object
Methods included from Auditable
Constructor Details
#initialize(game_name) ⇒ Game
Returns a new instance of Game.
11 12 13 14 15 16 17 |
# File 'lib/studio_game/game.rb', line 11 def initialize(game_name) @game_name = game_name @game_round = 0 @players_ = [] @test_string = '' @round_count = 0 end |
Instance Attribute Details
#game_name ⇒ Object (readonly)
Returns the value of attribute game_name.
9 10 11 |
# File 'lib/studio_game/game.rb', line 9 def game_name @game_name end |
#players_ ⇒ Object
Returns the value of attribute players_.
10 11 12 |
# File 'lib/studio_game/game.rb', line 10 def players_ @players_ end |
Instance Method Details
#add_player(player) ⇒ Object
56 57 58 |
# File 'lib/studio_game/game.rb', line 56 def add_player(player) @players_ << player end |
#game_round_count ⇒ Object
29 |
# File 'lib/studio_game/game.rb', line 29 def game_round_count = @game_round += 1 |
#high_score_entry(player_obj, b = "yes") ⇒ Object
118 119 120 121 122 |
# File 'lib/studio_game/game.rb', line 118 def high_score_entry(player_obj, b = "yes") name = player_obj.name.ljust(12, '.') score = player_obj.score.to_s.rjust(12, '.') "#{name}#{score} + #{b}" end |
#load_players(from_file) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/studio_game/game.rb', line 19 def load_players(from_file) File.readlines(from_file, chomp: true).each do |l| p = Player.load_players_(l) @players_ << p end rescue Errno::ENOENT puts "#{from_file} does not exist ..." exit 1 end |
#play_the_game(rounds = 1) ⇒ Object
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 87 88 89 90 91 92 |
# File 'lib/studio_game/game.rb', line 60 def play_the_game(rounds = 1) @round_count = rounds puts Treasure_Trove.treasure_items 1.upto(@round_count) do |i| puts '*' * 10 + " #{@game_name} " + '*' * 10 + " round #{i}" puts "the following treasures are in the map:" Treasure_Trove::TREASURES.each do |treasure| puts "·"*30 + "#{treasure.name} worth #{treasure.points} points" + "·"*30 end puts ("Before the carnage").upcase + "\n" @players_.each { |k| puts "#{k.name} starting score: #{k.score}"} @players_.each do |pdd| case roll_die when 1..2 pdd.drain puts "#{pdd.name} got drained 😩 " \ "Health now at #{pdd.health}" when 3..4 puts "#{pdd.name} got skipped" else pdd.boost puts "#{pdd.name} got boosted 😁 " \ "Health now at #{pdd.health}" end item_found = Treasure_Trove.random_treasure pdd.found_treasure(item_found.name, item_found.points) pdd.treasure_total(item_found.points) puts "#{pdd.name} found a #{item_found.name} worth #{item_found.points} points" end puts (("After the carnage".upcase) +"\n") puts @players_ end end |
#print_score ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/studio_game/game.rb', line 125 def print_score @dot_count = 0 @players_.each do |player| total_value = 0 puts "#{player.name} treasure totals: " + "\n" player.found_treasures.each do | item | padding = 15 name_length = (item[0].length) + 1 @dot_count = padding - name_length puts "#{item[0]}:" + "." * @dot_count + "#{item[1]}" + "\n" total_value += item[1] end puts "total:" + "." * 9 + "$#{total_value}" + "\n" end end |
#print_stats ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/studio_game/game.rb', line 107 def print_stats game_name_len = @game_name.length left_padding = 30 right_padding = 40 - game_name_len puts "ª"*left_padding + " #{@game_name} " + "ª"*right_padding puts "ª"*left_padding + " Stats: " + "ª"*34 print_score listy = @players_.sort_by{|t| t.score}.reverse listy.each {|ele| puts high_score_entry(ele, "print stats")} end |
#roll ⇒ Object
not needed
44 |
# File 'lib/studio_game/game.rb', line 44 def roll = rand(1..6) # not needed |
#roll_die ⇒ Object
46 47 48 49 50 51 |
# File 'lib/studio_game/game.rb', line 46 def roll_die # number = [1,1,2,5,6,6].sample number = rand(1..6) audit(number) # audit is called on the Auditable module return number end |
#round_count ⇒ Object
40 41 42 |
# File 'lib/studio_game/game.rb', line 40 def round_count @round_count end |
#save_high_scores(to_file = "high_scores.txt") ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/studio_game/game.rb', line 31 def save_high_scores(to_file = "high_scores.txt") File.open(to_file, "a") do |file| file.puts "#{@game_name} Iteration: #{ game_round_count} with #{@round_count} rounds" sorted_players.each do |l| file.puts high_score_entry(l, "save high scores") end end end |
#sorted_players ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/studio_game/game.rb', line 95 def sorted_players sort_players = [] sort_players = @players_.sort_by { |player| player.score }.reverse puts "--High Scores--" sort_players.each do |iter| padding = 12 name_length = iter.name.length dot_count = padding - name_length puts "#{iter.name}" + "." * dot_count + "#{iter.score}" end end |
#to_s ⇒ Object
54 |
# File 'lib/studio_game/game.rb', line 54 def to_s = "#{@game_name} is the name of the game" |