class StudioGame::Game

Attributes

title[R]

Public Class Methods

new(title) click to toggle source

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

add_player(player) click to toggle source

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
high_score_entry(player) click to toggle source
# File lib/studio_game/game.rb, line 139
def high_score_entry(player)
  formatted_name = player.name.ljust(20, '.')
  "#{formatted_name} #{player.score}"
end
load(from_file) click to toggle source

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
play(rounds) { || ... } click to toggle source

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
players_round(round) click to toggle source
# 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
print_stats() click to toggle source
print_treasures() click to toggle source

To iterate each over TreasureTrove::TREASURES to print its name and points.

@return [void] @note It prints the total point when finished

save_high_scores(to_file = 'doc/game/game_results.csv') click to toggle source

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
total_points() click to toggle source
# File lib/studio_game/game.rb, line 135
def total_points
  @players.reduce(0) { |sum, player| sum + player.points }
end