class Init

Public Class Methods

new() click to toggle source
# File bin/treasure_game, line 9
def initialize
  @game = assign_game_name
  @rounds = get_rounds
  add_players_to_game(get_players)
  @game.play(@rounds)
end

Public Instance Methods

add_players_to_game(players) click to toggle source
# File bin/treasure_game, line 71
def add_players_to_game(players)
  players.each do |p|
    name, type = p.split(",")
    next unless  name =~ /(\w+|\d+)/
    type ||= ""
    @game.add_player(name: name.strip, health: rand(50..100),
                     type: type.strip)
  end
end
assign_game_name() click to toggle source
# File bin/treasure_game, line 34
def assign_game_name
  TreasureGame::Game.new(title: get_response("What would you like to name your game?"))
end
get_players() click to toggle source
# File bin/treasure_game, line 42
def get_players
  options = "Enter:\n(1) to load player data from a file\n" +
          "(2) to enter them manually\n(q) to quit"
  response = ""
  until response == ("1" || "2" || "q" || "Q" )
    puts options
    response = gets.chomp
    case response
    when "1"
      return load_player_file
    when "2"
      return get_players_from_input
    when /(q|Q)/
      abort("Sorry you changed your mind. Play again sometime.")
    else
      puts unknown_response(response, options)
    end
  end
end
get_players_from_input() click to toggle source
# File bin/treasure_game, line 99
def get_players_from_input
  i = 0
  players = []
  player = ""
  until player == ("P" || "p" || "Q" || "q")
    i += 1
    puts %Q(Enter the #{ordinalize(i)} player's info in this format: name,type.
         (or Q to Quit, D when Done):)
    player = gets.strip
    case player
    when ("D" || "d")
      return players
    when ("Q" || "q")
      abort("Thanks for thinking about playing; please play again some other time.")
    else
      players << player
    end
  end
end
get_response(options) click to toggle source
# File bin/treasure_game, line 62
def get_response(options)
  puts options
  gets.chomp
end
get_rounds() click to toggle source
# File bin/treasure_game, line 38
def get_rounds
  get_response("How many rounds would you like to play? (enter a number)").to_i
end
load_player_file() click to toggle source
# File bin/treasure_game, line 81
def load_player_file
  options = "What's the name of your file?\nEnter file path, or\n" +
            "(d) for default, or\n(q) to Quit"
  loop do
    file_name = get_response(options)
    if file_name == "D" || file_name == "d"
      return File.readlines(@@default_player_file)
    elsif File.file?(file_name)
      return File.readlines(file_name)
    elsif file_name =~ /(Q|q)/
      abort("Sorry we couldn't find your file. Try entering your players manually.")
    else
      #binding.pry
      puts unknown_response(file_name)
    end
  end
end
ordinal(number) click to toggle source
# File bin/treasure_game, line 16
def ordinal(number)
  abs_number = number.to_i.abs
  if (11..13).include?(abs_number % 100)
    "th"
  else
    case abs_number % 10
    when 1; "st"
    when 2; "nd"
    when 3; "rd"
    else "th"
    end
  end
end
ordinalize(number) click to toggle source
# File bin/treasure_game, line 30
def ordinalize(number)
  "#{number}#{ordinal(number)}"
end
unknown_response(response) click to toggle source
# File bin/treasure_game, line 67
def unknown_response(response)
  "Sorry, I don't recognize #{response} as a valid option."
end