class Mastermind::Oscar::RecordManager

Attributes

user[R]

Public Class Methods

create_save_files() click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 12
def self.create_save_files
   Dir.mkdir(file_path) unless Dir.exist?(file_path)
    level = Mastermind::Oscar.game_level.values
    level.each do |lvl|
      File.open("#{file_path}top_ten_#{lvl}.yaml","w+")
      File.open("#{file_path}#{lvl}_record.txt", "w+")
    end
end
file_path() click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 25
def self.file_path
  # File.join(File.dirname(File.expand_path(__FILE__)), '../../../files/')
  "files/"
end
get_heros_file(difficulty) click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 82
def self.get_heros_file(difficulty)
  "#{file_path}top_ten_#{difficulty}.yaml"
end
get_instructions() click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 132
      def self.get_instructions
        msg = <<-EOS
          Playing Mastermind is pretty easy.

##Rules

* Once the game starts, you have the option to either play, view instructions, view top ten scores or view score records
* To play the game, enter p
* To view instructions, enter i
* To view top ten scores, enter t
* To view score records, enter s
* Once you enter p to play, your are given the option to select a difficulty level; beginner, intermediate or expert. The level default to beginner for invalid choice.
* You will be required to enter you name to aid in recording your score
* Computer will generate a set of random characters made of colours. The lenght of the characters generated and the colours used depends on the game level.
* For beginners, 4 characters consisting of red, blue, green and/or yellow. e.g. rbgy
* For intermediate, 6 characters consisting of red, blue, green, yellow and/or cyan e.g. rrbyc
* For expert, 8 characters consisting of red, blue, green, yellow, cyan and magenta e.g. bgrrgm
* Note that colours can be repeated in the sequence
* You are to guess the sequence by inputing the combination.
* You will be given helpful feedback on each guess you make. Study the feedback to know the colours you were able to guess and their right position in the sequence.
* After 12 unsuccessful attempts, the game is over and you lost
* You can stop the game at any time by inputing q

Have fun
        EOS
      end
get_records() click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 116
def self.get_records
  #levels = []
  #if difficulty
   # levels << difficulty
  #else
    levels = Mastermind::Oscar.game_level
    levels = levels.values
  #end
  
  files = []
  levels.each do |level|
    files << File.open("files/"+level.to_s+"_record.txt", "r+")
  end          
  files
end
get_top_ten(difficulty) click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 159
def self.get_top_ten (difficulty)
  file = get_heros_file(difficulty)
  content = []
  File.open(file, "r+") do |val| 
    content = YAML::load(val)
  end
  content = [] if !content
  content
end
new(name = nil, stream = STDIN) click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 7
def initialize(name = nil, stream = STDIN)
  set_read_stream(stream)
  name.nil? ? set_user : @user = name
                  end

Public Instance Methods

check_for_top_ten(code, guess, time, difficulty) click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 62
def check_for_top_ten(code, guess, time, difficulty)
  top_ten = self.class.get_top_ten(difficulty)
  position = is_hero?(top_ten, guess, time)

  position = top_ten.size if(!position && top_ten.size < 10)

  if position
    top_ten = insert_in_top_ten(prep_hash(code, guess, time), position, top_ten)
 
    save_top_ten(top_ten, difficulty)
    return position
  end
end
close() click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 58
def close
  @input_file.close if @input_file
end
file_path() click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 21
def file_path
  RecordManager.file_path
end
initalize_file() click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 52
def initalize_file
  text = "\nName:\t#{@user}\n"
  text += Time.now.strftime("%l:%M%P %b %d, %Y")
  print_to_file(text)
end
insert_in_top_ten(hero, position, array) click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 86
def insert_in_top_ten(hero, position, array)
  new_arr = []
  i = 0
  shift = false
  limit  = array.size
  limit = (limit >= 10) ? limit : limit + 1
  
  limit.times do
    if i == position
      new_arr << hero
      shift = true
    else
      index = shift ? i - 1 : i
      new_arr << array[index]
    end
    i += 1
  end

  new_arr
end
is_hero?(heros, guess, time) click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 169
def is_hero?(heros, guess, time)
  return unless heros.is_a?(Array)
  guess = guess.to_i
  heros.each_with_index do |hero, index|
    hero[:guess] = hero[:guess].to_i
    if hero[:guess].to_i > guess
      return index
    elsif hero[:guess] == guess
      return index if hero[:time] >time            
    end
  end
  nil
end
open_save_file(difficulty) click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 39
def open_save_file(difficulty)
  @input_file = File.open(file_path+difficulty.to_s+"_record.txt", "a+")
  initalize_file
end
prep_hash(code, guess, time) click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 107
def prep_hash(code, guess, time)
  {
    name: user,
    code: code,
    guess: guess,
    time: time
  }
end
print_to_file(content) click to toggle source
save_top_ten(array,difficulty) click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 76
def save_top_ten(array,difficulty)
  file = self.class.get_heros_file(difficulty)
  serial = YAML::dump(array)
  File.open(file, "w+") {|f| f.puts serial}
end
set_read_stream(stream = STDIN) click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 44
def set_read_stream (stream = STDIN)
  @stream = stream
end
set_user() click to toggle source
# File lib/mastermind/oscar/record_manager.rb, line 30
      def set_user
        print <<-EOS
            \tAwesome!, you want to play. Let's begin!
            \tMy name is Mastermind; what's is yours?
            EOS
        @user = @stream.gets.chomp.capitalize
        @user = "Anonymous" if @user.empty?
      end