Module: GameUtils

Includes:
CodebreakerArtem
Included in:
CodebreakerArtem::Game
Defined in:
lib/codebreaker_artem/game_utils.rb

Constant Summary

Constants included from CodebreakerArtem

CodebreakerArtem::VERSION

Instance Method Summary collapse

Instance Method Details

#exit?(input) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/codebreaker_artem/game_utils.rb', line 69

def exit?(input)
  exit if input =~ /exit/
  false
end

#guess_promptObject



14
15
16
# File 'lib/codebreaker_artem/game_utils.rb', line 14

def guess_prompt
  print "Please enter your guess no. #{@guess_count}: "
end

#lose_msgObject



99
100
101
102
# File 'lib/codebreaker_artem/game_utils.rb', line 99

def lose_msg
  puts "You've used all your attempts (#{Game::MAX_GUESS_NUMBER})" \
        " and LOST THE GAME\n"
end

#one_hint_onlyObject



60
61
62
# File 'lib/codebreaker_artem/game_utils.rb', line 60

def one_hint_only
  puts 'You have only one hint'
end

#play_againObject



64
65
66
67
# File 'lib/codebreaker_artem/game_utils.rb', line 64

def play_again
  exit unless yes? { 'Would you like to play one more time? (y/n): ' }
  start
end

#reveal_codeObject



104
105
106
# File 'lib/codebreaker_artem/game_utils.rb', line 104

def reveal_code
  puts "The secret code was #{@secret_code}\n"
end

#save_scoreObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/codebreaker_artem/game_utils.rb', line 28

def save_score
  return false unless yes? { 'Do you want to save your score? (y/n): ' }
  begin
    Dir.mkdir('./score') unless File.exist?('./score')
    file = File.new('./score/score.txt', 'a')
  rescue
    puts "Can't create file './score/score.txt'"
    return
  end
  write_score_to_file file
  file.close
end

#submit_guessObject



18
19
20
21
22
23
24
25
26
# File 'lib/codebreaker_artem/game_utils.rb', line 18

def submit_guess
  guess = $stdin.gets.chomp
  exit?(guess)
  return false if take_hint(guess)
  return wrong_code_length unless guess.length == 4
  return wrong_code_numbers unless guess =~ /[1-6]{4}/
  @guess_count += 1
  guess
end

#take_hint(input = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/codebreaker_artem/game_utils.rb', line 50

def take_hint(input = nil)
  return false unless input =~ /hint/i
  return false unless hint_available?
  position = @numbers_guess_count.index(@numbers_guess_count.min)
  secret_number = @secret_code[position]
  puts "HINT: Number #{secret_number} is in position #{position + 1}"
  @hint_available = false
  secret_number
end

#welcome_msgObject



6
7
8
9
10
11
12
# File 'lib/codebreaker_artem/game_utils.rb', line 6

def welcome_msg
  print "Welcome to the Codebreaker Game!\n" \
    "New secret code has just been generated\n" \
    "You have #{Game::MAX_GUESS_NUMBER} attempts\n" \
    "Type 'hint' to take a hint\n" \
    "Type 'exit' to exit the game\n\n"
end

#win_msgObject



95
96
97
# File 'lib/codebreaker_artem/game_utils.rb', line 95

def win_msg
  puts "\nCONGRATULATIONS! You've won the Codebreaker game!"
end

#write_score_to_file(file) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/codebreaker_artem/game_utils.rb', line 41

def write_score_to_file(file)
  print 'Please enter your name: '
  name = $stdin.gets.chomp
  file << "Name: #{name}\n"
  file << "Time: #{Time.now}\n"
  file << "Score: #{@score}\n\n"
  puts "Your data was added to a file #{file.path}"
end

#wrong_code_lengthObject



85
86
87
88
# File 'lib/codebreaker_artem/game_utils.rb', line 85

def wrong_code_length
  puts 'Code contains 4 numbers, please try again'
  false
end

#wrong_code_numbersObject



90
91
92
93
# File 'lib/codebreaker_artem/game_utils.rb', line 90

def wrong_code_numbers
  puts 'All numbers are between 1 and 6, please try again'
  false
end

#yes?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
# File 'lib/codebreaker_artem/game_utils.rb', line 74

def yes?
  print yield if block_given?
  loop do
    answer = $stdin.gets.chomp
    exit?(answer)
    return false if answer =~ /^n/i
    return true if answer =~ /^y/i
    puts "Please enter 'y' or 'n': "
  end
end