class App

Run top level methods and initialize menu

Attributes

main_menu[RW]
prizes[RW]
questions[RW]
statistics[RW]

Public Class Methods

new() click to toggle source
# File lib/gemillionaire/app.rb, line 16
def initialize
  @args = ARGV.clone
  check_dependencies
  @questions = JSON.parse(File.read("#{__dir__}/questions.json"))
  @statistics = JSON.parse(File.read("#{__dir__}/hiscores.json"))
  @prizes = ['0', '500', '1,000', '2,000', '3,000', '5,000', '7,500', '10,000', '12,500', '15,000',
             '25,000', '50,000', '100,000', '250,000', '500,000', '1,000,000'].freeze
  @main_menu = [
    { name: 'New Game', value: -> { new_game } },
    { name: 'Instructions', value: -> { run_instructions } },
    { name: 'Hiscores', value: -> { run_hiscores } },
    { name: 'Exit', value: -> { exit } }
  ]
end

Public Instance Methods

any_key() click to toggle source
# File lib/gemillionaire/app.rb, line 99
def any_key
  print 'Press any key to continue.'.green
  $stdin.getch
end
check_dependencies() click to toggle source
# File lib/gemillionaire/app.rb, line 36
def check_dependencies
  missing_file = 'app.rb' unless File.exist?("#{__dir__}/app.rb")
  missing_file = 'game.rb' unless File.exist?("#{__dir__}/game.rb")
  missing_file = 'hiscores.json' unless File.exist?("#{__dir__}/hiscores.json")
  missing_file = 'questions.json' unless File.exist?("#{__dir__}/questions.json")
  return unless missing_file

  puts 'Missing File Error'.bold.red
  puts "Oops! You appear to be missing the file #{missing_file.bold}. Please re-install the gem."
  puts "Visit #{'https://www.github.com/mjsterling/T1A3'.underline} for more information."
  any_key
  exit
end
display_intro() click to toggle source
# File lib/gemillionaire/app.rb, line 64
def display_intro
  puts ('─' * 50).yellow
  puts "#{' ' * 12}🇬 ​​​​​🇪​​​​​ 🇲​​​​​ 🇮 ​​🇱​​​​​ 🇱​​​​​ 🇮​​​​​ 💎🇳​​​​​ 🇦​​​​​ 🇮​​​​​ 🇷​​​​​ 🇪#{' ' * 12}​​​​​"
  puts "#{('─' * 50).yellow}\n"
  puts "Who Wants To Be A Terminal-Based Millionaire?\n".yellow.bold
  puts 'Created by Matthew Sterling, 2021.'
  puts "Source code: https://github.com/mjsterling/T1A3 \n"
end
menu() click to toggle source
new_game() click to toggle source
# File lib/gemillionaire/app.rb, line 31
def new_game
  game = Game.new
  game.start
end
run_hiscores() click to toggle source
# File lib/gemillionaire/app.rb, line 87
def run_hiscores
  @statistics = JSON.parse(File.read("#{__dir__}/hiscores.json"))
  games_played = @statistics['games_played'].to_i
  total_winnings = @statistics['total_winnings'].to_i
  average_earnings = games_played.zero? ? 0 : total_winnings / games_played
  puts "\nTotal games played: #{games_played}"
  puts "Top score: #{@statistics['hiscore']} 💎"
  puts "Total winnings: #{total_winnings} 💎"
  puts "Average earnings per game: #{average_earnings} 💎\n"
  any_key
end
run_instructions() click to toggle source
# File lib/gemillionaire/app.rb, line 73
def run_instructions
  puts "\nHow To Play Gemillionaire:".bold
  puts 'You must answer 15 multiple-choice questions correctly in a row to win 1 million 💎.'
  puts 'You may walk away at any time and keep your earnings.'
  print 'If you answer a question wrong, you fall back to the last guarantee point - '
  puts '5,000 💎 if 5 questions correct, 25,000 💎 if 10 questions correct.'
  puts 'At any point, you may use one of the three lifelines:'
  puts '   - 50/50: Removes two incorrect options from the list'
  puts '   - Ask The Audience: Each audience member answers the question and the results are displayed as a graph.'
  puts '   - Phone-A-Friend: A friend will say what they think the answer is. They are not always correct, beware!'
  puts "Each lifeline may only be used once.\n"
  any_key
end