class StudioGame::Game
Attributes
Public Class Methods
Class constructor.
@params ‘title’ [String] a name for the game. @return Game
[Object] New instance of game. @example
[ #<Game:0x0000462ed36a1e0 @title="Knuckeheads", @players=[]" > ]
# File lib/studio_game/game.rb, line 23 def initialize(title) @title = title @players = [] end
Public Instance Methods
To include a player object into an instance array.
@params ‘player’ [Object] @return [Array] a group of players objects inside an array. @example
[ #<Player:0x00005628d364f4e0 @found_treasures={}, @health=100, @name="Moe" >, #<Player:0x00005628d3335340 @found_treasures={}, @health=60, @name="Larry" > ]
# File lib/studio_game/game.rb, line 84 def add_player(player) @players << player end
# File lib/studio_game/game.rb, line 139 def high_score_entry(player) formatted_name = player.name.ljust(20, '.') "#{formatted_name} #{player.score}" end
To read from a csv file to add players for a game, in order for that player(s) to play a game.
@params ‘from_file’ [String] a directory path to access csv file. @return [void] @example
csv: 1 moe,100 2 larry, 3 curly,65
# File lib/studio_game/game.rb, line 39 def load(from_file) File.readlines(from_file).each do |line| add_player(Player.from_csv(line)) end end
To iterate each player in n-times to play a game.
@params ‘round’ [Integer] @return [void] @note It prints the total point when finished
# File lib/studio_game/game.rb, line 94 def play(rounds) print_treasures puts "This Game is up to #{rounds} round(s)" 1.upto(rounds).each do |round| break if block_given? && yield players_round(round) end puts "TOTAL Points #{total_points}" end
# File lib/studio_game/game.rb, line 117 def players_round(round) puts ". . . . . . . . . . . . . . . . . --- *> Round# #{round} <* --- . . . . . . . . . . . . . . . . ." @players.each do |player| GameTurn.take_turn(player) puts player end end
# File lib/studio_game/game.rb, line 125 def print_stats @players.sort_by(&:points).reverse.each do |player| puts player player.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end puts high_score_entry(player) end end
To iterate each over TreasureTrove::TREASURES to print its name and points.
@return [void] @note It prints the total point when finished
# File lib/studio_game/game.rb, line 110 def print_treasures puts "There are #{TreasureTrove::TREASURES.size} treasures to be found:" TreasureTrove::TREASURES.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end end
To create a csv file with player(s) names and final game score, as the return of a game played.
@params ‘to_file’ [String] a directory path to save the csv file created. @return [void] @example
csv: 1 moe,850 2 larry,680, 3 curly,250
@note Default path/file name: docs/game_results.csv
# File lib/studio_game/game.rb, line 57 def save_high_scores(to_file = 'doc/game/game_results.csv') File.open(to_file, 'w') do |file| file.puts "#{@title} High Scores:" @players.sort_by(&:points).reverse.each do |player| file.puts high_score_entry(player) end end end
# File lib/studio_game/game.rb, line 135 def total_points @players.reduce(0) { |sum, player| sum + player.points } end