class Game
Constants
- DIFFICULTY
- PATH
Attributes
attempts[RW]
count_minus[RW]
count_plus[RW]
difficulty[RW]
hints[RW]
secret_hash[RW]
symbol_guess_number[RW]
symbol_guess_position[RW]
total_attempts[RW]
total_hints[RW]
user_name[RW]
win[RW]
Public Class Methods
new()
click to toggle source
# File lib/cbreaker/game.rb, line 10 def initialize @attempts = 0 @hints = 0 @difficulty = '' @count_minus = 0 @count_plus = 0 @symbol_guess_position = '+' @symbol_guess_number = '-' end
Public Instance Methods
add_rating()
click to toggle source
# File lib/cbreaker/game.rb, line 40 def add_rating rating = 1 new_sort_arr = [] sort_data.each do |data| new_sort_arr << { rating: rating }.merge!(data) rating += 1 end new_sort_arr end
check_attempt(user_string)
click to toggle source
# File lib/cbreaker/game.rb, line 78 def check_attempt(user_string) @user_hash = Hash[(0...4).zip user_string.split('').map(&:to_i)] array_of_guess_position = [] @attempts -= 1 compare_hashes @count_plus.times { array_of_guess_position << @symbol_guess_position } @count_minus.times { array_of_guess_position << @symbol_guess_number } array_of_guess_position end
choose_difficulty(choose_difficult_string)
click to toggle source
# File lib/cbreaker/game.rb, line 31 def choose_difficulty(choose_difficult_string) @difficulty = DIFFICULTY[choose_difficult_string.to_sym] @attempts = @difficulty[0] @hints = @difficulty[1] @difficulty = @difficulty[2] @total_attempts = @attempts @total_hints = @hint end
compare_hashes()
click to toggle source
# File lib/cbreaker/game.rb, line 88 def compare_hashes @secret_hash.merge(@user_hash) { |k, o, n| @secret_hash.reject! { |key| key == k } && @count_plus += 1 if o == n } @secret_hash.select { |_, value| @user_hash.value? value }.size.times { @count_minus += 1 } end
enter_user_name(string)
click to toggle source
# File lib/cbreaker/game.rb, line 20 def enter_user_name(string) validate_name(string) @user_name = string end
play_again?(string)
click to toggle source
# File lib/cbreaker/game.rb, line 100 def play_again?(string) @attempts = @total_attempts @hints = @total_hints @count_minus = 0 @count_plus = 0 return true if string == 'yes' return false if string == 'no' end
save?(string)
click to toggle source
# File lib/cbreaker/game.rb, line 93 def save?(string) hash = { name: @user_name, difficult: @difficulty, total_attempts: @total_attempts, attempts: @attempts, total_hints: @total_hints, hints: @hints } File.open(PATH, 'a') { |file| file.write(hash.to_yaml) } if string == 'yes' end
show_hint()
click to toggle source
# File lib/cbreaker/game.rb, line 70 def show_hint return false if @hints.zero? @hints -= 1 secret_arr_value = @secret_hash.values secret_arr_value.delete_at(rand(secret_arr_value.length)) end
sort_data()
click to toggle source
# File lib/cbreaker/game.rb, line 56 def sort_data data = Psych.load_stream(File.read(PATH)) data.sort_by { |h| [h[:difficult], h[:attempts], h[:hints]] }.reverse end
start()
click to toggle source
# File lib/cbreaker/game.rb, line 66 def start @secret_hash = Hash[(0...4).zip Array.new(4) { rand(1...6) }] end
stats()
click to toggle source
# File lib/cbreaker/game.rb, line 50 def stats rows = [] add_rating.each { |d| rows << d.map { |_k, v| v } } to_table(rows) end
to_table(rows)
click to toggle source
# File lib/cbreaker/game.rb, line 61 def to_table(rows) Terminal::Table.new headings: ['Rating', 'Name', 'Difficulty', 'Attempts Total', 'Attempts Used', 'Hints Total', 'Hints Used'], rows: rows end